diff --git a/src/_apis/gathering/gathering-detail-apis.ts b/src/_apis/gathering/gathering-detail-apis.ts index 3830233a..85b82def 100644 --- a/src/_apis/gathering/gathering-detail-apis.ts +++ b/src/_apis/gathering/gathering-detail-apis.ts @@ -1,5 +1,5 @@ import { fetchApi } from '@/src/utils/api'; -import { GatheringDetailType } from '@/src/types/gathering-data'; +import { CreateGatheringRequestTypes, GatheringDetailType } from '@/src/types/gathering-data'; // NOTE: 약속 디테일 불러오기 export async function GetGatheringDetail( @@ -52,3 +52,18 @@ export async function LeaveGathering(crewId: number, gatheringId: number): Promi }, }); } + +export async function createGathering(id: number, data: CreateGatheringRequestTypes) { + try { + await fetchApi(`/api/crews/${id}/gatherings`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', // Include authentication credentials + body: JSON.stringify(data), + }); + } catch (error) { + throw error; + } +} diff --git a/src/_queries/gathering/gathering-detail-queries.ts b/src/_queries/gathering/gathering-detail-queries.ts index 293c2aac..e7e29cc5 100644 --- a/src/_queries/gathering/gathering-detail-queries.ts +++ b/src/_queries/gathering/gathering-detail-queries.ts @@ -1,5 +1,7 @@ -import { useQuery } from '@tanstack/react-query'; -import { GetGatheringDetail } from '@/src/_apis/gathering/gathering-detail-apis'; +import { toast } from 'react-toastify'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { GetGatheringDetail, createGathering } from '@/src/_apis/gathering/gathering-detail-apis'; +import { CreateGatheringRequestTypes } from '@/src/types/gathering-data'; export function useGetGatheringDetailQuery(crewId: number, gatheringId: number) { return useQuery({ @@ -7,3 +9,21 @@ export function useGetGatheringDetailQuery(crewId: number, gatheringId: number) queryFn: () => GetGatheringDetail(crewId, gatheringId), }); } + +export function useCreateGatheringQuery(crewId: number) { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: (data: CreateGatheringRequestTypes) => createGathering(crewId, data), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ['gatheringList', crewId], + refetchType: 'all', + }); + toast.success('크루가 생성되었습니다.'); + }, + onError: (error) => { + toast.error(error.message); + }, + }); +} diff --git a/src/app/(crew)/crew/create/page.tsx b/src/app/(crew)/crew/create/page.tsx index 25dcc84d..c1f34671 100644 --- a/src/app/(crew)/crew/create/page.tsx +++ b/src/app/(crew)/crew/create/page.tsx @@ -2,7 +2,6 @@ import Image from 'next/image'; import { Loader } from '@mantine/core'; -import { getImageUrl } from '@/src/_apis/image/get-image-url'; import { useCreateCrewQuery } from '@/src/_queries/crew/crew-detail-queries'; import CreateCrewForm from '@/src/app/(crew)/crew/create/_components/create-crew-form'; import { CreateCrewFormTypes, CreateCrewRequestTypes } from '@/src/types/create-crew'; diff --git a/src/app/(crew)/crew/detail/[id]/_components/create-gathering/create-gathering-form/index.tsx b/src/app/(crew)/crew/detail/[id]/_components/create-gathering/create-gathering-form/index.tsx index 4028a52d..2e037d86 100644 --- a/src/app/(crew)/crew/detail/[id]/_components/create-gathering/create-gathering-form/index.tsx +++ b/src/app/(crew)/crew/detail/[id]/_components/create-gathering/create-gathering-form/index.tsx @@ -2,6 +2,7 @@ import { Controller, useForm, useWatch } from 'react-hook-form'; import { NumberInput } from '@mantine/core'; +import { getImageUrl } from '@/src/_apis/image/get-image-url'; import Button from '@/src/components/common/input/button'; import DateTimePicker from '@/src/components/common/input/date-time-picker'; import FileInputWrap from '@/src/components/common/input/file-input-wrap'; @@ -38,6 +39,16 @@ export default function CreateGatheringForm({ const location = useWatch({ control, name: 'location' }); const introduce = useWatch({ control, name: 'introduce' }); + const handleFileChange = async ( + file: File | string | null, + onChange: (value: string | File) => void, + ) => { + if (file instanceof File) { + const imgResponse = await getImageUrl(file, 'CREW'); + onChange(imgResponse?.imageUrl || ''); + } + }; + return (