Skip to content

Commit 0a8b935

Browse files
authored
Merge pull request #46 from Konkuk-KUIT/hwanggyuun
3주차 실습 성공
2 parents 63306e7 + dc6a2ec commit 0a8b935

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

week3/gyuun/todo/app.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
};

week3/gyuun/todo/db.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"todos": [
3+
{
4+
"id": 1727783920822,
5+
"title": "ㅜㅜㅜㅜ드디어",
6+
"createdAt": "Tue Oct 01 2024",
7+
"completed": false
8+
}
9+
]
10+
}

week3/gyuun/todo/index.css

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 #ddd;
47+
display: flex;
48+
justify-content: space-between;
49+
align-items: center;
50+
}
51+
li:last-child {
52+
border-bottom: none;
53+
}
54+
.deleteBtn {
55+
cursor: pointer;
56+
color: #ff0000;
57+
}
58+
.deleteBtn:hover {
59+
text-decoration: underline;
60+
}

week3/gyuun/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)