Skip to content

Commit d024a24

Browse files
committed
refactor into more than one component
1 parent 0f33780 commit d024a24

File tree

1 file changed

+71
-57
lines changed

1 file changed

+71
-57
lines changed

src/App.js

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,6 @@ const todoReducer = (state, action) => {
6464
const App = () => {
6565
const [filter, dispatchFilter] = useReducer(filterReducer, 'ALL');
6666
const [todos, dispatchTodos] = useReducer(todoReducer, initalTodos);
67-
const [task, setTask] = useState('');
68-
69-
const handleShowAll = () => {
70-
dispatchFilter({ type: 'SHOW_ALL' });
71-
};
72-
73-
const handleShowComplete = () => {
74-
dispatchFilter({ type: 'SHOW_COMPLETE' });
75-
};
76-
77-
const handleShowIncomplete = () => {
78-
dispatchFilter({ type: 'SHOW_INCOMPLETE' });
79-
};
8067

8168
const filteredTodos = todos.filter(todo => {
8269
if (filter === 'ALL') {
@@ -94,65 +81,92 @@ const App = () => {
9481
return false;
9582
});
9683

97-
const handleChangeCheckbox = todo => {
98-
dispatchTodos({
84+
return (
85+
<div>
86+
<Filter dispatch={dispatchFilter} />
87+
<TodoList dispatch={dispatchTodos} todos={filteredTodos} />
88+
<AddTodo dispatch={dispatchTodos} />
89+
</div>
90+
);
91+
};
92+
93+
const Filter = ({ dispatch }) => {
94+
const handleShowAll = () => {
95+
dispatch({ type: 'SHOW_ALL' });
96+
};
97+
98+
const handleShowComplete = () => {
99+
dispatch({ type: 'SHOW_COMPLETE' });
100+
};
101+
102+
const handleShowIncomplete = () => {
103+
dispatch({ type: 'SHOW_INCOMPLETE' });
104+
};
105+
106+
return (
107+
<div>
108+
<button type="button" onClick={handleShowAll}>
109+
Show All
110+
</button>
111+
<button type="button" onClick={handleShowComplete}>
112+
Show Complete
113+
</button>
114+
<button type="button" onClick={handleShowIncomplete}>
115+
Show Incomplete
116+
</button>
117+
</div>
118+
);
119+
};
120+
121+
const TodoList = ({ dispatch, todos }) => (
122+
<ul>
123+
{todos.map(todo => (
124+
<TodoItem key={todo.id} dispatch={dispatch} todo={todo} />
125+
))}
126+
</ul>
127+
);
128+
129+
const TodoItem = ({ dispatch, todo }) => {
130+
const handleChange = () =>
131+
dispatch({
99132
type: todo.complete ? 'UNDO_TODO' : 'DO_TODO',
100133
id: todo.id,
101134
});
102-
};
103135

104-
const handleChangeInput = event => {
105-
setTask(event.target.value);
106-
};
136+
return (
137+
<li>
138+
<label>
139+
<input
140+
type="checkbox"
141+
checked={todo.complete}
142+
onChange={handleChange}
143+
/>
144+
{todo.task}
145+
</label>
146+
</li>
147+
);
148+
};
149+
150+
const AddTodo = ({ dispatch }) => {
151+
const [task, setTask] = useState('');
107152

108153
const handleSubmit = event => {
109154
if (task) {
110-
dispatchTodos({ type: 'ADD_TODO', task });
155+
dispatch({ type: 'ADD_TODO', task });
111156
}
112157

113158
setTask('');
114159

115160
event.preventDefault();
116161
};
117162

163+
const handleChange = event => setTask(event.target.value);
164+
118165
return (
119-
<div>
120-
<div>
121-
<button type="button" onClick={handleShowAll}>
122-
Show All
123-
</button>
124-
<button type="button" onClick={handleShowComplete}>
125-
Show Complete
126-
</button>
127-
<button type="button" onClick={handleShowIncomplete}>
128-
Show Incomplete
129-
</button>
130-
</div>
131-
132-
<ul>
133-
{filteredTodos.map(todo => (
134-
<li key={todo.id}>
135-
<label>
136-
<input
137-
type="checkbox"
138-
checked={todo.complete}
139-
onChange={() => handleChangeCheckbox(todo)}
140-
/>
141-
{todo.task}
142-
</label>
143-
</li>
144-
))}
145-
</ul>
146-
147-
<form onSubmit={handleSubmit}>
148-
<input
149-
type="text"
150-
value={task}
151-
onChange={handleChangeInput}
152-
/>
153-
<button type="submit">Add Todo</button>
154-
</form>
155-
</div>
166+
<form onSubmit={handleSubmit}>
167+
<input type="text" value={task} onChange={handleChange} />
168+
<button type="submit">Add Todo</button>
169+
</form>
156170
);
157171
};
158172

0 commit comments

Comments
 (0)