onsitesurvey/src/components/popup/SurveySaleSubmitPopup.tsx
keyy1315 17d306bb11 fix: change submit data set logic
- 제출 폼 데이터 삽입 안되는 오류 해결
- api error 메세지 설정
- api 응답 객체 변경
2025-06-17 13:20:18 +09:00

284 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Image from 'next/image'
import { usePopupController } from '@/store/popupController'
import { useParams } from 'next/navigation'
import { useSurvey } from '@/hooks/useSurvey'
import { useEffect, useRef, useState } from 'react'
import { useSessionStore } from '@/store/session'
import { useCommCode } from '@/hooks/useCommCode'
import { CommCode } from '@/types/CommCode'
import { sendEmail } from '@/libs/mailer'
import { useSpinnerStore } from '@/store/spinnerStore'
interface SubmitFormData {
saleBase: string | null
targetId: string | null
targetNm: string | null
sender: string
receiver: string[] | string
reference: string[] | null
title: string
contents: string | null
}
interface FormField {
id: keyof SubmitFormData
name: string
required: boolean
}
export default function SurveySaleSubmitPopup() {
const popupController = usePopupController()
const { session } = useSessionStore()
const params = useParams()
const routeId = params.id
const { setIsShow } = useSpinnerStore()
const { getCommCode } = useCommCode()
const { surveyDetail, getSubmitTarget } = useSurvey(Number(routeId))
const [submitData, setSubmitData] = useState<SubmitFormData>({
saleBase: null,
targetId: null,
targetNm: null,
sender: '',
receiver: [],
reference: null,
title: '',
contents: '',
})
const [commCodeList, setCommCodeList] = useState<CommCode[]>([])
/** 제출 타겟 데이터 조회 및 제출 폼 데이터 삽입 */
useEffect(() => {
if (!session?.isLoggedIn || !surveyDetail?.id) return
const baseUpdate = {
sender: session?.email ?? '',
title: '[HANASYS現地調査] 調査物件が提出. (' + surveyDetail?.srlNo + ')',
}
if (session?.role === 'Admin') {
getCommCode('SALES_OFFICE_CD').then((codes) => {
setCommCodeList(codes)
})
setSubmitData((prev) => ({
...prev,
...baseUpdate,
}))
} else if (session?.role === 'Builder' || session?.role === 'Admin_Sub') {
getSubmitTarget({ storeId: surveyDetail?.storeId ?? '', role: session?.role ?? '' }).then((data) => {
if (!data) return
if (data && data.length > 0) {
const updateData: Partial<SubmitFormData> = {
sender: session?.email ?? '',
title: '[HANASYS現地調査] 調査物件が提出. (' + surveyDetail?.srlNo + ')',
targetId: data[0]?.targetStoreId ?? '',
targetNm: data[0]?.targetStoreNm ?? '',
receiver: data.filter((item) => item.auth === 'S').map((item) => item.repUserEmail),
reference: data.filter((item) => item.auth === 'N').map((item) => item.repUserEmail),
saleBase: null,
contents: '',
}
setSubmitData((prev) => ({
...prev,
...updateData,
}))
}
})
}
}, [session, surveyDetail])
const FORM_FIELDS: FormField[] = [
{ id: 'sender', name: '発送者', required: true },
{ id: 'saleBase', name: '提出地点選択', required: session?.role === 'Admin' },
{ id: 'targetNm', name: '提出販売店', required: session?.role !== 'Admin' },
{ id: 'receiver', name: '受信者', required: true },
{ id: 'reference', name: '参考', required: false },
{ id: 'title', name: 'タイトル', required: true },
{ id: 'contents', name: '内容', required: false },
]
const { submitSurvey, isSubmittingSurvey } = useSurvey(Number(routeId))
const handleInputChange = (field: keyof SubmitFormData, value: string) => {
setSubmitData((prev) => ({ ...prev, [field]: value }))
}
/** 필수값 검증 */
const validateData = (data: SubmitFormData): boolean => {
const requiredFields = FORM_FIELDS.filter((field) => field.required)
for (const field of requiredFields) {
if (data[field.id] === '' || data[field.id] === null || data[field.id]?.length === 0) {
alert(`${field.name}は必須入力項目です。`)
const element = document.getElementById(field.id)
if (element) {
element.focus()
}
return false
}
}
return true
}
/** 제출 처리 - 데이터 검증 이후 메일 전송 완료되면 데이터 저장 */
const handleSubmit = () => {
if (validateData(submitData)) {
window.neoConfirm('送信しますか? 送信後は変更・修正することはできません。', () => {
setIsShow(true)
sendEmail({
to: submitData.receiver,
cc: submitData.reference ?? '',
subject: submitData.title,
content: contentsRef.current?.innerHTML ?? '',
})
.then(() => {
if (!isSubmittingSurvey) {
alert('提出が完了しました。')
submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm })
popupController.setSurveySaleSubmitPopup(false)
}
})
.catch((error) => {
console.error('Error sending email:', error)
alert('メール送信に失敗しました。 再度送信してください。')
})
.finally(() => {
setIsShow(false)
popupController.setSurveySaleSubmitPopup(false)
})
})
}
}
const contentsRef = useRef<HTMLDivElement>(null)
const handleClose = () => {
popupController.setSurveySaleSubmitPopup(false)
}
/** 권한 별 폼 필드 렌더링 */
const renderFormField = (field: FormField) => {
const isReadOnly = false
if (field.id === 'saleBase' && session?.role !== 'Admin') {
return null
}
if (field.id === 'targetNm' && session?.role === 'Admin') {
return null
}
return (
<div className="data-input-form-bx" key={field.id}>
<div className="data-input-form-tit">
{field.name} {field.required && <i className="import">*</i>}
</div>
<div className="data-input">
{field.id === 'contents' ? (
<div className="submit-content" id={field.id}>
<div ref={contentsRef}>
<p style={{ fontSize: '13px', fontWeight: '400', color: '#2e3a59', marginBottom: '15px' }}>
HANASYS現地調査アプリを使用した現地調査結果が送信されました
</p>
<p style={{ fontSize: '13px', fontWeight: '400', color: '#2e3a59', marginBottom: '3px' }}>
- <span style={{ color: '#417DDC' }}>{surveyDetail?.representative}</span>
</p>
<p style={{ fontSize: '13px', fontWeight: '400', color: '#2e3a59', marginBottom: '3px' }}>
-
<span style={{ color: '#417DDC' }}>
{surveyDetail?.store} ({surveyDetail?.storeId})
</span>
</p>
<p style={{ fontSize: '13px', fontWeight: '400', color: '#2e3a59', marginBottom: '15px' }}>
-:
<span style={{ color: '#417DDC' }}>{surveyDetail?.constructionPoint}</span>
</p>
<p>
<a
style={{ fontSize: '13px', fontWeight: '400', color: '#1259CB', marginBottom: '5px', textDecoration: 'underline' }}
href={`${process.env.NEXT_PUBLIC_API_URL}/pdf/survey-sale/${surveyDetail?.id}`}
target="_blank"
>
調PDFダウンロード
</a>
</p>
<p style={{ fontSize: '13px', fontWeight: '400', color: '#2e3a59' }}>
調PDFをダウンロードできます
</p>
</div>
</div>
) : (
<>
{field.id === 'saleBase' && session?.role === 'Admin' ? (
<select
className="select-form"
id={field.id}
value={submitData[field.id] ?? ''}
onChange={(e) => {
const selectedOffice = commCodeList.find((item) => item.code === e.target.value)
if (selectedOffice) {
//@ts-ignore
const receiver = selectedOffice.REF_CHR1.split(';')
setSubmitData((prev) => ({ ...prev, receiver: receiver, saleBase: e.target.value }))
}
}}
>
<option value=""></option>
{commCodeList.map((item) => (
<option key={item.code} value={item.code}>
{item.codeJp}
</option>
))}
</select>
) : (
<input
className="input-frame"
type="text"
id={field.id}
value={submitData[field.id] ?? ''}
onChange={(e) => handleInputChange(field.id, e.target.value)}
readOnly={isReadOnly}
/>
)}
</>
)}
</div>
</div>
)
}
return (
<div className="modal-popup">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<div className="modal-header-inner">
<div className="modal-name-wrap">
<div className="modal-img">
<Image src="/assets/images/layout/modal_header_icon04.svg" width={19} height={22} alt="" />
</div>
<div className="modal-name">調</div>
</div>
<button className="modal-close" onClick={handleClose}></button>
</div>
</div>
<div className="modal-body">
<div className="data-form-wrap">{FORM_FIELDS.map(renderFormField)}</div>
<div className="btn-flex-wrap">
<div className="btn-bx">
<button className="btn-frame n-blue icon" onClick={handleClose}>
<i className="btn-arr"></i>
</button>
</div>
<div className="btn-bx">
<button className="btn-frame red icon" onClick={handleSubmit} disabled={isSubmittingSurvey}>
<i className="btn-arr"></i>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
)
}