|
1 | 1 | import { queryKeys } from '@src/constants';
|
2 |
| -import { MemoSupabaseClient, MemoSupabaseResponse } from '@src/types'; |
3 |
| -import { formatUrl, insertMemo, updateMemo } from '@src/utils'; |
4 |
| -import { getSupabaseClient } from '@src/utils/extension'; |
| 2 | +import { MemoSupabaseClient, MemoSupabaseResponse, MemoTableInsert } from '@src/types'; |
| 3 | +import { insertMemo } from '@src/utils'; |
5 | 4 | import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query';
|
6 |
| -import useMemoListQuery from './useMemoListQuery'; |
7 | 5 |
|
8 |
| -interface SaveMemoProps { |
9 |
| - memo: string; |
10 |
| - url: string; |
11 |
| - title: string; |
12 |
| - category: string; |
13 |
| -} |
| 6 | +interface PostMemoProps extends MemoTableInsert {} |
14 | 7 |
|
15 |
| -interface UseMemoPostMutationProps extends UseMutationOptions<MemoSupabaseResponse, Error, SaveMemoProps> { |
| 8 | +interface UseMemoPostMutationProps extends UseMutationOptions<MemoSupabaseResponse, Error, PostMemoProps> { |
16 | 9 | supabaseClient: MemoSupabaseClient;
|
17 |
| - handleSettled: () => void; |
| 10 | + handleSuccess: () => void; |
18 | 11 | }
|
19 | 12 |
|
20 | 13 | export default function useMemoPostMutation({
|
21 | 14 | supabaseClient,
|
22 |
| - handleSettled, |
| 15 | + handleSuccess, |
23 | 16 | ...useMutationProps
|
24 | 17 | }: UseMemoPostMutationProps) {
|
25 | 18 | const queryClient = useQueryClient();
|
26 |
| - const { data: memoList } = useMemoListQuery({ supabaseClient }); |
27 |
| - |
28 |
| - const saveMemo = async ({ memo, title, url, category }: SaveMemoProps) => { |
29 |
| - const currentMemo = memoList?.data?.find(memo => memo.url === formatUrl(url)); |
30 |
| - const supabaseClient = await getSupabaseClient(); |
31 |
| - |
32 |
| - console.log(memo, title, url, category); |
33 |
| - |
34 |
| - if (currentMemo) return await updateMemo(supabaseClient, { ...currentMemo, memo, category }); |
35 |
| - else return await insertMemo(supabaseClient, { memo, title, url, category }); |
36 |
| - }; |
37 |
| - |
38 |
| - return useMutation<MemoSupabaseResponse, Error, SaveMemoProps>({ |
| 19 | + return useMutation<MemoSupabaseResponse, Error, PostMemoProps>({ |
39 | 20 | ...useMutationProps,
|
40 |
| - mutationFn: saveMemo, |
41 |
| - onSettled: async () => { |
| 21 | + mutationFn: async (postMemoProps: PostMemoProps) => await insertMemo(supabaseClient, postMemoProps), |
| 22 | + onSuccess: async () => { |
42 | 23 | queryClient.invalidateQueries({ queryKey: queryKeys.memoList() });
|
43 |
| - handleSettled(); |
| 24 | + handleSuccess(); |
44 | 25 | },
|
45 | 26 | });
|
46 | 27 | }
|
0 commit comments