diff --git a/src/features/psp-company/components/psp-devices-v2/PspDevicesV2.js b/src/features/psp-company/components/psp-devices-v2/PspDevicesV2.js index 86ec829..b2a5b9b 100644 --- a/src/features/psp-company/components/psp-devices-v2/PspDevicesV2.js +++ b/src/features/psp-company/components/psp-devices-v2/PspDevicesV2.js @@ -400,6 +400,34 @@ const AssignOwnerModal = ({ device, onSuccess }) => { const [loading, setLoading] = useState(false); const [submitting, setSubmitting] = useState(false); const [fetchError, setFetchError] = useState(""); + const [values, setValues] = useState({ + receiver_number: device?.receiverNumber, + terminal_number: device?.terminalNumber, + password: device?.password, + }); + + const [errors, setErrors] = useState({}); + + const validate = () => { + const newErrors = {}; + + if (!values.receiver_number?.trim()) { + newErrors.receiver_number = "وارد کردن شماره پذیرنده الزامی است."; + } + + if (!values.terminal_number?.trim()) { + newErrors.terminal_number = "وارد کردن شماره ترمینال الزامی است."; + } + + if (!values.password?.trim()) { + newErrors.password = "وارد کردن رمز عبور الزامی است."; + } else if (!/^\d{4}$/.test(values.password)) { + newErrors.password = "رمز عبور باید دقیقاً ۴ رقم عددی باشد."; + } + + setErrors(newErrors); + return Object.keys(newErrors).length === 0; + }; useEffect(() => { let isMounted = true; @@ -461,10 +489,19 @@ const AssignOwnerModal = ({ device, onSuccess }) => { dispatch(CLOSE_MODAL()); }; - const handleSubmit = async () => { - if (!selectedOption) { - return; + const handleChange = (event) => { + const { name, value } = event.target; + setValues((prev) => ({ ...prev, [name]: value })); + + if (errors[name]) { + setErrors((prev) => ({ ...prev, [name]: "" })); } + }; + + const handleSubmit = async () => { + if (!selectedOption) return; + + if (!validate()) return; if (!device?.key && !device?.id) { openNotif({ @@ -482,6 +519,10 @@ const AssignOwnerModal = ({ device, onSuccess }) => { key: device?.key || device?.id, recipient_type: "owner", recipient_key: selectedOption?.key || selectedOption?.id, + + receiver_number: values.receiver_number.trim(), + terminal_number: values.terminal_number.trim(), + password: values.password.replace(/\D/g, "").slice(0, 4), }; await axios.put("/user-pos-machine/0/", payload); @@ -493,9 +534,7 @@ const AssignOwnerModal = ({ device, onSuccess }) => { msg: "مالک با موفقیت ثبت شد.", }); - if (onSuccess) { - onSuccess(); - } + onSuccess?.(); handleCloseModal(); } catch (error) { openNotif({ @@ -584,6 +623,39 @@ const AssignOwnerModal = ({ device, onSuccess }) => { )} /> )} + + + {fetchError && ( {fetchError} @@ -594,7 +666,7 @@ const AssignOwnerModal = ({ device, onSuccess }) => { variant="outlined" color="primary" onClick={handleCloseModal} - disabled={submitting} + disabled={!selectedOption || submitting || loading} > انصراف