-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[길수진] week20 #1083
The head ref may contain hidden characters: "part4-\uAE38\uC218\uC9C4-week20"
[길수진] week20 #1083
Changes from 1 commit
e07640b
b4adfa0
c10719d
baf6573
097b2ce
1a1ffed
4750f55
603e888
6771c68
e3f5c91
e2b8034
2f80437
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import instance from "lib/axios"; | ||
|
||
export const useDeleteFolder = () => { | ||
const deleteFolder = async (folderId: number) => { | ||
try { | ||
await instance.delete(`/folders/${folderId}`); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분도 api 폴더로 따로 빼서 관리해보면 좋을 것 같아요! |
||
|
||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationFn: deleteFolder, | ||
onSuccess: () => | ||
queryClient.invalidateQueries({ | ||
queryKey: ["folders"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tanstack query 메인테이너인 tkdodo 블로그를 보면 queryKey를 관리하는 방식에 대해서 설명해주고 있어요! 해당 부분 참고해서 보시면 좋을 것 같아요! 📌 참고 |
||
}), | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import instance from "lib/axios"; | ||
|
||
export const useDeleteLink = (folderId: number) => { | ||
const deleteLink = async (linkId: number) => { | ||
try { | ||
await instance.delete(`/links/${linkId}`); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationFn: deleteLink, | ||
onSuccess: () => | ||
queryClient.invalidateQueries({ | ||
queryKey: ["links", folderId], | ||
}), | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import useFetch from "hooks/useFetch"; | ||
import type { Folder } from "types"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import instance from "lib/axios"; | ||
|
||
export const useGetFolders = () => { | ||
export type Folder = { | ||
id: number; | ||
created_at: Date; | ||
favorite: boolean; | ||
name: string; | ||
link_count: number; | ||
}; | ||
|
||
const { data, loading, error } = useFetch<{ data: { folder: Folder[] } }>( | ||
`/folders` | ||
); | ||
const folderData = data?.data?.folder ?? []; | ||
const fetchFolders = async () => { | ||
try { | ||
const { data } = await instance.get<Folder[]>("/folders"); | ||
return data; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
return { data: folderData, loading, error }; | ||
export const useGetFolders = () => { | ||
return useQuery({ | ||
queryKey: ["folders"], | ||
queryFn: fetchFolders, | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api 요청을 hook으로 관리하려는 시도 너무 좋은 것 같아요👍🏻