|
| 1 | +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; |
| 2 | +import { useParams } from 'react-router-dom'; |
| 3 | + |
| 4 | +import { EvaluationResult } from '@customTypes/applicant'; |
| 5 | +// import { useToast } from '@contexts/ToastContext'; |
| 6 | + |
| 7 | +import evaluationApis from '@api/domain/evaluation'; |
| 8 | +import QUERY_KEYS from '@hooks/queryKeys'; |
| 9 | + |
| 10 | +interface DefaultQueryParams { |
| 11 | + processId: number; |
| 12 | + applicantId: number; |
| 13 | +} |
| 14 | + |
| 15 | +interface UseCreateEvaluationMutationParams extends DefaultQueryParams { |
| 16 | + closeOnSuccess: () => void; |
| 17 | +} |
| 18 | + |
| 19 | +interface CreateMutationParams extends DefaultQueryParams { |
| 20 | + evaluator: string; |
| 21 | + score: number; |
| 22 | + content: string; |
| 23 | +} |
| 24 | + |
| 25 | +function useEvaluationQuery({ processId, applicantId }: DefaultQueryParams) { |
| 26 | + return useQuery<{ evaluations: EvaluationResult[] }>({ |
| 27 | + queryKey: [QUERY_KEYS.EVALUATION, processId, applicantId], |
| 28 | + queryFn: () => evaluationApis.get({ processId, applicantId }), |
| 29 | + enabled: !!processId && !!applicantId, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function useEvaluationQueryInvalidation() { |
| 34 | + const queryClient = useQueryClient(); |
| 35 | + const { dashboardId, applyFormId } = useParams() as { dashboardId: string; applyFormId: string }; |
| 36 | + |
| 37 | + return (processId: number, applicantId: number) => { |
| 38 | + queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.EVALUATION, processId, applicantId] }); |
| 39 | + queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.DASHBOARD, dashboardId, applyFormId] }); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export const evaluationQueries = { |
| 44 | + useGetEvaluations: ({ processId, applicantId }: DefaultQueryParams) => { |
| 45 | + const { data, error, isLoading } = useEvaluationQuery({ processId, applicantId }); |
| 46 | + |
| 47 | + const evaluations = data?.evaluations || []; |
| 48 | + |
| 49 | + const evaluationList: EvaluationResult[] = evaluations.map((e) => ({ |
| 50 | + evaluationId: e.evaluationId, |
| 51 | + evaluator: e.evaluator ?? undefined, |
| 52 | + score: e.score, |
| 53 | + content: e.content, |
| 54 | + createdDate: e.createdDate ?? undefined, |
| 55 | + })); |
| 56 | + |
| 57 | + return { |
| 58 | + evaluations, |
| 59 | + evaluationList, |
| 60 | + error, |
| 61 | + isLoading, |
| 62 | + }; |
| 63 | + }, |
| 64 | +}; |
| 65 | + |
| 66 | +export const evaluationMutations = { |
| 67 | + useCreateEvaluation: ({ processId, applicantId, closeOnSuccess }: UseCreateEvaluationMutationParams) => { |
| 68 | + const invalidateQueries = useEvaluationQueryInvalidation(); |
| 69 | + /** |
| 70 | + * 지원자 상세 모달이 <dialog>로 구현되어, 모달이 toast 메시지를 덮는 문제가 남아있습니다. |
| 71 | + * 모달이 <div> 기반으로 재구현될 때까지 toast 메시지 코드를 주석 처리합니다. |
| 72 | + * - 25/01/08 아르 |
| 73 | + */ |
| 74 | + // const toast = useToast(); |
| 75 | + |
| 76 | + return useMutation({ |
| 77 | + mutationFn: (params: CreateMutationParams) => evaluationApis.create(params), |
| 78 | + onSuccess: () => { |
| 79 | + invalidateQueries(processId, applicantId); |
| 80 | + closeOnSuccess(); |
| 81 | + // toast.success('지원자에 대한 평가가 등록되었습니다.'); |
| 82 | + }, |
| 83 | + }); |
| 84 | + }, |
| 85 | + |
| 86 | + useDeleteEvaluation: ({ processId, applicantId }: DefaultQueryParams) => { |
| 87 | + const invalidateQueries = useEvaluationQueryInvalidation(); |
| 88 | + /** |
| 89 | + * 지원자 상세 모달이 <dialog>로 구현되어, 모달이 toast 메시지를 덮는 문제가 남아있습니다. |
| 90 | + * 모달이 <div> 기반으로 재구현될 때까지 toast 메시지 코드를 주석 처리합니다. |
| 91 | + * - 25/01/08 아르 |
| 92 | + */ |
| 93 | + // const toast = useToast(); |
| 94 | + |
| 95 | + return useMutation({ |
| 96 | + mutationFn: ({ evaluationId }: { evaluationId: number }) => evaluationApis.delete({ evaluationId }), |
| 97 | + onSuccess: () => { |
| 98 | + invalidateQueries(processId, applicantId); |
| 99 | + // toast.success('지원자에 대한 해당 평가가 삭제되었습니다.'); |
| 100 | + }, |
| 101 | + }); |
| 102 | + }, |
| 103 | +}; |
0 commit comments