Skip to content

Commit

Permalink
🎨 모각코 참석/참석 취소 관련 query hook 및 query key 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
js43o committed Nov 23, 2023
1 parent 52e65f0 commit 8c8cbb2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/frontend/src/mocks/mogaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const participantsList = [

export const mogacoAPIHandlers = [
http.get('/mogaco', () => HttpResponse.json<Mogaco[]>(mogacoList)),
http.delete('/mogaco/:id', () => HttpResponse.json(null, { status: 200 })),
http.get('/mogaco/:id', ({ params: { id } }) =>
HttpResponse.json<Mogaco>(mogacoList[Number(id) - 1]),
),
Expand All @@ -81,5 +82,4 @@ export const mogacoAPIHandlers = [
);
return HttpResponse.json(null, { status: 200 });
}),
http.delete('/mogaco/:id', () => HttpResponse.json(null, { status: 200 })),
];
29 changes: 27 additions & 2 deletions app/frontend/src/queries/hooks/mogaco.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { queryKeys } from '@/queries';
import { mogaco } from '@/services';

import { queryKeys } from '..';

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

Expand All @@ -16,3 +15,29 @@ export const useDeleteMogacoQuery = () => {
},
});
};

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

return useMutation({
mutationFn: (postId: string) => mogaco.join(postId),
onSuccess: (data, postId) => {
queryClient.invalidateQueries({
queryKey: queryKeys.mogaco.participants(postId).queryKey,
});
},
});
};

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

return useMutation({
mutationFn: (postId: string) => mogaco.delete(postId),
onSuccess: (data, postId) => {
queryClient.invalidateQueries({
queryKey: queryKeys.mogaco.participants(postId).queryKey,
});
},
});
};
3 changes: 2 additions & 1 deletion app/frontend/src/queries/mogaco.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { createQueryKeys } from '@lukemorales/query-key-factory';

import { mogaco } from '@/services';
Expand All @@ -9,4 +9,5 @@ export const mogacoKeys = createQueryKeys('mogaco', {
queryFn: () => mogaco.list(),
}),
detail: (id: string) => [id],
participants: (id: string) => [id],
});
8 changes: 6 additions & 2 deletions app/frontend/src/services/mogaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export const mogaco = {
return data;
},
join: async (id: string) => {
await morakAPI.post(`${mogaco.endPoint.index}/${id}/join`);
const response = await morakAPI.post(`${mogaco.endPoint.index}/${id}/join`);
return response;
},
quit: async (id: string) => {
await morakAPI.delete(`${mogaco.endPoint.index}/${id}/join`);
const response = await morakAPI.delete(
`${mogaco.endPoint.index}/${id}/join`,
);
return response;
},
delete: async (id: string) => {
const response = await morakAPI.delete(`${mogaco.endPoint.index}/${id}`);
Expand Down

0 comments on commit 8c8cbb2

Please sign in to comment.