1
1
"use client" ;
2
2
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" ;
6
6
7
7
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
+ } , [ ] ) ;
12
23
13
24
function handleAddTask ( text : any ) {
14
25
dispatch ( {
15
- type : ' added' ,
26
+ type : " added" ,
16
27
id : nextId ++ ,
17
28
text : text ,
18
- isNew : true
29
+ isNew : true ,
19
30
} ) ;
20
31
}
21
32
22
33
function handleChangeTask ( task : any ) {
23
34
dispatch ( {
24
- type : ' changed' ,
25
- task : task
35
+ type : " changed" ,
36
+ task : task ,
26
37
} ) ;
27
38
}
28
39
29
- function handleDeleteTask ( taskId : any ) {
40
+ function handleDeleteTask ( taskId : any ) {
30
41
dispatch ( {
31
- type : ' deleted' ,
32
- id : taskId
42
+ type : " deleted" ,
43
+ id : taskId ,
33
44
} ) ;
34
45
}
35
46
36
47
return (
37
48
< >
38
- < AddTask
39
- onAddTask = { handleAddTask }
40
- />
49
+ < AddTask onAddTask = { handleAddTask } />
41
50
< TaskList
42
51
tasks = { tasks }
43
52
onChangeTask = { handleChangeTask }
@@ -49,35 +58,38 @@ export default function TaskApp() {
49
58
50
59
function tasksReducer ( tasks : any , action : any ) {
51
60
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
+ ] ;
59
71
}
60
- case ' changed' : {
61
- return tasks . map ( ( t : any ) => {
72
+ case " changed" : {
73
+ return tasks . map ( ( t : any ) => {
62
74
if ( t . id === action . task . id ) {
63
75
return action . task ;
64
76
} else {
65
77
return t ;
66
78
}
67
79
} ) ;
68
80
}
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 ) ;
71
83
}
72
84
default : {
73
- throw Error ( ' Unknown action: ' + action . type ) ;
85
+ throw Error ( " Unknown action: " + action . type ) ;
74
86
}
75
87
}
76
88
}
77
89
78
90
let nextId = 3 ;
79
91
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 } ,
83
95
] ;
0 commit comments