-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskDashboard.js
More file actions
66 lines (58 loc) · 2.12 KB
/
Copy pathTaskDashboard.js
File metadata and controls
66 lines (58 loc) · 2.12 KB
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
import React, { useEffect, useState } from 'react';
import axios from 'axios';
const TaskDashboard = () => {
const [tasks, setTasks] = useState([]);
useEffect(() => {
const fetchTasks = async () => {
try {
const response = await axios.get('http://localhost:5000/api/tasks');
setTasks(response.data);
} catch (error) {
console.error('Error fetching tasks:', error);
}
};
fetchTasks();
}, []);
const createTask = async (title) => {
try {
const response = await axios.post('http://localhost:5000/api/tasks', { title, completed: false });
setTasks([...tasks, response.data]);
} catch (error) {
console.error('Error creating task:', error);
}
};
const updateTask = async (id, updatedTask) => {
try {
const response = await axios.put(`http://localhost:5000/api/tasks/${id}`, updatedTask);
setTasks(tasks.map(task => (task.id === id ? response.data : task)));
} catch (error) {
console.error('Error updating task:', error);
}
};
const deleteTask = async (id) => {
try {
await axios.delete(`http://localhost:5000/api/tasks/${id}`);
setTasks(tasks.filter(task => task.id !== id));
} catch (error) {
console.error('Error deleting task:', error);
}
};
return (
<div>
<h1>Task Dashboard</h1>
<button onClick={() => createTask('New Task')}>Add Task</button>
<ul>
{tasks.map(task => (
<li key={task.id}>
{task.title}
<button onClick={() => updateTask(task.id, { ...task, completed: !task.completed })}>
{task.completed ? 'Mark Incomplete' : 'Mark Complete'}
</button>
<button onClick={() => deleteTask(task.id)}>Delete</button>
</li>
))}
</ul>
</div>
);
};
export default TaskDashboard;