diff --git a/src/Pages/CustomerMaster/CustMasterForm.jsx b/src/Pages/CustomerMaster/CustMasterForm.jsx index 0698166..d86a502 100644 --- a/src/Pages/CustomerMaster/CustMasterForm.jsx +++ b/src/Pages/CustomerMaster/CustMasterForm.jsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef } from 'react'; +import { useState, useEffect, useRef, useCallback } from 'react'; import { useDispatch } from 'react-redux'; import { InputField } from '../../Components/Forms/InputField.jsx'; import Buttons from '../../Components/Forms/Buttons.jsx'; @@ -37,6 +37,7 @@ import { MdLocationOn, MdHome } from 'react-icons/md'; import { useSelector } from 'react-redux'; import { ApplicationPreferences } from '../../Features/BrachLogin/BranchLogin.js'; import { IoMdAddCircleOutline } from 'react-icons/io'; +import { isIndianMobile } from '../../utils/calculations.js'; const subDirectory = import.meta.env.BASE_URL; @@ -354,57 +355,41 @@ const CustomerForm = ({ formType }) => { //post data const onFinish = async (values) => { - let postData = values; - console.log(postData, 'postData'); - - // Handle address data - use only the addresses from addressesList - const addedAddresses = addressesList || []; - - // Only include addresses that have Address1 and Zip - const validAddresses = addedAddresses.filter( - (addr) => addr?.Address1 && addr?.Zip - ); - - postData['CustomerAddressDtl'] = validAddresses; - - postData['CreatedBy'] = UserId; - postData['CompId'] = CompId; - postData['BranchId'] = BranchId; - postData['UserId'] = GetUserId; - // postData["OutStandingAmount"] = parseInt(values?.OutStandingAmount); - postData['InitialOutStanding'] = parseInt(values?.OutStandingAmount); - - postData['Document'] = ImageUrl; - postData['CustomerProfile'] = ImageUrl ? ImageUrl : null; - postData['CreditDays'] = - selectedCredit === 'others' ? otherCreditValue : selectedCredit; - let response = {}; - if (formType === 'add') { - // if (SelectedUserCreation === "N") { - postData['AppId'] = AppId; - // } - if (offerType) { - postData['OfferType'] = offerType; - } - if (Maritalstatus == 'N') { - postData['MarriedStatus'] = 'N'; - postData['CustDOB'] = DobDate; - } - if (Maritalstatus == 'Y') { - postData['MarriedStatus'] = 'Y'; - postData['CustDOB'] = DobDate; - postData['SpouseDOB'] = SpouseDobDate; - postData['AnniversaryDate'] = anniversaryDate; + try { + let postData = values; + console.log(postData, 'postData'); + const validMobile = isIndianMobile(postData?.CustMobile); + if (!validMobile) { + setMessageType('warning'); + setMessageData('Please enter a valid Indian mobile number.'); + return; } + // Handle address data - use only the addresses from addressesList + const addedAddresses = addressesList || []; - response = await dispatch(postCust(postData)).unwrap(); - } else if (formType === 'edit') { - if (editstate) { - postData['CustId'] = editstate?.CustId; - postData['AddId'] = editstate?.CustAddId; - postData['AppId'] = editstate?.AppId; - postData['UpdatedBy'] = UserId; - postData['UserId'] = editstate?.UserId; + // Only include addresses that have Address1 and Zip + const validAddresses = addedAddresses.filter( + (addr) => addr?.Address1 && addr?.Zip + ); + + postData['CustomerAddressDtl'] = validAddresses; + + postData['CreatedBy'] = UserId; + postData['CompId'] = CompId; + postData['BranchId'] = BranchId; + postData['UserId'] = GetUserId; + // postData["OutStandingAmount"] = parseInt(values?.OutStandingAmount); + postData['InitialOutStanding'] = parseInt(values?.OutStandingAmount); + + postData['Document'] = ImageUrl; + postData['CustomerProfile'] = ImageUrl ? ImageUrl : null; + postData['CreditDays'] = + selectedCredit === 'others' ? otherCreditValue : selectedCredit; + let response = {}; + if (formType === 'add') { + // if (SelectedUserCreation === "N") { + postData['AppId'] = AppId; + // } if (offerType) { postData['OfferType'] = offerType; } @@ -418,22 +403,48 @@ const CustomerForm = ({ formType }) => { postData['SpouseDOB'] = SpouseDobDate; postData['AnniversaryDate'] = anniversaryDate; } - } - response = await dispatch(putCust(postData)).unwrap(); - } - if (response?.data?.statusCode == 1) { - navigate(`${subDirectory}setting/customer-master/`, { - state: { - Notiffy: { - messageType: 'success', - messageData: response?.data?.response, + response = await dispatch(postCust(postData)).unwrap(); + } else if (formType === 'edit') { + if (editstate) { + postData['CustId'] = editstate?.CustId; + postData['AddId'] = editstate?.CustAddId; + postData['AppId'] = editstate?.AppId; + postData['UpdatedBy'] = UserId; + postData['UserId'] = editstate?.UserId; + if (offerType) { + postData['OfferType'] = offerType; + } + if (Maritalstatus == 'N') { + postData['MarriedStatus'] = 'N'; + postData['CustDOB'] = DobDate; + } + if (Maritalstatus == 'Y') { + postData['MarriedStatus'] = 'Y'; + postData['CustDOB'] = DobDate; + postData['SpouseDOB'] = SpouseDobDate; + postData['AnniversaryDate'] = anniversaryDate; + } + } + response = await dispatch(putCust(postData)).unwrap(); + } + + if (response?.data?.statusCode == 1) { + navigate(`${subDirectory}setting/customer-master/`, { + state: { + Notiffy: { + messageType: 'success', + messageData: response?.data?.response, + }, }, - }, - }); - } else { - setMessageType('error'); - setMessageData(response?.data?.response); + }); + } else { + setMessageType('error'); + setMessageData(response?.data?.response); + } + } catch (error) { + // Validation error will stop submit + console.log("Validation Failed:", error); } }; @@ -841,12 +852,17 @@ const CustomerForm = ({ formType }) => { ); }; + const onComplete = useCallback(() => { + setMessageType(null); + setMessageData(null); + }, []); + return (
Upload Profile