Skip to content

Commit 63306e7

Browse files
authored
Merge pull request #45 from Konkuk-KUIT/junghyun
[3주차] updateTodo 미션
2 parents 9288e6b + 3f5a768 commit 63306e7

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed

week3/junghyun/es6/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const arr = [-3, -2, -1, 0, 1, 2, 3];
2+
3+
const arr1 = arr.map((value, index) => {
4+
return value * 2;
5+
});
6+
7+
const arr2 = arr.filter((value) => {
8+
return value > 0;
9+
});
10+
11+
const arr3 = arr.reduce((prevValue, currValue) => {
12+
console.log(prevValue, currValue);
13+
return prevValue + currValue;
14+
});
15+
16+
console.log(arr1);
17+
console.log(arr2);
18+
console.log(arr3);

week3/junghyun/todo/app.js

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

week3/junghyun/todo/db.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "1727766228534",
5+
"title": "",
6+
"createdAt": "Tue Oct 01 2024",
7+
"completed": false
8+
},
9+
{
10+
"id": "1727766232631",
11+
"title": "",
12+
"createdAt": "Tue Oct 01 2024",
13+
"completed": false
14+
},
15+
{
16+
"id": "1727766236314",
17+
"title": "현's",
18+
"createdAt": "Tue Oct 01 2024",
19+
"completed": false
20+
},
21+
{
22+
"id": "1727766241171",
23+
"title": "todos",
24+
"createdAt": "Tue Oct 01 2024",
25+
"completed": false
26+
}
27+
]
28+
}

week3/junghyun/todo/index.css

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

week3/junghyun/todo/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
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">gjh 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)