-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-update-todo-submit.ts
More file actions
62 lines (54 loc) · 1.73 KB
/
Copy pathuse-update-todo-submit.ts
File metadata and controls
62 lines (54 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use client";
import { useQueryClient } from "@tanstack/react-query";
import type { ErrorType } from "@/api/client/custom-instance";
import type { ErrorDto, TodoUpdateRequest } from "@/api/generated/models";
import { getGetFocusTodoQueryKey } from "@/api/generated/endpoints/focus/focus";
import {
getGetHomeQueryKey,
getGetTodayQueryKey,
} from "@/api/generated/endpoints/home/home";
import {
getGetTodoDetailQueryKey,
useUpdateTodo,
} from "@/api/generated/endpoints/todo/todo";
import { useStatisticsQueryInvalidation } from "@/hooks/statistics/use-statistics-query-invalidation";
export interface UpdateTodoSubmitParams {
todoId: number;
date: string;
data: TodoUpdateRequest;
}
export interface UpdateTodoSubmitHandlers {
onSuccess?: () => void;
onError?: (error: ErrorType<ErrorDto>) => void;
}
export const useUpdateTodoSubmit = () => {
const { mutate: updateTodo } = useUpdateTodo();
const queryClient = useQueryClient();
const { invalidateStatistics } = useStatisticsQueryInvalidation();
const handleUpdate = (
{ todoId, date, data }: UpdateTodoSubmitParams,
{ onSuccess, onError }: UpdateTodoSubmitHandlers = {},
) => {
updateTodo(
{ todoId, data },
{
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: getGetHomeQueryKey() });
queryClient.invalidateQueries({ queryKey: getGetTodayQueryKey() });
queryClient.invalidateQueries({
queryKey: getGetTodoDetailQueryKey(todoId, { date }),
});
invalidateStatistics();
queryClient.invalidateQueries({
queryKey: getGetFocusTodoQueryKey(),
});
onSuccess?.();
},
onError,
},
);
};
return {
handleUpdate,
};
};