|
| 1 | +import * as React from 'react'; |
| 2 | +import { useEffect, useMemo } from 'react'; |
| 3 | +import store from '../store/store'; |
| 4 | +import { Todo } from './model/state/initialTodoListState'; |
| 5 | +import removeTodo from './model/actions/removeTodo'; |
| 6 | +import toggleShouldShowOnlyDoneTodos from './model/actions/toggleShouldShowOnlyDoneTodos'; |
| 7 | +import fetchTodos from './model/actions/fetchTodos'; |
| 8 | +import todoListController from './controller/todoListController'; |
| 9 | +import toggleIsDoneTodo from './model/actions/toggleIsDoneTodo'; |
| 10 | + |
| 11 | +const TodoListView = () => { |
| 12 | + const { todosState } = store.getState(); |
| 13 | + store.useState([todosState]); |
| 14 | + const { shownTodos } = store.getSelectors(); |
| 15 | + store.useSelectors([shownTodos]); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + // noinspection JSIgnoredPromiseFromCall |
| 19 | + fetchTodos(); |
| 20 | + document.addEventListener('keypress', todoListController.handleKeyPress); |
| 21 | + return () => document.removeEventListener('keypress', todoListController.handleKeyPress); |
| 22 | + }, []); |
| 23 | + |
| 24 | + const todoItems = useMemo( |
| 25 | + () => |
| 26 | + shownTodos.value.map((todo: Todo, index: number) => ( |
| 27 | + <li key={index}> |
| 28 | + <input |
| 29 | + id={todo.name} |
| 30 | + type="checkbox" |
| 31 | + defaultChecked={todo.isDone} |
| 32 | + onChange={() => toggleIsDoneTodo(todo)} |
| 33 | + /> |
| 34 | + <label>{todo.name}</label> |
| 35 | + <button onClick={() => removeTodo(todo)}>Remove</button> |
| 36 | + </li> |
| 37 | + )), |
| 38 | + [shownTodos.value] |
| 39 | + ); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <input |
| 44 | + id="shouldShowOnlyDoneTodos" |
| 45 | + type="checkbox" |
| 46 | + defaultChecked={todosState.shouldShowOnlyDoneTodos} |
| 47 | + onChange={toggleShouldShowOnlyDoneTodos} |
| 48 | + /> |
| 49 | + <label>Show only done todos</label> |
| 50 | + <ul>{todoItems}</ul> |
| 51 | + </div> |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +export default TodoListView; |
0 commit comments