From c9812d1159d4277f02be77484021185091b23c07 Mon Sep 17 00:00:00 2001 From: keyy1315 Date: Fri, 2 May 2025 18:05:15 +0900 Subject: [PATCH] feat: implement survey basic information update function --- src/app/api/survey-sales/[id]/route.ts | 24 ++-- .../survey-sale/detail/BasicForm.tsx | 106 +++++++++++++++--- src/components/survey-sale/list/ListTable.tsx | 4 + src/hooks/useSurvey.ts | 2 + src/types/Survey.ts | 1 - 5 files changed, 110 insertions(+), 27 deletions(-) diff --git a/src/app/api/survey-sales/[id]/route.ts b/src/app/api/survey-sales/[id]/route.ts index 6010be1..288a8b6 100644 --- a/src/app/api/survey-sales/[id]/route.ts +++ b/src/app/api/survey-sales/[id]/route.ts @@ -31,17 +31,19 @@ export async function GET(request: Request, context: { params: { id: string } }) export async function PUT(request: Request, context: { params: { id: string } }) { const { id } = await context.params const body = await request.json() - // @ts-ignore - const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({ - where: { id: Number(id) }, - data: { - ...body, - detail_info: { - update: body.detail_info, + try { + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({ + where: { id: Number(id) }, + data: { + ...body, }, - }, - }) - return NextResponse.json(survey) + }) + return NextResponse.json(survey) + } catch (error) { + console.error(error) + throw error + } } export async function DELETE(request: Request, context: { params: { id: string } }) { @@ -72,7 +74,7 @@ export async function DELETE(request: Request, context: { params: { id: string } return NextResponse.json({ message: 'Survey deleted successfully' }) } catch (error) { console.error(error) - return NextResponse.json({ message: 'Survey deletion failed' }, { status: 500 }) + throw error } } diff --git a/src/components/survey-sale/detail/BasicForm.tsx b/src/components/survey-sale/detail/BasicForm.tsx index 744ee47..2f74673 100644 --- a/src/components/survey-sale/detail/BasicForm.tsx +++ b/src/components/survey-sale/detail/BasicForm.tsx @@ -1,8 +1,9 @@ 'use client' +import { useServey } from '@/hooks/useSurvey' import { SurveyBasicRequest } from '@/types/Survey' -import { useRouter } from 'next/navigation' -import { useState } from 'react' +import { useRouter, useSearchParams } from 'next/navigation' +import { useState, useEffect } from 'react' const defaultBasicInfoForm: SurveyBasicRequest = { representative: '', @@ -19,34 +20,72 @@ const defaultBasicInfoForm: SurveyBasicRequest = { } export default function BasicForm() { + const searchParams = useSearchParams() + const id = searchParams.get('id') + + const { surveyDetail, createSurvey, isCreatingSurvey, updateSurvey, isUpdatingSurvey } = useServey(Number(id)) + const [basicInfoData, setBasicInfoData] = useState(defaultBasicInfoForm) + useEffect(() => { + if (surveyDetail) { + const { id, updated_at, created_at, detail_info, ...rest } = surveyDetail + setBasicInfoData(rest) + } + }, [surveyDetail]) + const handleChange = (key: keyof SurveyBasicRequest, value: string) => { setBasicInfoData({ ...basicInfoData, [key]: value }) } const router = useRouter() const handleSave = () => { - console.log('save') + if (id) { + console.log('basicInfoData:: ', basicInfoData) + updateSurvey(basicInfoData) + } else { + createSurvey(basicInfoData) + } + router.push('/survey-sale') } - const handleDelete = () => { - console.log('delete') + + if (isCreatingSurvey || isUpdatingSurvey) { + return
Loading...
} + return ( <>
担当者名
- handleChange('representative', e.target.value)} /> + handleChange('representative', e.target.value)} + />
販売店
- handleChange('store', e.target.value)} /> + handleChange('store', e.target.value)} + />
施工店
- handleChange('construction_point', e.target.value)} /> + handleChange('construction_point', e.target.value)} + />
@@ -55,29 +94,60 @@ export default function BasicForm() {
現地調査日
+ {/* TODO: 달력 라이브러리 추가 ?? */}
- handleChange('investigation_date', e.target.value)} /> + handleChange('investigation_date', e.target.value)} + />
建物名
- handleChange('building_name', e.target.value)} /> + handleChange('building_name', e.target.value)} + />
顧客名
- handleChange('customer_name', e.target.value)} /> + handleChange('customer_name', e.target.value)} + />
建物の住所
- + handleChange('post_code', e.target.value)} + />
- handleChange('address', e.target.value)} + > @@ -94,7 +164,13 @@ export default function BasicForm() {
市区町村名, 以後の住所
- + handleChange('address_detail', e.target.value)} + />
@@ -104,7 +180,7 @@ export default function BasicForm() {
-
diff --git a/src/components/survey-sale/list/ListTable.tsx b/src/components/survey-sale/list/ListTable.tsx index fcf7769..a9cfd34 100644 --- a/src/components/survey-sale/list/ListTable.tsx +++ b/src/components/survey-sale/list/ListTable.tsx @@ -8,6 +8,7 @@ import { useRouter } from 'next/navigation' export default function ListTable() { const router = useRouter() const { surveyList, isLoadingSurveyList } = useServey() + const [hasMore, setHasMore] = useState(surveyList.length > 5) const [visibleItems, setVisibleItems] = useState(5) @@ -24,6 +25,9 @@ export default function ListTable() { const handleDetail = (id: number) => { router.push(`/survey-sale/${id}`) } + if (isLoadingSurveyList) { + return
Loading...
+ } return ( <> diff --git a/src/hooks/useSurvey.ts b/src/hooks/useSurvey.ts index e8cc428..1e56750 100644 --- a/src/hooks/useSurvey.ts +++ b/src/hooks/useSurvey.ts @@ -44,12 +44,14 @@ export function useServey(id?: number): { }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ['survey', 'list'] }) + queryClient.invalidateQueries({ queryKey: ['survey', id] }) return data }, }) const { mutate: updateSurvey, isPending: isUpdatingSurvey } = useMutation({ mutationFn: async (survey: SurveyBasicRequest) => { + console.log('updateSurvey:: ', survey) if (id === undefined) throw new Error('id is required') const resp = await axiosInstance.put(`/api/survey-sales/${id}`, survey) return resp.data diff --git a/src/types/Survey.ts b/src/types/Survey.ts index aa7d61e..39dcc01 100644 --- a/src/types/Survey.ts +++ b/src/types/Survey.ts @@ -107,5 +107,4 @@ export type SurveyDetailRequest = { installation_availability: number | null installation_availability_etc: string | null memo: string | null - basic_info_id: number }