-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.ts
77 lines (67 loc) · 1.89 KB
/
App.ts
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
import { NewFuncParams, StateArray } from '../globalTypes';
import { storage } from '../utils/storage';
import validation from '../utils/validation';
import Header from './Header';
import TodoCount from './TodoCount';
import TodoForm from './TodoForm';
import TodoList from './TodoList';
export default function App(
this: any,
{ $target, initialState }: NewFuncParams
) {
// new 미사용 방어코드
if (!new.target) {
throw new Error('new 키워드를 사용하여야 합니다.');
}
this.state = initialState;
this.setState = (nextState: StateArray) => {
this.state = nextState;
todoCount.setState(this.state);
todoList.setState(this.state);
};
// 헤더
new Header({
$target,
text: 'Simple Todo List',
});
// Todo 입력폼
new TodoForm({
$target,
onSubmit: (text: string) => {
const nextState = [
...todoList.state,
{
text,
isCompleted: false,
},
];
// state 유효성 검사 후 업데이트
this.setState(validation(nextState));
storage.setItem('todos', JSON.stringify(nextState));
},
});
// Todo 리스트
const todoList = new TodoList({
$target,
initialState: validation(initialState),
onClick: (text: string, id: string | undefined) => {
const nextState = [...this.state];
// 삭제할 값이 있을 경우 삭제
if (text === '삭제') nextState.splice(Number(id), 1);
nextState.forEach((task, i) => {
if (task.text === text && i === Number(id)) {
// 데이터 수정
nextState[i].isCompleted = !nextState[i].isCompleted;
}
});
// state 유효성 검사 후 업데이트
this.setState(validation(nextState));
storage.setItem('todos', JSON.stringify(nextState));
},
});
// TodoCount
const todoCount = new TodoCount({
$target,
initialState: this.state,
});
}