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 } }) {
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||||
const { id } = await context.params
|
const { id } = await context.params
|
||||||
const body = await request.json()
|
const body = await request.json()
|
||||||
// @ts-ignore
|
try {
|
||||||
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
// @ts-ignore
|
||||||
where: { id: Number(id) },
|
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
||||||
data: {
|
where: { id: Number(id) },
|
||||||
...body,
|
data: {
|
||||||
detail_info: {
|
...body,
|
||||||
update: body.detail_info,
|
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
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 } }) {
|
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' })
|
return NextResponse.json({ message: 'Survey deleted successfully' })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
return NextResponse.json({ message: 'Survey deletion failed' }, { status: 500 })
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
import { useServey } from '@/hooks/useSurvey'
|
||||||
import { SurveyBasicRequest } from '@/types/Survey'
|
import { SurveyBasicRequest } from '@/types/Survey'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter, useSearchParams } from 'next/navigation'
|
||||||
import { useState } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
|
|
||||||
const defaultBasicInfoForm: SurveyBasicRequest = {
|
const defaultBasicInfoForm: SurveyBasicRequest = {
|
||||||
representative: '',
|
representative: '',
|
||||||
@ -19,34 +20,72 @@ const defaultBasicInfoForm: SurveyBasicRequest = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function BasicForm() {
|
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)
|
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) => {
|
const handleChange = (key: keyof SurveyBasicRequest, value: string) => {
|
||||||
setBasicInfoData({ ...basicInfoData, [key]: value })
|
setBasicInfoData({ ...basicInfoData, [key]: value })
|
||||||
}
|
}
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const handleSave = () => {
|
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 <div>Loading...</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="sale-frame">
|
<div className="sale-frame">
|
||||||
<div className="data-form-wrap">
|
<div className="data-form-wrap">
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">担当者名</div>
|
<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>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">販売店</div>
|
<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>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">施工店</div>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -55,29 +94,60 @@ export default function BasicForm() {
|
|||||||
<div className="data-form-wrap">
|
<div className="data-form-wrap">
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">現地調査日</div>
|
<div className="data-input-form-tit">現地調査日</div>
|
||||||
|
{/* TODO: 달력 라이브러리 추가 ?? */}
|
||||||
<div className="date-input">
|
<div className="date-input">
|
||||||
<button className="date-btn">
|
<button className="date-btn">
|
||||||
<i className="date-icon"></i>
|
<i className="date-icon"></i>
|
||||||
</button>
|
</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>
|
</div>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">建物名</div>
|
<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>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">顧客名</div>
|
<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>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">建物の住所</div>
|
<div className="data-input-form-tit">建物の住所</div>
|
||||||
<div className="form-flex">
|
<div className="form-flex">
|
||||||
<div className="form-bx">
|
<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>
|
||||||
<div className="form-bx">
|
<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>
|
<option value="">東京都</option>
|
||||||
<option value="">東京都</option>
|
<option value="">東京都</option>
|
||||||
@ -94,7 +164,13 @@ export default function BasicForm() {
|
|||||||
</div>
|
</div>
|
||||||
<div className="data-input-form-bx">
|
<div className="data-input-form-bx">
|
||||||
<div className="data-input-form-tit">市区町村名, 以後の住所</div>
|
<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>
|
</div>
|
||||||
<div className="btn-flex-wrap">
|
<div className="btn-flex-wrap">
|
||||||
@ -104,7 +180,7 @@ export default function BasicForm() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="btn-bx">
|
<div className="btn-bx">
|
||||||
<button className="btn-frame red icon" onClick={handleDelete}>
|
<button className="btn-frame red icon">
|
||||||
保存<i className="btn-arr"></i>
|
保存<i className="btn-arr"></i>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { useRouter } from 'next/navigation'
|
|||||||
export default function ListTable() {
|
export default function ListTable() {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { surveyList, isLoadingSurveyList } = useServey()
|
const { surveyList, isLoadingSurveyList } = useServey()
|
||||||
|
|
||||||
const [hasMore, setHasMore] = useState(surveyList.length > 5)
|
const [hasMore, setHasMore] = useState(surveyList.length > 5)
|
||||||
const [visibleItems, setVisibleItems] = useState(5)
|
const [visibleItems, setVisibleItems] = useState(5)
|
||||||
|
|
||||||
@ -24,6 +25,9 @@ export default function ListTable() {
|
|||||||
const handleDetail = (id: number) => {
|
const handleDetail = (id: number) => {
|
||||||
router.push(`/survey-sale/${id}`)
|
router.push(`/survey-sale/${id}`)
|
||||||
}
|
}
|
||||||
|
if (isLoadingSurveyList) {
|
||||||
|
return <div>Loading...</div>
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@ -44,12 +44,14 @@ export function useServey(id?: number): {
|
|||||||
},
|
},
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
queryClient.invalidateQueries({ queryKey: ['survey', 'list'] })
|
queryClient.invalidateQueries({ queryKey: ['survey', 'list'] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['survey', id] })
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const { mutate: updateSurvey, isPending: isUpdatingSurvey } = useMutation({
|
const { mutate: updateSurvey, isPending: isUpdatingSurvey } = useMutation({
|
||||||
mutationFn: async (survey: SurveyBasicRequest) => {
|
mutationFn: async (survey: SurveyBasicRequest) => {
|
||||||
|
console.log('updateSurvey:: ', survey)
|
||||||
if (id === undefined) throw new Error('id is required')
|
if (id === undefined) throw new Error('id is required')
|
||||||
const resp = await axiosInstance.put<SurveyBasicInfo>(`/api/survey-sales/${id}`, survey)
|
const resp = await axiosInstance.put<SurveyBasicInfo>(`/api/survey-sales/${id}`, survey)
|
||||||
return resp.data
|
return resp.data
|
||||||
|
|||||||
@ -107,5 +107,4 @@ export type SurveyDetailRequest = {
|
|||||||
installation_availability: number | null
|
installation_availability: number | null
|
||||||
installation_availability_etc: string | null
|
installation_availability_etc: string | null
|
||||||
memo: string | null
|
memo: string | null
|
||||||
basic_info_id: number
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user