|
| 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 | + .catch((error) => console.log("error: " + error)); |
| 10 | + |
| 11 | +const renderTodo = (newTodos) => { |
| 12 | + todoListEl.innerHTML = ""; |
| 13 | + newTodos.forEach((todo) => { |
| 14 | + const listEl = document.createElement("li"); |
| 15 | + listEl.textContent = todo.title; |
| 16 | + listEl.id = `todo-${todo.id}`; |
| 17 | + |
| 18 | + const deleteEl = document.createElement("span"); |
| 19 | + deleteEl.textContent = "🗑️"; |
| 20 | + deleteEl.onclick = () => deleteTodo(todo.id); |
| 21 | + |
| 22 | + const udpateEl = document.createElement("span"); |
| 23 | + udpateEl.textContent = "✏️"; |
| 24 | + udpateEl.onclick = () => updateTodo(todo.id, todo.title); |
| 25 | + |
| 26 | + listEl.append(deleteEl); |
| 27 | + listEl.append(udpateEl); |
| 28 | + todoListEl.append(listEl); |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +const addTodo = () => { |
| 33 | + const title = todoInputEl.value; |
| 34 | + const date = new Date(); |
| 35 | + const createdAt = date.toDateString(); |
| 36 | + |
| 37 | + if (!title) return; |
| 38 | + |
| 39 | + const newTodo = { |
| 40 | + title, |
| 41 | + createdAt, |
| 42 | + completed: false, |
| 43 | + }; |
| 44 | + |
| 45 | + fetch(API_URL, { |
| 46 | + method: "POST", |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json", |
| 49 | + }, |
| 50 | + body: JSON.stringify({ ...newTodo, completed: false }), |
| 51 | + }) |
| 52 | + .then((response) => response.json()) |
| 53 | + .then(() => { |
| 54 | + todoInputEl.value = ""; |
| 55 | + return fetch(API_URL); |
| 56 | + }) |
| 57 | + .then((response) => response.json()) |
| 58 | + .then((data) => renderTodo(data)); |
| 59 | +}; |
| 60 | +const deleteTodo = (todoId) => { |
| 61 | + console.log(todoId); |
| 62 | + fetch(API_URL + "/" + todoId, { |
| 63 | + method: "DELETE", |
| 64 | + }) |
| 65 | + .then(() => fetch(API_URL)) |
| 66 | + .then((response) => response.json()) |
| 67 | + .then((data) => renderTodo(data)); |
| 68 | +}; |
| 69 | +//연필 이모지 누르면 input으로 바뀌고 enter 누르면 바뀜 |
| 70 | +const updateTodo = (todoId, originalTitle) => { |
| 71 | + const todoItem = document.querySelector(`#todo-${todoId}`); |
| 72 | + |
| 73 | + const updateInput = document.createElement("input"); |
| 74 | + updateInput.type = "text"; |
| 75 | + updateInput.value = originalTitle; |
| 76 | + todoItem.innerHTML = ""; |
| 77 | + |
| 78 | + todoItem.append(updateInput); |
| 79 | + updateInput.onkeydown = () => { |
| 80 | + console.log("inner"); |
| 81 | + if (event.key == "Enter") { |
| 82 | + fetch(API_URL + "/" + todoId, { |
| 83 | + method: "PUT", |
| 84 | + headers: { |
| 85 | + "Content-Type": "application/json", |
| 86 | + }, |
| 87 | + body: JSON.stringify({ |
| 88 | + title: updateInput.value, |
| 89 | + }), |
| 90 | + }) |
| 91 | + .then(() => { |
| 92 | + todoItem.innerHTML = ""; |
| 93 | + return fetch(API_URL); |
| 94 | + }) |
| 95 | + .then((response) => response.json()) |
| 96 | + .then((data) => renderTodo(data)); |
| 97 | + } |
| 98 | + }; |
| 99 | +}; |
0 commit comments