Skip to content

Commit

Permalink
deal with みずしまレビュー1 #1 No.5
Browse files Browse the repository at this point in the history
  • Loading branch information
cataws committed Dec 12, 2021
1 parent 5344717 commit b63945d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/components/TodoItem.jsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { useContext } from "react";
import { TodoContext } from "./provider/TodoProvider";
import { setPersistentTodos } from "../utils/index";
import useCustomTodo from "../hooks/useCustomTodo";
import Button from "./Button";

const TodoItem = function () {
const { todos, setTodo } = useContext(TodoContext);

// Completeボタン押下時の処理(完了⇔未完了のトグル)
const handleCompleteTask = (completeTask) => {
const completedTodos = [...todos].map((todo) => {
if (todo.item === completeTask.item) {
todo.isCompleted = !todo.isCompleted;
}
return todo;
});
setTodo(completedTodos);
setPersistentTodos(completedTodos);
};

// Deleteボタン押下時の処理
const handleDeleteTask = (deleteTask) => {
const deletedTodos = [...todos].filter((todo) => todo.item !== deleteTask);
setTodo(deletedTodos);
setPersistentTodos(deletedTodos);
};
const { handleCompleteTask, handleDeleteTask } = useCustomTodo();

return (
<ul id="task_list">
Expand Down
29 changes: 29 additions & 0 deletions src/hooks/useCustomTodo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useContext } from "react";
import { TodoContext } from "../components/provider/TodoProvider";
import { setPersistentTodos } from "../utils/index";

const useCustomTodo = function () {
const { todos, setTodo } = useContext(TodoContext);

// Completeボタン押下時の処理(完了⇔未完了のトグル)
const handleCompleteTask = (completeTask) => {
const completedTodos = [...todos].map((todo) => {
if (todo.item === completeTask.item) {
todo.isCompleted = !todo.isCompleted;
}
return todo;
});
setTodo(completedTodos);
setPersistentTodos(completedTodos);
};

// Deleteボタン押下時の処理
const handleDeleteTask = (deleteTask) => {
const deletedTodos = [...todos].filter((todo) => todo.item !== deleteTask);
setTodo(deletedTodos);
setPersistentTodos(deletedTodos);
};
return { handleCompleteTask, handleDeleteTask };
};

export default useCustomTodo;

0 comments on commit b63945d

Please sign in to comment.