diff --git a/src/components/estimate/popup/EstimateCopyPop.jsx b/src/components/estimate/popup/EstimateCopyPop.jsx index 8e2e4251..39ca5a28 100644 --- a/src/components/estimate/popup/EstimateCopyPop.jsx +++ b/src/components/estimate/popup/EstimateCopyPop.jsx @@ -1,40 +1,33 @@ 'use client' -import { useEffect, useState, useContext, use } from 'react' +import { useEffect, useState, useContext, useRef } from 'react' import { useMessage } from '@/hooks/useMessage' import { useAxios } from '@/hooks/useAxios' -import { useRecoilValue } from 'recoil' -import { floorPlanObjectState } from '@/store/floorPlanObjectAtom' import Select, { components } from 'react-select' import { SessionContext } from '@/app/SessionProvider' -import { isEmptyArray } from '@/util/common-utils' +import { isEmptyArray, isObjectNotEmpty } from '@/util/common-utils' import { useEstimateController } from '@/hooks/floorPlan/estimate/useEstimateController' -import { estimateState } from '@/store/floorPlanObjectAtom' - export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { const { getMessage } = useMessage() - const { get, promisePost } = useAxios() - - const [selOptions, setSelOptions] = useState('') //선택한 1차점 전송할 + const { get } = useAxios() const { handleEstimateCopy, state } = useEstimateController(planNo) const { session } = useContext(SessionContext) - // recoil 물건번호 - const objectRecoil = useRecoilValue(floorPlanObjectState) - const [saleStoreList, setSaleStoreList] = useState([]) // 판매점 리스트 const [favoriteStoreList, setFavoriteStoreList] = useState([]) //즐겨찾기한 판매점목록 const [showSaleStoreList, setShowSaleStoreList] = useState([]) //보여줄 판매점목록 - - const estimateRecoilState = useRecoilValue(estimateState) - const [otherSaleStoreList, setOtherSaleStoreList] = useState([]) const [originOtherSaleStoreList, setOriginOtherSaleStoreList] = useState([]) + const [saleStoreId, setSaleStoreId] = useState('') //선택한 1차점 + const [otherSaleStoreId, setOtherSaleStoreId] = useState('') //선택한 1차점 이외 + const [sendPlanNo, setSendPlanNo] = useState('1') - const [sendCharger, setSendCharger] = useState('') + const [copyReceiveUser, setCopyReceiveUser] = useState('') + + const ref = useRef() //2차점 자동완성 초기화용 useEffect(() => { let url @@ -47,7 +40,8 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { if (session.storeLvl === '1') { url = `/api/object/saleStore/${session?.storeId}/list?firstFlg=1&userId=${session?.userId}` } else { - url = `/api/object/saleStore/${session?.storeId}/list?firstFlg=1&userId=${session?.userId}` + //T01 or 1차점만 복사버튼 노출됨으로 + // url = `/api/object/saleStore/${session?.storeId}/list?firstFlg=1&userId=${session?.userId}` } } @@ -60,21 +54,44 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { }) if (session.storeId === 'T01') { - console.log('T01:::::::::::', res) + firstList = res + //T01을 첫번째로 정렬 + firstList.sort((a, b) => (a.saleStoreId !== 'T01') - (b.saleStoreId !== 'T01') || a.saleStoreId - b.saleStoreId) + favList = firstList.filter((row) => row.saleStoreId === 'T01' || row.priority !== 'B') + + setSaleStoreList(firstList) + setFavoriteStoreList(favList) + setShowSaleStoreList(favList) + setSaleStoreId(session?.storeId) + + // T01때 디폴트로 T01셋팅하고 하위 2차목록 조회하기 바꾸면(onSelectionChange..) 바꾼거로 2차목록 조회하기 + url = `/api/object/saleStore/${session?.storeId}/list?firstFlg=0&userId=${session?.userId}` + get({ url: url }).then((res) => { + if (!isEmptyArray(res)) { + res.map((row) => { + row.value == row.saleStoreId + row.label = row.saleStoreName + }) + otherList = res + setOtherSaleStoreList(otherList) + setOriginOtherSaleStoreList(otherList) + } else { + setOtherSaleStoreList([]) + } + }) } else { if (session.storeLvl === '1') { - console.log('T01아닌 1차점', res) firstList = res favList = res.filter((row) => row.priority !== 'B') otherList = res.filter((row) => row.firstAgentYn === 'N') setSaleStoreList(firstList) setFavoriteStoreList(firstList) setShowSaleStoreList(firstList) - setSelOptions(firstList[0].saleStoreId) + setSaleStoreId(firstList[0].saleStoreId) setOtherSaleStoreList(otherList) } else { - console.log('1차점 아님::::::::', res) + //T01 or 1차점만 복사버튼 노출됨으로 } } } @@ -89,22 +106,73 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { useEffect(() => { if (state?.charger) { - setSendCharger(state.charger) + setCopyReceiveUser(state.charger) } }, [state.charger]) + //T01 1차점 자동완성 인풋때 목록 변환 + const onInputChange = (key) => { + if (key !== '') { + setShowSaleStoreList(saleStoreList) + } else { + setShowSaleStoreList(favoriteStoreList) + } + } + // 1차점 변경 이벤트 const onSelectionChange = (key) => { if (isObjectNotEmpty(key)) { - if (key.saleStoreId === selOptions) { + if (key.saleStoreId === saleStoreId) { return } } if (isObjectNotEmpty(key)) { - console.log('KEY::', key) + setSaleStoreId(key.saleStoreId) + const url = `/api/object/saleStore/${key.saleStoreId}/list?firstFlg=0&userId=${session?.userId}` + let otherList + get({ url: url }).then((res) => { + if (!isEmptyArray(res)) { + res.map((row) => { + row.value = row.saleStoreId + row.label = row.saleStoreName + }) + + otherList = res + setOtherSaleStoreList(otherList) + setOtherSaleStoreId('') + } else { + setOtherSaleStoreId('') + setOtherSaleStoreList([]) + } + }) } else { - console.log('XXXXXXXXXXX') + setSaleStoreId('') + //otherSaleStoreId는 onSelectionChange2에서 초기화됨 + setOtherSaleStoreList(originOtherSaleStoreList) + handleClear() + } + } + + // 2차점 변경 이벤트 + const onSelectionChange2 = (key) => { + if (isObjectNotEmpty(key)) { + if (key.saleStoreId === otherSaleStoreId) { + return + } + } + + if (isObjectNotEmpty(key)) { + setOtherSaleStoreId(key.saleStoreId) + } else { + setOtherSaleStoreId('') + } + } + + //2차점 자동완성 초기화 + const handleClear = () => { + if (ref.current) { + ref.current.clearValue() } } @@ -141,9 +209,19 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { className="react-select-custom" classNamePrefix="custom" placeholder="Select" + options={showSaleStoreList} + onInputChange={onInputChange} + onChange={onSelectionChange} + getOptionLabel={(x) => x.saleStoreName} + getOptionValue={(x) => x.saleStoreId} + isClearable={true} + isDisabled={false} + value={saleStoreList.filter(function (option) { + return option.saleStoreId === saleStoreId + })} /> -
로그인T01 선택한 1차점 아이디
+
{saleStoreId}
)} {session.storeId !== 'T01' && session.storeLvl === '1' && ( @@ -162,32 +240,16 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { isClearable={false} isDisabled={session?.storeLvl !== '1' ? true : session?.storeId !== 'T01' ? true : false} value={showSaleStoreList.filter(function (option) { - return option.saleStoreId === selOptions + return option.saleStoreId === saleStoreId })} /> -
로그인 1차점 선택한 1차점 아이디
- - )} - {session.storeId !== 'T01' && session.storeLvl !== '1' && ( -
-
- x.saleStoreName} + getOptionValue={(x) => x.saleStoreId} + isClearable={true} + isDisabled={otherSaleStoreList.length > 0 ? false : true} + value={otherSaleStoreList.filter(function (option) { + return option.saleStoreId === otherSaleStoreId + })} />
-
선택한 2차점 아이디
+
{otherSaleStoreId}
@@ -209,9 +281,10 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { { - setSendCharger(e.target.value) + setCopyReceiveUser(e.target.value) }} />
@@ -226,7 +299,7 @@ export default function EstimateCopyPop({ planNo, setEstimateCopyPopupOpen }) { type="button" className="btn-origin navy" onClick={() => { - handleEstimateCopy(sendPlanNo, sendCharger, selOptions) + handleEstimateCopy(sendPlanNo, copyReceiveUser, saleStoreId, otherSaleStoreId) }} > {getMessage('estimate.detail.estimateCopyPopup.copyBtn')} diff --git a/src/hooks/floorPlan/estimate/useEstimateController.js b/src/hooks/floorPlan/estimate/useEstimateController.js index 89b4c8f7..e3883125 100644 --- a/src/hooks/floorPlan/estimate/useEstimateController.js +++ b/src/hooks/floorPlan/estimate/useEstimateController.js @@ -51,15 +51,15 @@ export const useEstimateController = (planNo) => { useEffect(() => { if (planNo && !isLoading) { if (objectRecoil.floorPlanObjectNo && planNo) { - fetchSetting() + fetchSetting(objectRecoil.floorPlanObjectNo, planNo) } } }, []) // 상세 조회 - const fetchSetting = async () => { + const fetchSetting = async (objectNo, planNo) => { try { - await get({ url: `/api/estimate/${objectRecoil.floorPlanObjectNo}/${planNo}/detail` }).then((res) => { + await get({ url: `/api/estimate/${objectNo}/${planNo}/detail` }).then((res) => { if (isObjectNotEmpty(res)) { if (res.itemList.length > 0) { res.itemList.map((item) => { @@ -84,7 +84,6 @@ export const useEstimateController = (planNo) => { } const addItem = () => { - // const newItemDispOrder = (Math.max(...state.itemList.map((item) => item.dispOrder)) / 100 + 1) * 100 let newItemDispOrder = Math.max(...state.itemList.map((item) => item.dispOrder)) newItemDispOrder = (Math.floor(newItemDispOrder / 100) + 1) * 100 setState({ @@ -191,11 +190,10 @@ export const useEstimateController = (planNo) => { //2. 상세데이터 저장 // return await promisePost({ url: `${ESTIMATE_API_ENDPOINT}/save-estimate`, data: estimateData }).then((res) => { - console.log('res::::::::::::', res) if (res.status === 201) { alert(getMessage('estimate.detail.save.alertMsg')) //어디로 보낼지 - router.push(`/floor-plan/estimate/5/${estimateData.planNo}`) + fetchSetting(objectRecoil.floorPlanObjectNo, estimateData.planNo) } }) } @@ -206,25 +204,33 @@ export const useEstimateController = (planNo) => { * (견적서 번호(estimateData.docNo)가 생성된 이후 버튼 활성화 ) * T01관리자 계정 및 1차판매점에게만 제공 */ - const handleEstimateCopy = async (sendPlanNo, sendCharger, selOptions) => { - console.log('입력한 값들!@@@@@', sendPlanNo, sendCharger, selOptions) + const handleEstimateCopy = async (sendPlanNo, copyReceiveUser, saleStoreId, otherSaleStoreId) => { + if (saleStoreId === '') { + return alert(getMessage('estimate.detail.productFeaturesPopup.requiredStoreId')) + } + + if (copyReceiveUser.trim().length === 0) { + return alert(getMessage('estimate.detail.productFeaturesPopup.requiredReceiveUser')) + } const params = { saleStoreId: session.storeId, sapSalesStoreCd: session.custCd, objectNo: objectRecoil.floorPlanObjectNo, planNo: sendPlanNo, - copySaleStoreId: selOptions, - copyReceiveUser: sendCharger, + copySaleStoreId: otherSaleStoreId ? otherSaleStoreId : saleStoreId, + copyReceiveUser: copyReceiveUser, userId: session.userId, } - console.log('전송파람:::', params) - return + // return await promisePost({ url: '/api/estimate/save-estimate-copy', data: params }).then((res) => { - console.log('복사결과::::::::', res) - //물건 번호 새로 내려옴 - //견적서가 복사되었습니다. 복사된 물건정보로 이동합니다. - // alert(getMessage('estimate.detail.estimateCopyPopup.copy.alertMessage')) + if (res.status === 201) { + if (isObjectNotEmpty(res.data)) { + let newObjectNo = res.data.objectNo + alert(getMessage('estimate.detail.estimateCopyPopup.copy.alertMessage')) + router.push(`/management/stuff/detail?objectNo=${newObjectNo.toString()}`, { scroll: false }) + } + } }) } diff --git a/src/locales/ja.json b/src/locales/ja.json index 2c0767ac..d1d56038 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -884,6 +884,8 @@ "estimate.detail.estimateCopyPopup.copy.alertMessage": "見積書がコピーされました. コピーした商品情報に移動します.", "estimate.detail.productFeaturesPopup.title": "製品特異事項", "estimate.detail.productFeaturesPopup.close": "閉じる", + "estimate.detail.productFeaturesPopup.requiredStoreId": "一次販売店は必須です.", + "estimate.detail.productFeaturesPopup.requiredReceiveUser": "担当者は必須です.", "estimate.detail.save.alertMsg": "保存されている見積書で製品を変更した場合、図面や回路には反映されません.", "estimate.detail.save.requiredMsg": "ファイル添付が必須のアイテムがあります。ファイルを添付するか、後日添付をチェックしてください.", "estimate.detail.save.requiredItem": "製品は1つ以上登録する必要があります.", diff --git a/src/locales/ko.json b/src/locales/ko.json index 55ad3b14..5ed1f4fc 100644 --- a/src/locales/ko.json +++ b/src/locales/ko.json @@ -894,6 +894,8 @@ "estimate.detail.estimateCopyPopup.copy.alertMessage": "견적서가 복사되었습니다. 복사된 물건정보로 이동합니다.", "estimate.detail.productFeaturesPopup.title": "제품특이사항", "estimate.detail.productFeaturesPopup.close": "닫기", + "estimate.detail.productFeaturesPopup.requiredStoreId": "1차 판매점은 필수값 입니다.", + "estimate.detail.productFeaturesPopup.requiredReceiveUser": "담당자는 필수값 입니다.", "estimate.detail.save.alertMsg": "저장되었습니다. 견적서에서 제품을 변경할 경우, 도면 및 회로에 반영되지 않습니다.", "estimate.detail.save.requiredMsg": "파일첨부가 필수인 아이템이 있습니다. 파일을 첨부하거나 후일첨부를 체크해주십시오.", "estimate.detail.save.requiredItem": "제품은 1개이상 등록해야 됩니다.",