-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[1주차] 최지원 미션 제출합니다. #3
base: master
Are you sure you want to change the base?
Changes from all commits
2174809
3556734
1434121
8ec025d
e3313a3
a254567
3152ab0
4de67aa
8f7e29b
c08b6fa
86260ea
3d0989b
ca4e76e
3d044fe
28d46e6
970222b
cd12e36
780ce6d
a8f23ca
eb9f93e
9ef7e0e
f6d8336
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,224 @@ | ||||||||||||||||||||||||||||||
//😍CEOS 20기 프론트엔드 파이팅😍 | ||||||||||||||||||||||||||||||
/* 날짜 및 현재 시각 */ | ||||||||||||||||||||||||||||||
const updateTime = () => { | ||||||||||||||||||||||||||||||
const today = document.querySelector(".today"); | ||||||||||||||||||||||||||||||
const now = new Date(); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const options = { | ||||||||||||||||||||||||||||||
weekday: "long", | ||||||||||||||||||||||||||||||
year: "numeric", | ||||||||||||||||||||||||||||||
month: "long", | ||||||||||||||||||||||||||||||
day: "numeric", | ||||||||||||||||||||||||||||||
hour: "2-digit", | ||||||||||||||||||||||||||||||
minute: "2-digit", | ||||||||||||||||||||||||||||||
second: "2-digit", | ||||||||||||||||||||||||||||||
hour12: false, | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 요일, 날짜 및 시각 포맷 적용 | ||||||||||||||||||||||||||||||
today.innerHTML = now.toLocaleString("ko-KR", options); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
updateTime(); | ||||||||||||||||||||||||||||||
setInterval(updateTime, 1000); // 1초마다 호출 | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 호출 시 즉시 실행시키면 첫 1초를 기다리지 않아서 이런식으로 구성해도 좋을 것 같습니다.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단순 로딩 문제라 생각했는데 앞으로는 쓰려는 함수의 작동을 정확히 이해하고 써야할 것 같습니다😂 감사합니다! |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/* localStorage 데이터 처리 */ | ||||||||||||||||||||||||||||||
// localStorage에 항목 저장하기 함수 | ||||||||||||||||||||||||||||||
const saveToLocalStorage = (key, data) => { | ||||||||||||||||||||||||||||||
localStorage.setItem(key, JSON.stringify(data)); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// localStorage의 항목 불러오기 함수 | ||||||||||||||||||||||||||||||
const loadFromLocalStorage = (key) => { | ||||||||||||||||||||||||||||||
const data = localStorage.getItem(key); | ||||||||||||||||||||||||||||||
return data ? JSON.parse(data) : []; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
let todos = loadFromLocalStorage("todos"); // 할 일 배열 | ||||||||||||||||||||||||||||||
let dones = loadFromLocalStorage("dones"); // 한 일 배열 | ||||||||||||||||||||||||||||||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todos와 dones는 배열을 변경할 필요가 없으므로, const를 사용해도 좋을 것같습니다! let은 값이 재할당되거나 변동될 가능성이 있는 경우에만 사용하는 것이 좋다고 합니다. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 항목을 삭제하거나 옮기는 사실 코드 쓸 때는 배열이 계속해서 변동된다는 생각에 곧바로 let을 사용하고선 넘어갔는데, 주신 리뷰보고서 한 번 더 디버깅 해보면서 정확히 어디까진 동작하고 어느 지점부터 재할당으로 인해 반영이 안되는지 파악해볼 수 있었습니다.
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로컬스토리지에 key값을 두 개로 분리하여 저장하는 방법을 배워갑니다! 호기심에 다른 방법은 없을까 찾아보다 |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// localStorage의 항목으로 초기화하기 함수 | ||||||||||||||||||||||||||||||
const initTodoList = () => { | ||||||||||||||||||||||||||||||
todos.forEach((todo) => printItem(todo.text, "todo", todo.id)); | ||||||||||||||||||||||||||||||
dones.forEach((done) => printItem(done.text, "done", done.id)); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 할 일과 한 일 개수 및 성취도 업데이트 함수 | ||||||||||||||||||||||||||||||
const updateCounts = () => { | ||||||||||||||||||||||||||||||
const totalCount = todos.length + dones.length; | ||||||||||||||||||||||||||||||
const doneCount = dones.length; | ||||||||||||||||||||||||||||||
const countElement = document.querySelector(".count"); | ||||||||||||||||||||||||||||||
const accomplishmentElement = document.querySelector(".accomplishment"); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
countElement.innerText = totalCount; | ||||||||||||||||||||||||||||||
accomplishmentElement.innerText = | ||||||||||||||||||||||||||||||
totalCount > 0 ? `${doneCount}/${totalCount}` : "0/0"; | ||||||||||||||||||||||||||||||
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 삼항조건문으로 count 처리를 하는 게 인상깊습니다! 저도 최근에 알게 된 사실인데 innerText와 textContent로 text를 넣는 차이가 있다는 것을 알았습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. innerHTML과 innerText만 알고 있었는데 말씀해주신 각 개념과 차이점까지 분명하게 정리하고 넘어갑니다👍 감사합니다! |
||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 이벤트 위임으로 버튼 클릭 처리를 위한 리스너 추가 | ||||||||||||||||||||||||||||||
document.querySelector(".todoList").addEventListener("click", (e) => { | ||||||||||||||||||||||||||||||
if (e.target.closest(".todo-check")) { | ||||||||||||||||||||||||||||||
completeTodo(e); | ||||||||||||||||||||||||||||||
} else if (e.target.closest(".todo-del")) { | ||||||||||||||||||||||||||||||
deleteTodoItem(e); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
document.querySelector(".doneList").addEventListener("click", (e) => { | ||||||||||||||||||||||||||||||
if (e.target.closest(".todo-check")) { | ||||||||||||||||||||||||||||||
restoreTodo(e); | ||||||||||||||||||||||||||||||
} else if (e.target.closest(".todo-del")) { | ||||||||||||||||||||||||||||||
deleteDoneItem(e); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 버튼 생성하기 함수 | ||||||||||||||||||||||||||||||
const createBtn = (src, className) => { | ||||||||||||||||||||||||||||||
const btn = document.createElement("button"); | ||||||||||||||||||||||||||||||
const img = document.createElement("img"); | ||||||||||||||||||||||||||||||
img.setAttribute("src", src); | ||||||||||||||||||||||||||||||
btn.appendChild(img); | ||||||||||||||||||||||||||||||
btn.setAttribute("class", className); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return btn; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 항목 출력하기 함수 | ||||||||||||||||||||||||||||||
const printItem = (text, type, id) => { | ||||||||||||||||||||||||||||||
const list = document.querySelector(`.${type}List`); | ||||||||||||||||||||||||||||||
const item = document.createElement("li"); | ||||||||||||||||||||||||||||||
const itemContent = document.createElement("div"); | ||||||||||||||||||||||||||||||
const itemText = document.createElement("span"); | ||||||||||||||||||||||||||||||
itemText.innerText = text; | ||||||||||||||||||||||||||||||
itemText.className = `${type}-text`; | ||||||||||||||||||||||||||||||
item.setAttribute("data-id", id); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 체크 버튼 생성하기 | ||||||||||||||||||||||||||||||
const checkBtn = createBtn( | ||||||||||||||||||||||||||||||
type === "todo" ? "images/empty_checkbox.svg" : "images/full_checkbox.svg", | ||||||||||||||||||||||||||||||
"todo-check" | ||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 삭제 버튼 생성하기 | ||||||||||||||||||||||||||||||
const deleteBtn = createBtn("images/delete_btn.svg", "todo-del"); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 항목 구성하기 | ||||||||||||||||||||||||||||||
itemContent.className = "todo-item"; | ||||||||||||||||||||||||||||||
itemContent.appendChild(checkBtn); | ||||||||||||||||||||||||||||||
itemContent.appendChild(itemText); | ||||||||||||||||||||||||||||||
itemContent.appendChild(deleteBtn); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
item.appendChild(itemContent); | ||||||||||||||||||||||||||||||
list.appendChild(item); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 할 일 추가하기 함수 | ||||||||||||||||||||||||||||||
const addTodoItem = (event) => { | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ![]() 항목의 내용이 동일한 경우 하나만 삭제해도 그 동일 항목은 모두 삭제되는 버그가 있어요! 필터링 기준이 텍스트로만 의존되어서 같은 텍스트를 가진 모든 항목이 배열에서 제거되는 것 같아요. 이러한 리스트나 배열 부분을 추가하거나 삭제할 경우 항목을 구분할 수 있는 고유한 식별자(예: id 또는 timestamp)를 추가하여 각 항목을 고유하게 식별하는 것을 추천드려요!
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 미처 확인해보지 못한 케이스인데, id로 배열의 요소를 관리하는 것은 어떻게 보면 기본이었다는 생각이 들어 반성합니다ㅜ.ㅜ (pr에도 쓴 아쉬운 점의 현상이 아무래도 동일 항목을 입력했을 때였을 거라고 예상됩니다.) 알려주신 부분 반영해보겠습니다! 감사합니다. |
||||||||||||||||||||||||||||||
event.preventDefault(); | ||||||||||||||||||||||||||||||
const inputElement = document.querySelector(".input"); | ||||||||||||||||||||||||||||||
const todoInput = inputElement.value.trim(); // 입력값 공백 확인 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (todoInput) { | ||||||||||||||||||||||||||||||
const todoItem = { id: Date.now().toString(), text: todoInput }; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
todos.push(todoItem); | ||||||||||||||||||||||||||||||
saveToLocalStorage("todos", todos); // 업데이트된 todos 배열을 localStorage에 저장 | ||||||||||||||||||||||||||||||
printItem(todoInput, "todo", todoItem.id); | ||||||||||||||||||||||||||||||
document.querySelector(".input").value = ""; // 입력창 초기화 | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 항목 삭제하기 함수 | ||||||||||||||||||||||||||||||
const deleteItem = (e, array, key, listSelector) => { | ||||||||||||||||||||||||||||||
const target = e.target.closest("li"); | ||||||||||||||||||||||||||||||
const itemId = target.getAttribute("data-id"); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
array = array.filter((item) => item.id !== itemId); | ||||||||||||||||||||||||||||||
saveToLocalStorage(key, array); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
document.querySelector(listSelector).removeChild(target); | ||||||||||||||||||||||||||||||
return array; | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const deleteTodoItem = (e) => { | ||||||||||||||||||||||||||||||
todos = deleteItem(e, todos, "todos", ".todoList"); | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updateCounts가 deleteItem 함수 내부에서 이미 호출되고 있으므로, todoToDid 및 didToTodo 함수에서 중복 호출되는 것 같아요. 이러한 중복 호출은 제거해도 괜찮을 것 같습니다 :) |
||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const deleteDoneItem = (e) => { | ||||||||||||||||||||||||||||||
dones = deleteItem(e, dones, "dones", ".doneList"); | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 할 일에서 한 일로 이동 함수 | ||||||||||||||||||||||||||||||
const completeTodo = (e) => { | ||||||||||||||||||||||||||||||
const target = e.target.closest("li"); | ||||||||||||||||||||||||||||||
const todoItem = { | ||||||||||||||||||||||||||||||
id: target.getAttribute("data-id"), | ||||||||||||||||||||||||||||||
text: target.innerText, | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
// const todoText = target.querySelector(".todo-text").innerText; | ||||||||||||||||||||||||||||||
todos = deleteItem(e, todos, "todos", ".todoList"); | ||||||||||||||||||||||||||||||
dones.push(todoItem); | ||||||||||||||||||||||||||||||
saveToLocalStorage("dones", dones); // 한 일 저장 | ||||||||||||||||||||||||||||||
printItem(todoItem.text, "done", todoItem.id); | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 한 일에서 할 일로 이동 함수 | ||||||||||||||||||||||||||||||
const restoreTodo = (e) => { | ||||||||||||||||||||||||||||||
const target = e.target.closest("li"); | ||||||||||||||||||||||||||||||
const doneItem = { | ||||||||||||||||||||||||||||||
id: target.getAttribute("data-id"), | ||||||||||||||||||||||||||||||
text: target.innerText, | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
//const doneText = target.querySelector(".done-text").innerText; | ||||||||||||||||||||||||||||||
dones = deleteItem(e, dones, "dones", ".doneList"); | ||||||||||||||||||||||||||||||
todos.push(doneItem); | ||||||||||||||||||||||||||||||
saveToLocalStorage("todos", todos); // 할 일 저장 | ||||||||||||||||||||||||||||||
printItem(doneItem.text, "todo", doneItem.id); | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/* todo 입력, 체크, 삭제 */ | ||||||||||||||||||||||||||||||
const form = document.querySelector(".input-box"); // 입력창 폼 요소 | ||||||||||||||||||||||||||||||
const showMessage = document.querySelector(".show-input"); // 입력창 열고 닫는 버튼 요소 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 입력창 토글 함수 | ||||||||||||||||||||||||||||||
let isFormOpen = false; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const toggleForm = () => { | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런식으로 스타일을 통해서 관리하는 것도 좋지만 입력창의 상태(열림/닫힘)를 명확하게 추적하기 위해 boolean 변수로 상태를 관리하여 여닫는것도 유지보수하기엔 더 편하니까 한번 사용해봐도 좋을 것 같아요 :)_ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클래스명은 워낙 지정하기 나름이라 혼동할 수도 있고, html/css에서 변경될 가능성도 있어 확실히 클래스 변경에 의존하는 것보다는 말씀해주신 것이 훨씬 직관적이고 혼란을 줄일 수 있겠다는 생각이 듭니다! 좋은 의견 주셔서 감사합니다 |
||||||||||||||||||||||||||||||
if (isFormOpen) { | ||||||||||||||||||||||||||||||
form.classList.remove("show"); | ||||||||||||||||||||||||||||||
form.classList.add("hide"); | ||||||||||||||||||||||||||||||
isFormOpen = false; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
showMessage.innerHTML = "입력창 불러오기"; | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
form.style.display = "flex"; | ||||||||||||||||||||||||||||||
form.classList.remove("hide"); | ||||||||||||||||||||||||||||||
form.classList.add("show"); | ||||||||||||||||||||||||||||||
isFormOpen = true; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
showMessage.innerHTML = "입력창 다시닫기"; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 애니메이션 종료 처리하는 이벤트리스너 | ||||||||||||||||||||||||||||||
const handleAnimationEnd = (e) => { | ||||||||||||||||||||||||||||||
if (form.classList.contains("hide")) { | ||||||||||||||||||||||||||||||
form.style.display = "none"; | ||||||||||||||||||||||||||||||
form.classList.remove("hide"); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 이벤트 리스너 등록 및 기존 데이터 불러오기 함수 | ||||||||||||||||||||||||||||||
const init = () => { | ||||||||||||||||||||||||||||||
initTodoList(); | ||||||||||||||||||||||||||||||
updateCounts(); | ||||||||||||||||||||||||||||||
form.addEventListener("submit", addTodoItem); | ||||||||||||||||||||||||||||||
form.addEventListener("animationend", handleAnimationEnd); | ||||||||||||||||||||||||||||||
showMessage.addEventListener("click", toggleForm); | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
init(); | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. window.onload / document.addEventListener("DOMContentLoaded" / 이벤트 리스너 등록없이 함수 실행 이라는 렌더링 방식에 있어 각각의 사용목적과 장단점이 있음을 이번에 알게 되었습니다. 링크 한번 달겠습니다! 한번 참고하셔도 좋을 것 같습니다. (링크를 줄이는 방법을 몰라 링크가 긴 점 양해부탁드립니다..) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
footer main 등등 시맨틱 태그들을 적절히 잘 활용해주셨네요!! 문서의 구조를 보다 더 명확하게 파악할 수 있는 것 같습니다 ㅎㅎ