1
+ const todoListEl = document . getElementById ( "todoList" ) ;
2
+ const todoInputEl = document . getElementById ( "todoInput" ) ;
3
+
4
+ const API_URL = "http://localhost:3000/todos" ;
5
+
6
+ fetch ( API_URL )
7
+ . then ( ( response ) => response . json ( ) )
8
+ . then ( ( data ) => renderTodo ( data ) ) ;
9
+
10
+ const updateTodo = ( todoId , originalTitle ) => {
11
+ const todoItem = document . querySelector ( `#todo-${ todoId } ` ) ;
12
+ const inputli = document . createElement ( "input" ) ;
13
+ inputli . type = "text" ;
14
+ inputli . value = originalTitle ;
15
+
16
+ todoItem . innerHTML = "" ; // 기존 내용을 삭제
17
+ todoItem . appendChild ( inputli ) ;
18
+ // todoItem.innerHTML("");
19
+ // todoItem.append(inputli);
20
+ // const updatedTodo = {
21
+ // title : inputli.value,
22
+
23
+ // };
24
+
25
+
26
+ inputli . addEventListener ( "keydown" , ( event ) => {
27
+ if ( event . key === "Enter" ) {
28
+ fetch ( API_URL + "/" + todoId , {
29
+ method : "PATCH" ,
30
+ headers : {
31
+ "Content-Type" : "application/json" ,
32
+ } ,
33
+ body : JSON . stringify ( {
34
+ title : inputli . value ,
35
+ } ) ,
36
+ } )
37
+ . then ( ( response ) => response . json ( ) )
38
+ . then ( ( ) => {
39
+ return fetch ( API_URL ) ;
40
+ } )
41
+ . then ( ( response ) => response . json ( ) )
42
+ . then ( ( data ) => renderTodo ( data ) ) ;
43
+ }
44
+ } ) ;
45
+
46
+ } ;
47
+
48
+ const renderTodo = ( newTodos ) => {
49
+ todoListEl . innerHTML = "" ;
50
+ newTodos . forEach ( ( todo ) => {
51
+ const listEl = document . createElement ( "li" ) ;
52
+ listEl . textContent = todo . title ;
53
+ listEl . id = `todo-${ todo . id } ` ;
54
+
55
+ const deleteEl = document . createElement ( "span" ) ;
56
+ deleteEl . textContent = "🗑️" ;
57
+ deleteEl . onclick = ( ) => deleteTodo ( todo . id ) ;
58
+
59
+ const udpateEl = document . createElement ( "span" ) ;
60
+ udpateEl . textContent = "✏️" ;
61
+ udpateEl . onclick = ( ) => updateTodo ( todo . id , todo . title ) ;
62
+
63
+ listEl . append ( deleteEl ) ;
64
+ listEl . append ( udpateEl ) ;
65
+ todoListEl . append ( listEl ) ;
66
+ } ) ;
67
+ } ;
68
+
69
+ const addTodo = ( ) => {
70
+ const title = todoInputEl . value ;
71
+ const date = new Date ( ) ;
72
+ const createdAt = date . toDateString ( ) ;
73
+
74
+ if ( ! title ) return ;
75
+
76
+ const newTodo = {
77
+ id : date . getTime ( ) ,
78
+ title,
79
+ createdAt,
80
+ } ;
81
+
82
+ fetch ( API_URL , {
83
+ method : "POST" ,
84
+ headers : {
85
+ "Content-Type" : "application/json" ,
86
+ } ,
87
+ body : JSON . stringify ( { ...newTodo , completed : false } ) ,
88
+ } )
89
+ . then ( ( response ) => response . json ( ) )
90
+ . then ( ( ) => {
91
+ todoInputEl . value = "" ;
92
+ return fetch ( API_URL ) ;
93
+ } )
94
+ . then ( ( response ) => response . json ( ) )
95
+ . then ( ( data ) => renderTodo ( data ) ) ;
96
+ } ;
97
+
98
+ const deleteTodo = ( todoId ) => {
99
+ fetch ( API_URL + "/" + todoId , {
100
+ method : "DELETE" ,
101
+ } )
102
+ . then ( ( ) => fetch ( API_URL ) )
103
+ . then ( ( response ) => response . json ( ) )
104
+ . then ( ( data ) => renderTodo ( data ) )
105
+ . catch ( error => (
106
+ console . log ( error )
107
+ ) )
108
+ } ;
0 commit comments