-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |