-
Notifications
You must be signed in to change notification settings - Fork 213
[10기 김형남] TodoList with CRUD #215
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
Open
hyoungnam
wants to merge
10
commits into
next-step:hyoungnam
Choose a base branch
from
hyoungnam:main
base: hyoungnam
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
08c6542
feat: :sparkles: 요구사항1 - todo list에 todoItem을 키보드로 입력하여 추가하기
hyoungnam 5e6d51e
feat: :sparkles: 요구사항2 - todo list의 체크박스를 클릭하여 complete 상태로 변경
hyoungnam 7b548bb
refactor: :recycle: todo의 중복로직 리팩토링
hyoungnam 45e3e62
feat: :sparkles: 요구사항4 - todo list의 x버튼을 이용해서 해당 엘리먼트를 삭제
hyoungnam 48331a5
feat: :sparkles: 요구사항5 - todo list의 item갯수를 count한 갯수를 리스트의 하단에 보여주기
hyoungnam 43ff3d1
feat: :sparkles: 요구사항6 - todo list의 상태값을 확인하여 view 바꾸기
hyoungnam a7b0419
refactor: :recycle: TodoList 분리
hyoungnam 07280d5
refactor: :recycle: deleteTodoItem 함수 분리
hyoungnam e5887c5
feat: :sparkles: 심화 요구사항 - localStorage에 데이터를 저장
hyoungnam e6c0e5c
docs: :memo: README 수정
hyoungnam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,6 @@ <h1>TODOS</h1> | |
</div> | ||
</main> | ||
</div> | ||
<script src="./src/index.js" type="module"></script> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import TodoInput from "./components/TodoInput.js"; | ||
import TodoList from "./components/TodoList.js"; | ||
import { $ } from "./utils/selectors.js"; | ||
|
||
export default function App(store) { | ||
store.addObserver(new TodoInput(store, $(".new-todo"))); | ||
store.addObserver(new TodoList(store, $(".todo-list"))); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { $ } from "../utils/selectors.js"; | ||
|
||
const CIPHER = 1000; | ||
|
||
export default class TodoInput { | ||
constructor(store, $app) { | ||
this.store = store; | ||
this.$app = $app; | ||
this.mount(); | ||
} | ||
mount() { | ||
this.$app.addEventListener("keypress", this.handleInputValue.bind(this)); | ||
} | ||
render() {} | ||
handleInputValue(e) { | ||
if (e.key === "Enter") { | ||
const prevState = this.store.getState(); | ||
const newTodo = { | ||
id: Math.floor(Math.random() * CIPHER), | ||
content: e.target.value, | ||
status: "false", | ||
}; | ||
const newState = { ...prevState, todos: [...prevState.todos, newTodo] }; | ||
this.store.setState(newState); | ||
e.target.value = ""; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { buildNewState } from "../utils/helpers.js"; | ||
import { $ } from "../utils/selectors.js"; | ||
|
||
export default class TodoList { | ||
constructor(store, $app) { | ||
this.store = store; | ||
this.$app = $app; | ||
this.mount(); | ||
} | ||
mount() { | ||
this.$app.addEventListener("click", (e) => { | ||
const isToggle = e.target.classList.contains("toggle"); | ||
const isDestroy = e.target.classList.contains("destroy"); | ||
if (isToggle) { | ||
const newState = buildNewState("TOGGLE", this.store, e); | ||
this.store.setState(newState); | ||
} | ||
if (isDestroy) { | ||
const newState = buildNewState("DELETE", this.store, e); | ||
this.store.setState(newState); | ||
} | ||
}); | ||
this.$app.addEventListener("dblclick", (e) => { | ||
const isList = e.target.closest("li"); | ||
if (isList) { | ||
isList.classList.add("editing"); | ||
} | ||
}); | ||
this.$app.addEventListener("keydown", (e) => { | ||
const isEditing = e.target.classList.contains("edit"); | ||
|
||
if (isEditing && e.key === "Enter") { | ||
const newState = buildNewState("EDIT", this.store, e); | ||
this.store.setState(newState); | ||
e.target.closest("li").classList.remove("editing"); | ||
} | ||
if (isEditing && e.key === "Escape") { | ||
const currentValue = $(".label").textContent; | ||
e.target.value = currentValue; | ||
e.target.closest("li").classList.remove("editing"); | ||
} | ||
}); | ||
} | ||
render() { | ||
const newState = this.store.getState(); | ||
this.$app.innerHTML = newState.todos | ||
.map(({ id, content, status, edit }) => { | ||
const isChecked = status === "completed" ? "checked" : "false"; | ||
return `<li dataset-id=${id} class="${status} ${edit}"> | ||
<div class="view"> | ||
<input class="toggle" type="checkbox" ${isChecked}> | ||
<label class="label">${content}</label> | ||
<button class="destroy" ></button> | ||
</div> | ||
<input class="edit" value="${content}"> | ||
</li>`; | ||
}) | ||
.join(""); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import App from "./App.js"; | ||
import Store from "./store/index.js"; | ||
new App(new Store()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export default function Store() { | ||
//State | ||
this.state = { | ||
todos: [], | ||
status: "all", | ||
}; | ||
//Observer | ||
this.observers = []; | ||
this.addObserver = (observer) => this.observers.push(observer); | ||
this.observing = () => | ||
this.observers.forEach((observer) => observer.render()); | ||
|
||
//GET | ||
this.getState = () => { | ||
return this.state; | ||
}; | ||
//SET | ||
this.setState = (newState) => { | ||
this.state = { ...this.state, ...newState }; | ||
this.observing(); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//NEWSTATE | ||
export function buildNewState(op, store, e) { | ||
const OPERATIONS = { | ||
TOGGLE: toggleTodoStatus, | ||
DELETE: deleteTodo, | ||
EDIT: editTodo, | ||
}; | ||
const prevState = store.getState(); | ||
const targetId = Number(e.target.closest("li").getAttribute("dataset-id")); | ||
|
||
const newTodos = OPERATIONS[op](prevState, targetId, e); | ||
|
||
const newState = { ...prevState, todos: newTodos }; | ||
return newState; | ||
} | ||
|
||
//NEWTODOS | ||
function toggleTodoStatus(prevState, targetId, e) { | ||
const newStatus = e.target.checked ? "completed" : "false"; | ||
const newTodos = prevState.todos.map((todo) => { | ||
if (todo.id === targetId) { | ||
return { ...todo, status: newStatus }; | ||
} | ||
return todo; | ||
}); | ||
return newTodos; | ||
} | ||
|
||
function deleteTodo(prevState, targetId) { | ||
const newTodos = prevState.todos.filter((todo) => { | ||
return todo.id !== targetId; | ||
}); | ||
return newTodos; | ||
} | ||
|
||
function editTodo(prevState, targetId, e) { | ||
const newTodos = prevState.todos.map((todo) => { | ||
if (todo.id === targetId) { | ||
return { ...todo, content: e.target.value }; | ||
} | ||
return todo; | ||
}); | ||
return newTodos; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const $ = (node) => document.querySelector(node); | ||
export const $all = (node) => document.querySelectorAll(node) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이벤트 콜백 함수를 따로 만들어서 관리하는 방법도 좋지 않을 까라는 생각을 해보았습니다.ㅎㅎ