-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
78 lines (70 loc) · 1.95 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useState, useEffect } from 'react';
import axios from 'axios';
function App() {
const [text, setText] = useState('');
const [texts, setTexts] = useState([]);
// Handle input change
const handleInputChange = (e) => {
setText(e.target.value);
};
// Handle form submission to save text
const handleSubmit = async (e) => {
e.preventDefault();
if (text) {
try {
await axios.post('http://your_ip/api/text', { content: text });
setText('');
fetchTexts(); // Refresh the list of texts
} catch (error) {
console.error('Error saving text:', error);
}
}
};
// Fetch all texts from the backend
const fetchTexts = async () => {
try {
const response = await axios.get('http://your_ip/api/texts');
setTexts(response.data);
} catch (error) {
console.error('Error fetching texts:', error);
}
};
// Handle deletion of a text item
const handleDelete = async (id) => {
try {
await axios.delete(`http://your_ip/api/text/${id}`);
fetchTexts(); // Refresh the list of texts
} catch (error) {
console.error('Error deleting text:', error);
}
};
useEffect(() => {
fetchTexts(); // Load texts when component mounts
}, []);
return (
<div className="App">
<h1>Text Recorder</h1>
<form onSubmit={handleSubmit}>
<textarea
value={text}
onChange={handleInputChange}
placeholder="Enter some text..."
rows="4"
cols="50"
/>
<br />
<button type="submit">Save Text</button>
</form>
<h2>Saved Texts</h2>
<ul>
{texts.map((textItem) => (
<li key={textItem.id}>
<strong>{new Date(textItem.created_at).toLocaleString()}</strong>: {textItem.content}
<button onClick={() => handleDelete(textItem.id)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default App;