Skip to content

Commit afe89f2

Browse files
committed
✨ feat: Now you can add tasks pressing 'Enter' on any part of the page
1 parent 642b12b commit afe89f2

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

app/Components/ConfigMenu/ThemeMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const ThemePod = ({
9494
};
9595

9696
export default function ThemeMenu() {
97-
const [theme, setTheme] = useLocalStorage("theme", "dark");
97+
const [theme, setTheme] = useLocalStorage("theme", "dim");
9898

9999
// useState(
100100
// () => {

app/Components/TasksApp/TaskApp.tsx

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
"use client";
22

3-
import { useReducer } from 'react';
4-
import AddTask from './AddTask';
5-
import TaskList from './TaskList';
3+
import { useEffect, useReducer } from "react";
4+
import AddTask from "./AddTask";
5+
import TaskList from "./TaskList";
66

77
export default function TaskApp() {
8-
const [tasks, dispatch] = useReducer(
9-
tasksReducer,
10-
initialTasks
11-
);
8+
const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
9+
10+
useEffect(() => {
11+
const handleKeyDown = (event: KeyboardEvent) => {
12+
if (event.key === "Enter") {
13+
handleAddTask("");
14+
}
15+
};
16+
17+
document.addEventListener("keydown", handleKeyDown);
18+
19+
return () => {
20+
document.removeEventListener("keydown", handleKeyDown);
21+
};
22+
}, []);
1223

1324
function handleAddTask(text: any) {
1425
dispatch({
15-
type: 'added',
26+
type: "added",
1627
id: nextId++,
1728
text: text,
18-
isNew: true
29+
isNew: true,
1930
});
2031
}
2132

2233
function handleChangeTask(task: any) {
2334
dispatch({
24-
type: 'changed',
25-
task: task
35+
type: "changed",
36+
task: task,
2637
});
2738
}
2839

29-
function handleDeleteTask(taskId: any ) {
40+
function handleDeleteTask(taskId: any) {
3041
dispatch({
31-
type: 'deleted',
32-
id: taskId
42+
type: "deleted",
43+
id: taskId,
3344
});
3445
}
3546

3647
return (
3748
<>
38-
<AddTask
39-
onAddTask={handleAddTask}
40-
/>
49+
<AddTask onAddTask={handleAddTask} />
4150
<TaskList
4251
tasks={tasks}
4352
onChangeTask={handleChangeTask}
@@ -49,35 +58,38 @@ export default function TaskApp() {
4958

5059
function tasksReducer(tasks: any, action: any) {
5160
switch (action.type) {
52-
case 'added': {
53-
return [...tasks, {
54-
id: action.id,
55-
text: action.text,
56-
done: false,
57-
isNew: action.isNew
58-
}];
61+
case "added": {
62+
return [
63+
...tasks,
64+
{
65+
id: action.id,
66+
text: action.text,
67+
done: false,
68+
isNew: action.isNew,
69+
},
70+
];
5971
}
60-
case 'changed': {
61-
return tasks.map( (t: any) => {
72+
case "changed": {
73+
return tasks.map((t: any) => {
6274
if (t.id === action.task.id) {
6375
return action.task;
6476
} else {
6577
return t;
6678
}
6779
});
6880
}
69-
case 'deleted': {
70-
return tasks.filter( (t: any) => t.id !== action.id);
81+
case "deleted": {
82+
return tasks.filter((t: any) => t.id !== action.id);
7183
}
7284
default: {
73-
throw Error('Unknown action: ' + action.type);
85+
throw Error("Unknown action: " + action.type);
7486
}
7587
}
7688
}
7789

7890
let nextId = 3;
7991
const initialTasks = [
80-
{ id: 0, text: 'Philosopher’s Path', done: true, isNew: false },
81-
{ id: 1, text: 'Visit the temple', done: false, isNew: false},
82-
{ id: 2, text: 'Drink matcha', done: false, isNew: false }
92+
{ id: 0, text: "Philosopher’s Path", done: true, isNew: false },
93+
{ id: 1, text: "Visit the temple", done: false, isNew: false },
94+
{ id: 2, text: "Drink matcha", done: false, isNew: false },
8395
];

app/Components/TasksApp/TaskList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export default function TaskList({
1111
onDeleteTask: any;
1212
}) {
1313
return (
14-
<ul className="w-1/2 mt-24 flex justify-center flex-col">
14+
<ul
15+
className="w-1/2 mt-24 flex justify-center flex-col"
16+
>
1517
{[...tasks].reverse().map((task: any) => (
1618
<li className="flex flex-row w-full my-2" key={task.id}>
1719
<Task task={task} onChange={onChangeTask} onDelete={onDeleteTask} />
@@ -49,7 +51,7 @@ function Task({
4951

5052
const audio = new Audio("/sounds/delete.mp3");
5153
audio.play();
52-
}
54+
};
5355

5456
console.log("Task", task);
5557

0 commit comments

Comments
 (0)