Skip to content
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단계 - todo list] 웨지(성시형) 미션 제출합니다. #37

Open
wants to merge 11 commits into
base: sihyung92
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@

## 🎯 요구사항

- [ ] todo list에 todoItem을 키보드로 입력하여 추가하기
- [ ] todo list의 체크박스를 클릭하여 complete 상태로 변경. (li tag 에 completed class 추가, input 태그에 checked 속성 추가)
- [ ] todo list의 x버튼을 이용해서 해당 엘리먼트를 삭제
- [ ] todo list를 더블클릭했을 때 input 모드로 변경. (li tag 에 editing class 추가) 단 이때 수정을 완료하지 않은 상태에서 esc키를 누르면 수정되지 않은 채로 다시 view 모드로 복귀
- [ ] todo list의 item갯수를 count한 갯수를 리스트의 하단에 보여주기
- [ ] todo list의 상태값을 확인하여, 해야할 일과, 완료한 일을 클릭하면 해당 상태의 아이템만 보여주기
- [ ] localStorage에 데이터를 저장하여, TodoItem의 CRUD를 반영하기. 따라서 새로고침하여도 저장된 데이터를 확인할 수 있어야 함
- [x] todo list에 todoItem을 키보드로 입력하여 추가하기
- [x] todo list의 체크박스를 클릭하여 complete 상태로 변경 (li tag 에 completed class 추가, input 태그에 checked 속성 추가)
- [x] todo list의 x버튼을 이용해서 해당 엘리먼트를 삭제
- [x] todo list를 더블클릭했을 때 input 모드로 변경 (li tag 에 editing class 추가) 단 이때 수정을 완료하지 않은 상태에서 esc키를 누르면 수정되지 않은 채로 다시 view 모드로 복귀
- [x] todo list의 item갯수를 count한 갯수를 리스트의 하단에 보여주기
- [x] todo list의 상태값을 확인하여, 해야할 일과, 완료한 일을 클릭하면 해당 상태의 아이템만 보여주기

<br/>

Expand Down
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<html lang="kr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>이벤트 - TODOS</title>
<link rel="stylesheet" href="./src/css/style.css" />
<script type="module" src="src/js/todoList.js"></script>
</head>
<body>
<div class="todoapp">
Expand All @@ -19,16 +20,15 @@ <h1>TODOS</h1>
<input class="toggle-all" type="checkbox" />
<ul id="todo-list" class="todo-list"></ul>
<div class="count-container">
<span class="todo-count">총 <strong>0</strong> 개</span>
<ul class="filters">
<li>
<a class="all selected" href="/#">전체보기</a>
<a class="filter-item all selected" href="#">전체보기</a>
</li>
<li>
<a class="active" href="#active">해야할 일</a>
<a class="filter-item active" href="#active">해야할 일</a>
</li>
<li>
<a class="completed" href="#completed">완료한 일</a>
<a class="filter-item completed" href="#completed">완료한 일</a>
</li>
</ul>
</div>
Expand Down
47 changes: 47 additions & 0 deletions src/js/store/todoListStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export {addTodoItem, updateTodoItem, removeTodoItem, findById, toggleStateTodoItem, deepCopyStore}

const todoListStore = [];

const EMPTY_STRING = "";

function deepCopyStore() {
return JSON.parse(JSON.stringify(todoListStore));
}

function createTodoItem(id, title = EMPTY_STRING, state = "active") {
return {id: `i-${id}`, title: title, state: state}
}

function addTodoItem(id, title = EMPTY_STRING) {
todoListStore.push(createTodoItem(id, title));
}

function updateTodoItem(id, insert = EMPTY_STRING) {
findById(id).title = insert;
}

function findById(id) {
if (todoListStore.some(item => item.id === id)) {
return todoListStore.find(item => item.id === id);
} else {
throw `${id}라는 ID를 가진 요소가 없습니다!`;
}
}

function removeTodoItem(id) {
const index = todoListStore.findIndex(item => item.id === id);
if (index !== -1) {
todoListStore.splice(index, 1);
} else {
throw `${id}라는 ID를 가진 요소가 없습니다!`;
}
}

function toggleStateTodoItem(id) {
const element = findById(id);
if (element.state === "completed") {
element.state = "active";
} else {
element.state = "completed";
}
}
57 changes: 57 additions & 0 deletions src/js/store/todoListStoreAccessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {addTodoItem, removeTodoItem, deepCopyStore, toggleStateTodoItem, updateTodoItem} from './todoListStore.js';
import itemTemplate from "../template/todoItemTemplate.js";
import countTemplate from "../template/todoCountTemplate.js";

export {execute, renderTodoList}

const EMPTY_STRING = "";

function execute(command, {id, title}, state) {
switch (command) {
case "add" :
addTodoItem(id, title);
break;
case "update" :
updateTodoItem(id, title);
break;
case "delete" :
removeTodoItem(id);
break;
case "toggle" :
toggleStateTodoItem(id);
break;
default :
throw `가능한 명령 : add, update, delete, toggle / 입력된 명령: ${command}`;
}
renderTodoList(state);
}

function renderTodoList(state) {
const todoListElement = document.querySelector(".todo-list");
todoListElement.innerHTML = EMPTY_STRING;
visibleTotoList(state).forEach(
item => todoListElement.insertAdjacentHTML("beforeend", itemTemplate(item.id, item.title, item.state))
)

const countContainerElement = document.querySelector(".count-container");
if (countContainerElement.querySelector(".todo-count")) {
countContainerElement.querySelector(".todo-count").remove();
}
countContainerElement.insertAdjacentHTML("afterbegin", createCountTemplate(state));
}

function visibleTotoList(state) {
if (state && state !== "all") {
return deepCopyStore().filter(item => item.state === state);
} else {
return deepCopyStore();
}
}

function createCountTemplate(state) {
if (state === "completed") {
return countTemplate(deepCopyStore().filter(item => item.state === state).length);
} else {
return countTemplate(deepCopyStore().length);
}
}
4 changes: 4 additions & 0 deletions src/js/template/todoCountTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function (count) {
return `<span class="todo-count">총 <strong>${count}</strong> 개</span>`
}

11 changes: 11 additions & 0 deletions src/js/template/todoItemTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function itemTemplate(id, title, state) {
return ` <li id="${id}" class="todo-item ${state}">
<div class="view">
<input class="toggle" ${state === "completed" ? "checked" : ""} type="checkbox">
<label class="label">${title}</label>
<button class="destroy"></button>
</div>
<input class="edit" value="${title}">
</li>`
}

Loading