-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
112 lines (99 loc) · 3.32 KB
/
Copy pathApp.js
File metadata and controls
112 lines (99 loc) · 3.32 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { useState } from 'react';
import './App.css';
function App() {
const [tasks, setTasks] = useState([
{ text: 'Eat', completed: true },
{ text: 'Sleep', completed: false },
{ text: 'Reading', completed: false },
]);
const [filter, setFilter] = useState('all');
const [newTask, setNewTask] = useState('');
const [editingIndex, setEditingIndex] = useState(null);
const [editText, setEditText] = useState('');
const filteredTasks = tasks.filter(task => {
if (filter === 'all') return true;
if (filter === 'active') return !task.completed;
if (filter === 'completed') return task.completed;
return true;
});
const toggleTaskCompleted = index => {
const newTasks = [...tasks];
newTasks[index].completed = !newTasks[index].completed;
setTasks(newTasks);
};
const addTask = () => {
if (newTask.trim()) {
setTasks([...tasks, { text: newTask, completed: false }]);
setNewTask('');
}
};
const startEdit = (index) => {
setEditingIndex(index);
setEditText(tasks[index].text);
};
const saveEdit = () => {
if (editText.trim()) {
const newTasks = [...tasks];
newTasks[editingIndex].text = editText;
setTasks(newTasks);
setEditingIndex(null);
setEditText('');
}
};
const deleteTask = (index) => {
const newTasks = tasks.filter((_, i) => i !== index);
setTasks(newTasks);
};
return (
<div className="TodoApp">
<h1>Today's Schedule</h1>
<p>What duty needs to be done?</p>
<input
type="text"
placeholder="Add a new task..."
value={newTask}
onChange={(e) => setNewTask(e.target.value)}
/>
<button onClick={addTask}>Add</button>
<div className="filter-buttons">
<button className={filter === 'all' ? 'active' : ''} onClick={() => setFilter('all')}>All</button>
<button className={filter === 'active' ? 'active' : ''} onClick={() => setFilter('active')}>Active</button>
<button className={filter === 'completed' ? 'active' : ''} onClick={() => setFilter('completed')}>Completed</button>
</div>
<h1>Tasks remaining</h1>
<ul className="task-list">
{filteredTasks.map((task, index) => (
<li key={index}>
<div className="task-label">
<input
type="checkbox"
checked={task.completed}
onChange={() => toggleTaskCompleted(index)}
/>
<span style={{ textDecoration: task.completed ? 'line-through' : 'none' }}>{task.text}</span>
</div>
<div className="buttons">
{editingIndex === index ? (
<>
<input
type="text"
value={editText}
onChange={(e) => setEditText(e.target.value)}
/>
<button onClick={saveEdit}>Save</button>
</>
) : (
<>
<button className="edit-btn" onClick={() => startEdit(index)}>Edit</button>
<button className="delete-btn" onClick={() => deleteTask(index)}>Delete</button>
</>
)}
</div>
</li>
))}
</ul>
</div>
);
}
export default App;
/**App.js created by Ojo Damilare */