Skip to content

Commit dd58a09

Browse files
authored
Merge pull request #48 from Konkuk-KUIT/WonUni
[3주차] updateTodo
2 parents 0a8b935 + f1d1dbf commit dd58a09

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

week3/WonUni/todo/app.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const todoListEl = document.getElementById("todoList");
2+
const todoInputEl = document.getElementById("todoInput");
3+
4+
const API_URL = "http://localhost:8080/todos";
5+
6+
fetch(API_URL) //API_URL에 get 요청을 보내 To-do 리스트 데이터를 가져옴, promise 객체를 반환. 응답이 도착하면 .then()메소드로 이어짐.
7+
.then((response) => response.json()) //서버에서 받은 응답은 response 객체로 반환되며 이 응답을 JSON형식으로 변환. json()도 비동기함수라 Promise를 반환하여 변환된 JSON데이터를 다음 then에서 사용
8+
.then((data) => renderTodo(data));// JSON형식으로 변환된 배열 데이터를 화면에 표시.
9+
10+
const updateTodo = (todoId, originalTitle) => {
11+
const todoItem = document.querySelector(`#todo-${todoId}`);
12+
13+
// mission
14+
const updateInput = document.createElement("input");
15+
updateInput.type="text";
16+
updateInput.value=originalTitle;
17+
todoItem.innerHTML="";
18+
todoItem.append(updateInput);
19+
const updatebtn = document.createElement("button");
20+
updatebtn.textContent = "✏️";
21+
todoItem.append(updatebtn);
22+
updatebtn.onclick = () => {
23+
const updateTitle = updateInput.value;
24+
if(updateTitle){
25+
fetch(API_URL + "/" + todoId, {
26+
method: "PUT", //서버에 있는 데이터를 수정
27+
headers: {
28+
"Content-Type": "application/json",
29+
},
30+
body: JSON.stringify({ title: updateTitle }),
31+
})
32+
.then(() => {
33+
updateInput.value = "";
34+
updatebtn.remove();
35+
return fetch(API_URL);
36+
})
37+
.then((response) => response.json())
38+
.then((data) => renderTodo(data));
39+
}
40+
}
41+
42+
43+
};
44+
45+
const renderTodo = (newTodos) => {
46+
todoListEl.innerHTML = "";
47+
newTodos.forEach((todo) => {
48+
const listEl = document.createElement("li");
49+
listEl.textContent = todo.title;
50+
listEl.id = `todo-${todo.id}`;
51+
52+
const deleteEl = document.createElement("button");
53+
deleteEl.textContent = "🗑️";
54+
deleteEl.onclick = () => deleteTodo(todo.id);
55+
56+
const udpateEl = document.createElement("button");
57+
udpateEl.textContent = "✏️";
58+
udpateEl.onclick = () => updateTodo(todo.id, todo.title);
59+
60+
listEl.append(deleteEl);
61+
listEl.append(udpateEl);
62+
todoListEl.append(listEl);
63+
});
64+
};
65+
66+
const addTodo = () => {
67+
const title = todoInputEl.value;
68+
const date = new Date();
69+
const createdAt = date.toDateString();
70+
71+
if (!title) return;
72+
73+
const newTodo = {
74+
id: date.getTime().toString(), //id가 string만 지원됨
75+
title,
76+
createdAt,
77+
};
78+
79+
fetch(API_URL, {
80+
method: "POST", //API_URL에 새로운 todo데이터 전송
81+
headers: { //요청 본문이 JSON형식임을 서버에 알림
82+
"Content-Type": "application/json",
83+
},
84+
body: JSON.stringify({ ...newTodo, completed: false }), //JSON.stringify()는 자바스크립트 객체나 배열을 JSON 문자열로 변환하는 함수, ...newTodo는 newTodo 객체의 모든 프로퍼티를 새 객체에 복사하는 역할,새 객체에 completed: false라는 새로운 프로퍼티가 추가
85+
})
86+
.then((response) => response.json())
87+
.then(() => {
88+
todoInputEl.value = "";
89+
return fetch(API_URL);
90+
})
91+
.then((response) => response.json())
92+
.then((data) => renderTodo(data));
93+
};
94+
95+
const deleteTodo = (todoId) => {
96+
fetch(API_URL + "/" + todoId, {
97+
method: "DELETE",
98+
})
99+
.then(() => fetch(API_URL))
100+
.then((response) => response.json())
101+
.then((data) => renderTodo(data));
102+
};

week3/WonUni/todo/db.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "1727948636190",
5+
"title": "KUIT 실습",
6+
"createdAt": "Thu Oct 03 2024",
7+
"completed": false
8+
},
9+
{
10+
"id": "1727948650828",
11+
"title": "KUIT 워크북",
12+
"createdAt": "Thu Oct 03 2024",
13+
"completed": false
14+
},
15+
{
16+
"id": "1727948662655",
17+
"title": "KUIT 스터디",
18+
"createdAt": "Thu Oct 03 2024",
19+
"completed": false
20+
}
21+
]
22+
}

week3/WonUni/todo/index.css

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
* {
2+
box-sizing: border-box;
3+
margin: 0;
4+
padding: 0;
5+
}
6+
body {
7+
background-color: #f5f5f5;
8+
font-family: "Arial", sans-serif;
9+
}
10+
.app {
11+
width: 400px;
12+
margin: 30px auto;
13+
background-color: #fff;
14+
padding: 20px;
15+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
16+
}
17+
.header {
18+
font-size: 1.5em;
19+
margin-bottom: 20px;
20+
text-align: center;
21+
}
22+
.inputContainer {
23+
display: flex;
24+
}
25+
#todoInput {
26+
flex: 1;
27+
padding: 10px;
28+
font-size: 1em;
29+
border: 1px solid #ddd;
30+
}
31+
button {
32+
background-color: #28a745;
33+
color: #fff;
34+
padding: 10px;
35+
font-size: 1em;
36+
cursor: pointer;
37+
}
38+
button:hover {
39+
background-color: #218838;
40+
}
41+
ul {
42+
list-style-type: none;
43+
}
44+
li {
45+
padding: 10px;
46+
border-bottom: 1px solid #000000;
47+
display: flex;
48+
justify-content: space-between;
49+
align-items: center;
50+
background-color: #ddd;
51+
}
52+
li:last-child {
53+
border-bottom: none;
54+
}
55+
.deleteBtn {
56+
cursor: pointer;
57+
color: #ff0000;
58+
}
59+
.deleteBtn:hover {
60+
text-decoration: underline;
61+
}
62+
63+
li button{
64+
background-color: #e7eee8;
65+
color: #fff;
66+
padding: 10px;
67+
font-size: 1em;
68+
cursor: pointer;
69+
}

week3/WonUni/todo/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Todo App</title>
6+
<link rel="stylesheet" href="index.css" />
7+
</head>
8+
<body>
9+
<div class="app">
10+
<div class="header">My Todos</div>
11+
<div class="inputContainer">
12+
<input id="todoInput" placeholder="What needs to be done?" />
13+
<button onclick="addTodo()">Add</button>
14+
</div>
15+
<ul id="todoList"></ul>
16+
</div>
17+
<script src="app.js"></script>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)