11import { setItem } from "../utils/storage.js" ;
22import validation from "../utils/validation.js" ;
3- import { TodoItem } from "../types/todo.js" ;
3+ import { TodoList as TodoLi } from "../types/todo.js" ;
44
55export default class TodoList {
6- state : TodoItem [ ] ;
6+ state : TodoLi ;
77 private readonly $todoList = document . createElement ( "div" ) ;
88
99 constructor (
1010 private readonly $target : HTMLElement ,
11- private readonly initialState : TodoItem [ ] ,
12- private readonly updateCount : ( state : TodoItem [ ] ) => void
11+ private readonly initialState : TodoLi ,
12+ private readonly updateCount : ( state : TodoLi ) => void
1313 ) {
14- this . $target . appendChild ( this . $todoList ) ;
14+ $target . appendChild ( this . $todoList ) ;
1515
16- if ( Array . isArray ( this . initialState ) ) this . state = this . initialState ;
16+ if ( Array . isArray ( initialState ) ) this . state = initialState ;
1717 else this . state = [ ] ;
1818
1919 this . render ( ) ;
@@ -22,29 +22,29 @@ export default class TodoList {
2222 const target = e . target as HTMLLIElement ;
2323 const $li = target . closest ( "li" ) ;
2424
25- if ( $li ) {
26- const newState = [ ...this . state ] ;
27- if ( typeof $li . dataset . index !== "string" ) return ;
28- const index = + $li . dataset . index ;
25+ if ( ! $li ) return ;
2926
30- if ( target . className === "deleteBtn" ) {
31- newState . splice ( index , 1 ) ;
32- this . setState ( newState ) ;
33- } else if ( target . className . includes ( "todoList" ) ) {
34- const isCompleted = target . className . includes ( "completed" ) ;
35- if ( isCompleted ) target . classList . remove ( "completed" ) ;
36- else target . classList . add ( "completed" ) ;
37- newState [ index ] = {
38- ...newState [ index ] ,
39- isCompleted : ! isCompleted ,
40- } ;
41- this . setState ( newState ) ;
42- }
27+ const newState = [ ...this . state ] ;
28+ if ( typeof $li . dataset . index !== "string" ) return ;
29+ const index = + $li . dataset . index ;
30+
31+ if ( target . className === "deleteBtn" ) {
32+ newState . splice ( index , 1 ) ;
33+ this . setState ( newState ) ;
34+ } else if ( target . className . includes ( "todoList" ) ) {
35+ const isCompleted = target . className . includes ( "completed" ) ;
36+ if ( isCompleted ) target . classList . remove ( "completed" ) ;
37+ else target . classList . add ( "completed" ) ;
38+ newState [ index ] = {
39+ ...newState [ index ] ,
40+ isCompleted : ! isCompleted ,
41+ } ;
42+ this . setState ( newState ) ;
4343 }
4444 } ) ;
4545 }
4646
47- setState ( nextState : TodoItem [ ] ) {
47+ setState ( nextState : TodoLi ) {
4848 const newState = validation . state ( nextState ) ;
4949 this . state = newState ;
5050 setItem ( "todo" , JSON . stringify ( newState ) ) ;
0 commit comments