@@ -4,12 +4,15 @@ import {
44 approveApplicant ,
55 rejectApplicant ,
66 UserProfile ,
7+ getMyApply ,
8+ postApply ,
79} from '../api/applicants' ;
10+ import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
811
912export function useApplicants ( projectId ?: number ) {
1013 const [ list , setList ] = useState < UserProfile [ ] > ( [ ] ) ;
1114 const [ loading , setLoading ] = useState ( false ) ;
12- const [ error , setError ] = useState < string | null > ( null ) ;
15+ const [ error , setError ] = useState < string | null > ( null ) ;
1316
1417 const fetch = useCallback ( async ( ) => {
1518 if ( projectId == null ) return ;
@@ -25,29 +28,63 @@ export function useApplicants(projectId?: number) {
2528 }
2629 } , [ projectId ] ) ;
2730
28- useEffect ( ( ) => { fetch ( ) ; } , [ fetch ] ) ;
31+ useEffect ( ( ) => {
32+ fetch ( ) ;
33+ } , [ fetch ] ) ;
2934
30- const approve = useCallback ( async ( userId : string ) => {
31- if ( projectId == null ) return ;
32- setList ( prev => prev . filter ( u => u . id !== userId ) ) ;
33- try {
34- await approveApplicant ( projectId , userId ) ;
35- } finally {
36- fetch ( ) ;
37- }
38- } , [ projectId , fetch ] ) ;
35+ const approve = useCallback (
36+ async ( userId : string ) => {
37+ if ( projectId == null ) return ;
38+ setList ( ( prev ) => prev . filter ( ( u ) => u . id !== userId ) ) ;
39+ try {
40+ await approveApplicant ( projectId , userId ) ;
41+ } finally {
42+ fetch ( ) ;
43+ }
44+ } ,
45+ [ projectId , fetch ] ,
46+ ) ;
3947
40- const reject = useCallback ( async ( userId : string ) => {
41- if ( projectId == null ) return ;
42- setList ( prev => prev . filter ( u => u . id !== userId ) ) ;
43- try {
44- await rejectApplicant ( projectId , userId ) ;
45- } finally {
46- fetch ( ) ;
47- }
48- } , [ projectId , fetch ] ) ;
48+ const reject = useCallback (
49+ async ( userId : string ) => {
50+ if ( projectId == null ) return ;
51+ setList ( ( prev ) => prev . filter ( ( u ) => u . id !== userId ) ) ;
52+ try {
53+ await rejectApplicant ( projectId , userId ) ;
54+ } finally {
55+ fetch ( ) ;
56+ }
57+ } ,
58+ [ projectId , fetch ] ,
59+ ) ;
4960
5061 return { applicants : list , loading, error, refetch : fetch , approve, reject } ;
5162}
5263
5364export default useApplicants ;
65+
66+ // 프로젝트 지원
67+
68+ export const usePostApply = ( projectId : number ) => {
69+ const queryClient = useQueryClient ( ) ;
70+ return useMutation ( {
71+ mutationFn : ( position : string ) => postApply ( projectId , position ) ,
72+ onSuccess : ( ) => {
73+ queryClient . invalidateQueries ( { queryKey : [ 'postApply' , projectId ] } ) ;
74+ } ,
75+ onError : ( error ) => {
76+ console . error ( 'Error Apply:' , error ) ;
77+ } ,
78+ } ) ;
79+ } ;
80+
81+ // 내가 지원한 프로젝트 조회
82+
83+ export const useGetMyApply = ( status ?: string ) => {
84+ return useQuery ( {
85+ queryKey : [ 'getMyApply' , status ] ,
86+ queryFn : ( ) => getMyApply ( status ) ,
87+ staleTime : 1000 * 60 * 5 , // 5분 동안 캐시 유지
88+ refetchOnWindowFocus : false , // 윈도우 포커스 시 재요청하지 않음
89+ } ) ;
90+ } ;
0 commit comments