|
| 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 | +}; |
0 commit comments