mirror of
https://github.com/ershisan99/todolist_next.git
synced 2025-12-16 20:59:24 +00:00
add create and deelte todolist functionalities
This commit is contained in:
@@ -2,15 +2,21 @@ import { type NextPage } from "next";
|
|||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { Button } from "../components/Button";
|
import { Button } from "../components/Button";
|
||||||
import {
|
import {
|
||||||
|
useCreateTodolistMutation,
|
||||||
useDeleteTaskMutation,
|
useDeleteTaskMutation,
|
||||||
|
useDeleteTodolistMutation,
|
||||||
useGetTasksQuery,
|
useGetTasksQuery,
|
||||||
useLogoutMutation,
|
useLogoutMutation,
|
||||||
usePutTaskMutation,
|
usePutTaskMutation,
|
||||||
useTodolistsQuery,
|
useTodolistsQuery,
|
||||||
} from "../services/hooks";
|
} from "../services/hooks";
|
||||||
import { Loader } from "../components/loader";
|
import { Loader } from "../components/loader";
|
||||||
|
import { Input } from "../components/Input";
|
||||||
|
import type { ChangeEvent } from "react";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
const Home: NextPage = () => {
|
const Home: NextPage = () => {
|
||||||
|
const [newTodolistTitle, setNewTodolistTitle] = useState("");
|
||||||
const { mutate: logout } = useLogoutMutation();
|
const { mutate: logout } = useLogoutMutation();
|
||||||
const { data: todolists, isLoading: isTodolistsLoading } =
|
const { data: todolists, isLoading: isTodolistsLoading } =
|
||||||
useTodolistsQuery();
|
useTodolistsQuery();
|
||||||
@@ -25,6 +31,8 @@ const Home: NextPage = () => {
|
|||||||
};
|
};
|
||||||
const { mutate: putTask } = usePutTaskMutation();
|
const { mutate: putTask } = usePutTaskMutation();
|
||||||
const { mutate: deleteTask } = useDeleteTaskMutation();
|
const { mutate: deleteTask } = useDeleteTaskMutation();
|
||||||
|
const { mutateAsync: createTodolist } = useCreateTodolistMutation();
|
||||||
|
const { mutate: deleteTodolist } = useDeleteTodolistMutation();
|
||||||
const handleChangeStatus = (todolistId: string, task: any) => {
|
const handleChangeStatus = (todolistId: string, task: any) => {
|
||||||
const newTask = { ...task, status: task.status === 0 ? 2 : 0 };
|
const newTask = { ...task, status: task.status === 0 ? 2 : 0 };
|
||||||
putTask({ todolistId, task: newTask });
|
putTask({ todolistId, task: newTask });
|
||||||
@@ -32,6 +40,19 @@ const Home: NextPage = () => {
|
|||||||
const handleDeleteTask = (todolistId: string, taskId: string) => {
|
const handleDeleteTask = (todolistId: string, taskId: string) => {
|
||||||
deleteTask({ todolistId, taskId });
|
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<HTMLInputElement>) => {
|
||||||
|
setNewTodolistTitle(e.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
if (isTodolistsLoading || isLoadingTasks)
|
if (isTodolistsLoading || isLoadingTasks)
|
||||||
return (
|
return (
|
||||||
@@ -50,7 +71,17 @@ const Home: NextPage = () => {
|
|||||||
<h1 className={"text-3xl"}>Todolist</h1>
|
<h1 className={"text-3xl"}>Todolist</h1>
|
||||||
<Button onClick={handleLogout}>Logout</Button>
|
<Button onClick={handleLogout}>Logout</Button>
|
||||||
</header>
|
</header>
|
||||||
<main className={"p-4"}>
|
<main className={"flex flex-col gap-4 p-4"}>
|
||||||
|
<div className={"flex items-end gap-2.5"}>
|
||||||
|
<label className={"flex w-52 flex-col gap-0.5"}>
|
||||||
|
new todolist
|
||||||
|
<Input
|
||||||
|
value={newTodolistTitle}
|
||||||
|
onChange={handleNewTodolistTitleChange}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<Button onClick={handleAddTodolist}>+</Button>
|
||||||
|
</div>
|
||||||
{todolists?.map((todolist) => {
|
{todolists?.map((todolist) => {
|
||||||
const tasksForTodolist = tasks?.find((t) => {
|
const tasksForTodolist = tasks?.find((t) => {
|
||||||
return t?.todolistId === todolist.id;
|
return t?.todolistId === todolist.id;
|
||||||
@@ -58,9 +89,14 @@ const Home: NextPage = () => {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={todolist.id}
|
key={todolist.id}
|
||||||
className={"mb-4 rounded-md border border-gray-300 p-4"}
|
className={"rounded-md border border-gray-300 p-4"}
|
||||||
>
|
>
|
||||||
<h2 className={"text-xl"}>{todolist.title}</h2>
|
<div className={"flex items-center justify-between "}>
|
||||||
|
<h2 className={"text-xl"}>{todolist.title}</h2>
|
||||||
|
<button onClick={() => handleDeleteTodolist(todolist.id)}>
|
||||||
|
x
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
{tasksForTodolist?.map((task: any) => (
|
{tasksForTodolist?.map((task: any) => (
|
||||||
<div className={"flex items-center gap-2"} key={task.id}>
|
<div className={"flex items-center gap-2"} key={task.id}>
|
||||||
<input
|
<input
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import {
|
|||||||
} from "@tanstack/react-query";
|
} from "@tanstack/react-query";
|
||||||
import type { PostLoginArgs } from "./index";
|
import type { PostLoginArgs } from "./index";
|
||||||
import {
|
import {
|
||||||
|
createTodolist,
|
||||||
deleteMe,
|
deleteMe,
|
||||||
deleteTask,
|
deleteTask,
|
||||||
|
deleteTodolist,
|
||||||
getMe,
|
getMe,
|
||||||
getTask,
|
getTask,
|
||||||
getTodolists,
|
getTodolists,
|
||||||
@@ -100,3 +102,23 @@ export const useDeleteTaskMutation = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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"]);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -47,6 +47,15 @@ export const putTask = (todolistId: string, task: any) => {
|
|||||||
const { id, ...rest } = task;
|
const { id, ...rest } = task;
|
||||||
return instance.put(`todo-lists/${todolistId}/tasks/${id}`, rest);
|
return instance.put(`todo-lists/${todolistId}/tasks/${id}`, rest);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteTask = (todolistId: string, taskId: string) => {
|
export const deleteTask = (todolistId: string, taskId: string) => {
|
||||||
return instance.delete(`todo-lists/${todolistId}/tasks/${taskId}`);
|
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}`);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user