-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskManager.js
More file actions
88 lines (80 loc) · 2.31 KB
/
Copy pathTaskManager.js
File metadata and controls
88 lines (80 loc) · 2.31 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
import React, { useState, useEffect } from "react";
function TaskManager() {
const [tasks, setTasks] = useState([]);
const [taskText, setTaskText] = useState("");
const [timerValue, setTimerValue] = useState(5); // Default timer value in seconds
const handleAddTask = () => {
if (taskText.trim() === "") return;
const newTask = {
text: taskText,
isCompleted: false,
timer: timerValue,
};
setTasks([...tasks, newTask]);
setTaskText("");
setTimerValue(5);
};
useEffect(() => {
const timers = tasks.map((task, index) => {
if (!task.isCompleted && task.timer > 0) {
const intervalId = setInterval(() => {
setTasks((prevTasks) =>
prevTasks.map((t, i) =>
i === index
? {
...t,
timer: t.timer - 1,
isCompleted: t.timer - 1 === 0 ? true : t.isCompleted,
}
: t
)
);
}, 1000);
return intervalId;
}
return null;
});
return () => {
timers.forEach((id) => id && clearInterval(id));
};
}, [tasks]);
return (
<div style={{ padding: "20px", fontFamily: "Arial, sans-serif" }}>
<h1>Task Manager with Timer</h1>
<div>
<input
type="text"
value={taskText}
placeholder="Enter task"
onChange={(e) => setTaskText(e.target.value)}
/>
<input
type="number"
value={timerValue}
placeholder="Timer (seconds)"
onChange={(e) => setTimerValue(Number(e.target.value))}
/>
<button onClick={handleAddTask}>Add Task</button>
</div>
<ul style={{ marginTop: "20px", listStyle: "none", padding: 0 }}>
{tasks.map((task, index) => (
<li
key={index}
style={{
textDecoration: task.isCompleted ? "line-through" : "none",
margin: "10px 0",
}}
>
<span>{task.text}</span>
<span style={{ marginLeft: "10px", color: "gray" }}>
{task.timer > 0
? ` - Time left: ${task.timer} seconds`
: " - Completed"}
</span>
</li>
))}
</ul>
</div>
);
}
export default TaskManager;