Skip to content

Commit 23d7d59

Browse files
committedMar 31, 2022
Refactor todo list
1 parent 4cfe1d6 commit 23d7d59

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed
 

‎todo-list/src/pages/TodoList/TodoList.tsx

+6-42
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,18 @@ import React, { useReducer, useState } from "react";
22
import Header from "../../components/Header/Header";
33
import InputContainer from "../../components/InputContainer/InputContainer";
44
import "./TodoList.css";
5-
6-
interface ITodoItem {
7-
text: string;
8-
id: number;
9-
done: boolean;
10-
}
11-
12-
interface IAction {
13-
type: string;
14-
payload?: ITodoItem | number;
15-
}
16-
17-
const ADD_TODO = "ADD_TODO";
18-
const CHANGE_STATUS = "CHANGE_STATUS";
19-
20-
const todoReducer = (state: ITodoItem[], action: IAction) => {
21-
switch (action.type) {
22-
case ADD_TODO:
23-
if (
24-
action.payload &&
25-
typeof action.payload !== "number" &&
26-
action.payload.text.trim() !== ""
27-
) {
28-
return [...state, action.payload];
29-
}
30-
case CHANGE_STATUS:
31-
const oldState = [...state];
32-
if (action.payload && typeof action.payload === "number") {
33-
const index = oldState.findIndex((ele) => ele.id === action.payload);
34-
if (index !== -1) {
35-
oldState[index].done = !oldState[index].done;
36-
}
37-
}
38-
return oldState;
39-
default:
40-
return state;
41-
}
42-
};
5+
import todoReducer from "../../utils/todoReducer";
6+
import { ADD_TODO, CHANGE_STATUS } from "../../utils/todoActions";
437

448
const TodoList = () => {
459
const [todoList, dispatch] = useReducer(todoReducer, []);
4610
const [inputTodo, setInputTodo] = useState<string>("");
4711

48-
const changeTextHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
12+
const handleChangeText = (event: React.ChangeEvent<HTMLInputElement>) => {
4913
setInputTodo(event.target.value);
5014
};
5115

52-
const addTodoItemHandler = () => {
16+
const handleAddTodoItem = () => {
5317
dispatch({
5418
type: ADD_TODO,
5519
payload: {
@@ -68,10 +32,10 @@ const TodoList = () => {
6832
<input
6933
type="text"
7034
placeholder="Search"
71-
onChange={changeTextHandler}
35+
onChange={handleChangeText}
7236
value={inputTodo}
7337
/>
74-
<button onClick={addTodoItemHandler}>Add</button>
38+
<button onClick={handleAddTodoItem}>Add</button>
7539
</InputContainer>
7640
{todoList.length !== 0 && (
7741
<div className="status">

‎todo-list/src/utils/todoActions.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const ADD_TODO = "ADD_TODO";
2+
export const CHANGE_STATUS = "CHANGE_STATUS";

‎todo-list/src/utils/todoReducer.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {ADD_TODO, CHANGE_STATUS} from './todoActions'
2+
3+
export interface ITodoItem {
4+
text: string;
5+
id: number;
6+
done: boolean;
7+
}
8+
9+
export interface IAction {
10+
type: string;
11+
payload?: ITodoItem | number;
12+
}
13+
14+
const todoReducer = (state: ITodoItem[], action: IAction) => {
15+
switch (action.type) {
16+
case ADD_TODO:
17+
if (
18+
action.payload &&
19+
typeof action.payload !== "number" &&
20+
action.payload.text.trim() !== ""
21+
) {
22+
return [...state, action.payload];
23+
}
24+
case CHANGE_STATUS:
25+
const oldState = [...state];
26+
if (action.payload && typeof action.payload === "number") {
27+
const index = oldState.findIndex((ele) => ele.id === action.payload);
28+
if (index !== -1) {
29+
oldState[index].done = !oldState[index].done;
30+
}
31+
}
32+
return oldState;
33+
default:
34+
return state;
35+
}
36+
};
37+
38+
export default todoReducer

0 commit comments

Comments
 (0)
Please sign in to comment.