feat: implement survey basic information update function
This commit is contained in:
parent
6777fae68c
commit
c9812d1159
@ -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()
|
||||
try {
|
||||
// @ts-ignore
|
||||
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
||||
where: { id: Number(id) },
|
||||
data: {
|
||||
...body,
|
||||
detail_info: {
|
||||
update: body.detail_info,
|
||||
},
|
||||
},
|
||||
})
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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<SurveyBasicRequest>(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)
|
||||
}
|
||||
const handleDelete = () => {
|
||||
console.log('delete')
|
||||
router.push('/survey-sale')
|
||||
}
|
||||
|
||||
if (isCreatingSurvey || isUpdatingSurvey) {
|
||||
return <div>Loading...</div>
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="sale-frame">
|
||||
<div className="data-form-wrap">
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">担当者名</div>
|
||||
<input type="text" className="input-frame" id='representative' value={basicInfoData.representative} onChange={(e) => handleChange('representative', e.target.value)} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="representative"
|
||||
value={basicInfoData.representative}
|
||||
onChange={(e) => handleChange('representative', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">販売店</div>
|
||||
<input type="text" className="input-frame" id='store' value={basicInfoData.store ?? ''} onChange={(e) => handleChange('store', e.target.value)} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="store"
|
||||
value={basicInfoData.store ?? ''}
|
||||
onChange={(e) => handleChange('store', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">施工店</div>
|
||||
<input type="text" className="input-frame" id='construction_point' value={basicInfoData.construction_point ?? ''} onChange={(e) => handleChange('construction_point', e.target.value)} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="construction_point"
|
||||
value={basicInfoData.construction_point ?? ''}
|
||||
onChange={(e) => handleChange('construction_point', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -55,29 +94,60 @@ export default function BasicForm() {
|
||||
<div className="data-form-wrap">
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">現地調査日</div>
|
||||
{/* TODO: 달력 라이브러리 추가 ?? */}
|
||||
<div className="date-input">
|
||||
<button className="date-btn">
|
||||
<i className="date-icon"></i>
|
||||
</button>
|
||||
<input type="date" className="date-frame" id='investigation_date' value={basicInfoData.investigation_date ?? ''} onChange={(e) => handleChange('investigation_date', e.target.value)} />
|
||||
<input
|
||||
type="date"
|
||||
className="date-frame"
|
||||
id="investigation_date"
|
||||
value={basicInfoData.investigation_date ?? ''}
|
||||
onChange={(e) => handleChange('investigation_date', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">建物名</div>
|
||||
<input type="text" className="input-frame" id='building_name' value={basicInfoData.building_name ?? ''} onChange={(e) => handleChange('building_name', e.target.value)} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="building_name"
|
||||
value={basicInfoData.building_name ?? ''}
|
||||
onChange={(e) => handleChange('building_name', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">顧客名</div>
|
||||
<input type="text" className="input-frame" id='customer_name' value={basicInfoData.customer_name ?? ''} onChange={(e) => handleChange('customer_name', e.target.value)} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="customer_name"
|
||||
value={basicInfoData.customer_name ?? ''}
|
||||
onChange={(e) => handleChange('customer_name', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">建物の住所</div>
|
||||
<div className="form-flex">
|
||||
<div className="form-bx">
|
||||
<input type="text" className="input-frame" defaultValue={'1050013'} disabled />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="post_code"
|
||||
value={basicInfoData.post_code ?? ''}
|
||||
onChange={(e) => handleChange('post_code', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="form-bx">
|
||||
<select className="select-form" name="" id="">
|
||||
<select
|
||||
className="select-form"
|
||||
name="address"
|
||||
id="address"
|
||||
value={basicInfoData.address ?? ''}
|
||||
onChange={(e) => handleChange('address', e.target.value)}
|
||||
>
|
||||
<option value="">東京都</option>
|
||||
<option value="">東京都</option>
|
||||
<option value="">東京都</option>
|
||||
@ -94,7 +164,13 @@ export default function BasicForm() {
|
||||
</div>
|
||||
<div className="data-input-form-bx">
|
||||
<div className="data-input-form-tit">市区町村名, 以後の住所</div>
|
||||
<input type="text" className="input-frame" defaultValue={'浜松 浜松町'} />
|
||||
<input
|
||||
type="text"
|
||||
className="input-frame"
|
||||
id="address_detail"
|
||||
value={basicInfoData.address_detail ?? ''}
|
||||
onChange={(e) => handleChange('address_detail', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="btn-flex-wrap">
|
||||
@ -104,7 +180,7 @@ export default function BasicForm() {
|
||||
</button>
|
||||
</div>
|
||||
<div className="btn-bx">
|
||||
<button className="btn-frame red icon" onClick={handleDelete}>
|
||||
<button className="btn-frame red icon">
|
||||
保存<i className="btn-arr"></i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@ -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 <div>Loading...</div>
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@ -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<SurveyBasicInfo>(`/api/survey-sales/${id}`, survey)
|
||||
return resp.data
|
||||
|
||||
@ -107,5 +107,4 @@ export type SurveyDetailRequest = {
|
||||
installation_availability: number | null
|
||||
installation_availability_etc: string | null
|
||||
memo: string | null
|
||||
basic_info_id: number
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user