-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (136 loc) · 4.66 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(function () {
'use strict';
const API_URL = 'http://localhost:3000/todos';
const $todos = document.querySelector('.todos');
const $todoForm = document.querySelector('.todo_form');
const $todoInput = document.querySelector('.todo_input');
// TODO DOM 생성
const createTodos = (todo) => {
const { id, content, completed } = todo;
const $item = document.createElement('div');
$item.dataset.id = id;
$item.className = 'item';
$item.innerHTML = `
<div class="content">
<input type="checkbox" class="todo_checkbox"
${completed ? 'checked' : ''} />
<label>${content}</label>
<input type="text" value="${content}" />
</div>
<div class="item_buttons content_buttons">
<button class="todo_edit_button"><i class="far fa-edit"></i></button>
<button class="todo_remove_button"><i class="far fa-trash-alt"></i></button>
</div>
<div class="item_buttons edit_buttons">
<button class="todo_edit_confirm_button"><i class="fas fa-check"></i></button>
<button class="todo_edit_cancel_button"><i class="fas fa-times"></i></button>
</div>
`;
return $item;
};
//만들어진 TODO DOM을 HTML문서에 SHOW
const showTodos = (todos) => {
$todos.innerHTML = '';
todos.forEach((todo) => {
const todosList = createTodos(todo);
$todos.appendChild(todosList);
});
};
//DB 불러오기
const getTodos = async () => {
try {
const response = await fetch(API_URL);
const data = await response.json();
const sortLatest = data.reverse();
showTodos(sortLatest);
} catch (error) {
console.log(error);
}
};
//입력 받은 데이터 추가
const onAddTodo = (e) => {
e.preventDefault();
const content = $todoInput.value;
if (!content) return;
const todo = {
content,
completed: false,
};
fetch(API_URL, {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(todo),
}).catch((error) => console.error(error.message));
};
// CHECKBOX가 선택되었을경우 TRUE 아닌 경우 FALSE로 변경
const onCheckEvent = (e) => {
if (e.target.className !== 'todo_checkbox') return;
const item = e.target.closest('.item');
const id = item.dataset.id;
const completed = e.target.checked;
fetch(`${API_URL}/${id}`, {
method: 'PATCH',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ completed }),
}).catch((error) => console.error(error.message));
};
//수정하기 버튼 눌렀을때 이벤트
const changeEditMode = (e) => {
const item = e.target.closest('.item');
const [content, contentButtons, editButtons] = item.children;
const [_, label, input] = content.children;
const text = input.value;
//수정 버튼을 눌렀을때
if (e.target.className === 'todo_edit_button') {
contentButtons.style.display = 'none';
editButtons.style.display = 'block';
label.style.display = 'none';
input.style.display = 'block';
input.value = '';
input.value = text;
input.focus();
}
//캔슬 버튼을 눌렀을때
if (e.target.className === 'todo_edit_cancel_button') {
contentButtons.style.display = 'block';
editButtons.style.display = 'none';
label.style.display = 'block';
input.style.display = 'none';
input.value = label.innerText;
}
return;
};
//수정 컨펌 버튼을 눌렀을때
const editTodos = (e) => {
if (e.target.className !== 'todo_edit_confirm_button') return;
const item = e.target.closest('.item');
const id = item.dataset.id;
const editInput = item.querySelector('input[type="text"]');
const content = editInput.value;
fetch(`${API_URL}/${id}`, {
method: 'PATCH',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ content }),
}).catch((error) => console.error(error.message));
};
//삭제버튼을 눌렀을 때
const deleteTodos = (e) => {
if (e.target.className !== 'todo_remove_button') return;
const item = e.target.closest('.item');
const id = item.dataset.id;
fetch(`${API_URL}/${id}`, { method: 'DELETE' }) //
.catch((error) => console.error(error.message));
};
const init = () => {
window.addEventListener('DOMContentLoaded', () => {
getTodos();
pagination();
});
$todoForm.addEventListener('submit', onAddTodo);
$todos.addEventListener('click', onCheckEvent);
$todos.addEventListener('click', changeEditMode);
$todos.addEventListener('click', editTodos);
$todos.addEventListener('click', deleteTodos);
};
init();
})();