Skip to content
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

6 changes: 6 additions & 0 deletions constants/queryKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const QUERY_KEYS = {
USER: ["user"] as const,
FOLDERS: ["folders"] as const,
LINKS: (folderId?: number | null) =>
folderId ? (["links", folderId] as const) : (["links"] as const),
Comment on lines +2 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞에 key 관리에 대한 리뷰를 달았었는데 key 관리를 잘하고 계셨군요!?!? 👍🏻👍🏻👍🏻👍🏻👍🏻

};
4 changes: 3 additions & 1 deletion hooks/useAddFolder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export const useAddFolder = () => {
const queryClient = useQueryClient();

Expand All @@ -10,7 +12,7 @@ export const useAddFolder = () => {
},
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ["folders"],
queryKey: QUERY_KEYS.FOLDERS,
}),
});
};
4 changes: 3 additions & 1 deletion hooks/useDeleteFolder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export const useDeleteFolder = () => {
const deleteFolder = async (folderId: number) => {
try {
Expand All @@ -16,7 +18,7 @@ export const useDeleteFolder = () => {
mutationFn: deleteFolder,
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ["folders"],
queryKey: QUERY_KEYS.FOLDERS,
}),
});
};
4 changes: 3 additions & 1 deletion hooks/useDeleteLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export const useDeleteLink = (folderId: number) => {
const deleteLink = async (linkId: number) => {
try {
Expand All @@ -16,7 +18,7 @@ export const useDeleteLink = (folderId: number) => {
mutationFn: deleteLink,
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ["links", folderId],
queryKey: QUERY_KEYS.LINKS(folderId),
}),
});
};
29 changes: 0 additions & 29 deletions hooks/useFetch.ts

This file was deleted.

4 changes: 3 additions & 1 deletion hooks/useGetFolders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export type Folder = {
id: number;
created_at: Date;
Expand All @@ -20,7 +22,7 @@ const fetchFolders = async () => {

export const useGetFolders = () => {
return useQuery({
queryKey: ["folders"],
queryKey: QUERY_KEYS.FOLDERS,
queryFn: fetchFolders,
staleTime: 1000 * 60,
});
Expand Down
4 changes: 3 additions & 1 deletion hooks/useGetLinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export type Link = {
id: number;
favorite: boolean;
Expand All @@ -23,7 +25,7 @@ export const useGetLinks = (folderId: number | null) => {
};

return useQuery({
queryKey: ["links", folderId],
queryKey: QUERY_KEYS.LINKS(folderId),
queryFn: fetchLinks,
staleTime: 1000 * 60,
});
Expand Down
27 changes: 0 additions & 27 deletions hooks/useGetSampleData.ts

This file was deleted.

4 changes: 3 additions & 1 deletion hooks/useGetUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

interface UserResponse {
id: number;
name: string;
Expand All @@ -17,7 +19,7 @@ const fetchUser = async (): Promise<UserResponse> => {

export const useGetUser = () => {
return useQuery({
queryKey: ["user"],
queryKey: QUERY_KEYS.USER,
queryFn: fetchUser,
staleTime: 1000 * 60 * 60,
});
Expand Down
17 changes: 0 additions & 17 deletions hooks/useLocalStorage.ts

This file was deleted.

1 change: 0 additions & 1 deletion hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const useLogin = () => {
return instance.post("/auth/sign-in", data);
},
onSuccess: (res) => {
// 토큰 저장
const { accessToken, refreshToken } = res.data;
setAccessToken(accessToken);
Cookies.set("refreshToken", refreshToken, { expires: 7 });
Expand Down
13 changes: 9 additions & 4 deletions hooks/useUpdateFolder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import instance from "lib/axios";

import { QUERY_KEYS } from "constants/queryKey";

export type Folder = {
id: number;
created_at: Date;
Expand All @@ -14,14 +16,17 @@ export const useUpdateFolder = () => {

return useMutation({
mutationFn: async (data: { folderId: number; name: string }) => {
const response = await instance.put<Folder[]>(`/folders/${data.folderId}`, {
name: data.name,
});
const response = await instance.put<Folder[]>(
`/folders/${data.folderId}`,
{
name: data.name,
}
);
return response.data;
},
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ["folders"],
queryKey: QUERY_KEYS.FOLDERS,
}),
onError: (error) => {
throw error;
Expand Down