-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueries.ts
More file actions
155 lines (142 loc) · 3.43 KB
/
queries.ts
File metadata and controls
155 lines (142 loc) · 3.43 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
useMutation,
UseMutationResult,
useQuery,
UseQueryResult,
} from '@tanstack/react-query';
import {
deleteCategory,
getDashboardCategories,
postCategory,
postSignUp,
postSignUpRequest,
putEditArticle,
putCategory,
putArticleReadStatus,
getArticleDetail,
getAcorns,
deleteRemindArticle,
getGoogleProfile,
getMyProfile,
} from '@shared/apis/axios';
import { AxiosError } from 'axios';
import {
DashboardCategoriesResponse,
AcornsResponse,
EditArticleRequest,
ArticleReadStatusResponse,
ArticleDetailResponse,
} from '@shared/types/api';
import { fetchOGData } from '@shared/utils/fetchOgData';
export const useGetDashboardCategories = (): UseQueryResult<
DashboardCategoriesResponse,
AxiosError
> => {
return useQuery({
queryKey: ['dashboardCategories'],
queryFn: () => getDashboardCategories(),
});
};
export const usePostCategory = () => {
return useMutation({
mutationFn: (categoryName: string) => postCategory(categoryName),
});
};
export const usePutCategory = () => {
return useMutation({
mutationFn: ({ id, categoryName }: { id: number; categoryName: string }) =>
putCategory(id, categoryName),
});
};
export const useDeleteCategory = () => {
return useMutation({
mutationFn: (id: number) => deleteCategory(id),
});
};
export const useGetArcons = (): UseQueryResult<AcornsResponse, AxiosError> => {
return useQuery({
queryKey: ['arcons'],
queryFn: () => getAcorns(),
});
};
export const usePostSignUp = () => {
return useMutation({
mutationFn: (data: postSignUpRequest) => postSignUp(data),
onSuccess: (data) => {
const newToken = data?.data?.token || data?.token;
const sendTokenToExtension = (token: string) => {
window.postMessage(
{
type: 'SET_TOKEN',
token,
},
window.location.origin
);
};
if (newToken) {
localStorage.setItem('token', newToken);
sendTokenToExtension(newToken);
}
console.log('회원가입 성공:', data);
},
onError: (error) => {
console.error('회원가입 실패:', error);
},
});
};
export const usePutArticleReadStatus = (): UseMutationResult<
ArticleReadStatusResponse,
AxiosError,
number
> => {
return useMutation({
mutationFn: (articleId: number) => putArticleReadStatus(articleId),
});
};
export const useDeleteRemindArticle = () => {
return useMutation({
mutationFn: (id: number) => deleteRemindArticle(id),
});
};
export const useGetArticleDetail = (): UseMutationResult<
ArticleDetailResponse,
AxiosError,
number
> => {
return useMutation({
mutationFn: (articleId: number) => getArticleDetail(articleId),
});
};
export const usePutEditArticle = () => {
return useMutation({
mutationFn: ({
articleId,
editArticleData,
}: {
articleId: number;
editArticleData: EditArticleRequest;
}) => putEditArticle(articleId, editArticleData),
});
};
export const useGetPageMeta = (url: string) => {
return useQuery({
queryKey: ['ogMeta', url],
queryFn: () => fetchOGData(url),
enabled: !!url,
staleTime: Infinity,
retry: false,
});
};
export const useGetGoogleProfile = () => {
return useQuery({
queryKey: ['googleProfile'],
queryFn: getGoogleProfile,
staleTime: Infinity,
});
};
export function useGetMyProfile() {
return useQuery({
queryKey: ['myProfile'],
queryFn: getMyProfile,
});
}