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