From e3c77f85f4f352ce07d84280fe8b81947181ff05 Mon Sep 17 00:00:00 2001 From: Andres Date: Sun, 20 Nov 2022 13:08:43 +0100 Subject: [PATCH] add create and deelte todolist functionalities --- src/pages/index.tsx | 42 +++++++++++++++++++++++++++++++++++++++--- src/services/hooks.ts | 22 ++++++++++++++++++++++ src/services/index.ts | 9 +++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index c7e87a1..1da3062 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -2,15 +2,21 @@ import { type NextPage } from "next"; import Head from "next/head"; import { Button } from "../components/Button"; import { + useCreateTodolistMutation, useDeleteTaskMutation, + useDeleteTodolistMutation, useGetTasksQuery, useLogoutMutation, usePutTaskMutation, useTodolistsQuery, } from "../services/hooks"; import { Loader } from "../components/loader"; +import { Input } from "../components/Input"; +import type { ChangeEvent } from "react"; +import { useState } from "react"; const Home: NextPage = () => { + const [newTodolistTitle, setNewTodolistTitle] = useState(""); const { mutate: logout } = useLogoutMutation(); const { data: todolists, isLoading: isTodolistsLoading } = useTodolistsQuery(); @@ -25,6 +31,8 @@ const Home: NextPage = () => { }; const { mutate: putTask } = usePutTaskMutation(); const { mutate: deleteTask } = useDeleteTaskMutation(); + const { mutateAsync: createTodolist } = useCreateTodolistMutation(); + const { mutate: deleteTodolist } = useDeleteTodolistMutation(); const handleChangeStatus = (todolistId: string, task: any) => { const newTask = { ...task, status: task.status === 0 ? 2 : 0 }; putTask({ todolistId, task: newTask }); @@ -32,6 +40,19 @@ const Home: NextPage = () => { const handleDeleteTask = (todolistId: string, taskId: string) => { deleteTask({ todolistId, taskId }); }; + const handleAddTodolist = () => { + createTodolist(newTodolistTitle).then((res) => { + if (res.data.resultCode === 0) setNewTodolistTitle(""); + }); + }; + + const handleDeleteTodolist = (todolistId: string) => { + deleteTodolist(todolistId); + }; + + const handleNewTodolistTitleChange = (e: ChangeEvent) => { + setNewTodolistTitle(e.target.value); + }; if (isTodolistsLoading || isLoadingTasks) return ( @@ -50,7 +71,17 @@ const Home: NextPage = () => {

Todolist

-
+
+
+ + +
{todolists?.map((todolist) => { const tasksForTodolist = tasks?.find((t) => { return t?.todolistId === todolist.id; @@ -58,9 +89,14 @@ const Home: NextPage = () => { return (
-

{todolist.title}

+
+

{todolist.title}

+ +
{tasksForTodolist?.map((task: any) => (
{ }, }); }; + +export const useCreateTodolistMutation = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: (title: string) => createTodolist(title), + onSuccess: () => { + queryClient.invalidateQueries(["todolists"]); + }, + }); +}; + +export const useDeleteTodolistMutation = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: (todolistId: string) => deleteTodolist(todolistId), + onSuccess: () => { + queryClient.invalidateQueries(["todolists"]); + }, + }); +}; diff --git a/src/services/index.ts b/src/services/index.ts index 56474a3..ddbe2d6 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -47,6 +47,15 @@ export const putTask = (todolistId: string, task: any) => { const { id, ...rest } = task; return instance.put(`todo-lists/${todolistId}/tasks/${id}`, rest); }; + export const deleteTask = (todolistId: string, taskId: string) => { return instance.delete(`todo-lists/${todolistId}/tasks/${taskId}`); }; + +export const createTodolist = (title: string) => { + return instance.post(`todo-lists`, { title }); +}; + +export const deleteTodolist = (todolistId: string) => { + return instance.delete(`todo-lists/${todolistId}`); +};