From 28f7d5c991279a139e2ec7125873e19eaa5e4169 Mon Sep 17 00:00:00 2001 From: wixarm Date: Tue, 10 Feb 2026 16:50:04 +0330 Subject: [PATCH] feat: inquiry unique id --- src/partials/management/AddOrganization.tsx | 109 ++++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/src/partials/management/AddOrganization.tsx b/src/partials/management/AddOrganization.tsx index 2a6daa4..b34be52 100644 --- a/src/partials/management/AddOrganization.tsx +++ b/src/partials/management/AddOrganization.tsx @@ -20,7 +20,13 @@ import { getToastResponse } from "../../data/getToastResponse"; import { useUserProfileStore } from "../../context/zustand-store/userStore"; import { useState } from "react"; import Checkbox from "../../components/CheckBox/CheckBox"; -import { PlusIcon, TrashIcon } from "@heroicons/react/24/outline"; +import { + ArrowPathIcon, + CheckBadgeIcon, + PlusIcon, + TrashIcon, +} from "@heroicons/react/24/outline"; +import axios from "axios"; const schema = z.object({ name: zValidateString("نام سازمان"), @@ -106,6 +112,10 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => { : [{ postal_code: "", address: "" }], ); + const [isInquiryRequired, setIsInquiryRequired] = useState(false); + const [inquiryPassed, setInquiryPassed] = useState(false); + const [inquiryLoading, setInquiryLoading] = useState(false); + const handleAddAddress = () => { setAddresses((prev) => [...prev, { postal_code: "", address: "" }]); }; @@ -124,7 +134,40 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => { ); }; + const handleInquiry = async () => { + const code = getValues("unique_unit_identity"); + if (!code) { + showToast("لطفاً شناسه یکتا واحد را وارد کنید!", "error"); + return; + } + setInquiryLoading(true); + try { + await axios.get( + `https://rsibackend.rasadyar.com/app/has_code_in_db/?code=${code}`, + ); + setInquiryPassed(true); + showToast("استعلام با موفقیت انجام شد!", "success"); + } catch (error: any) { + if (error?.response?.status === 404) { + setInquiryPassed(false); + showToast("شناسه موجود نیست!", "error"); + } else { + showToast("خطا در استعلام!", "error"); + } + } finally { + setInquiryLoading(false); + } + }; + const onSubmit = async (data: FormValues) => { + if (isInquiryRequired && !data.unique_unit_identity) { + showToast("شناسه یکتا واحد الزامی است!", "error"); + return; + } + if (isInquiryRequired && !inquiryPassed) { + showToast("لطفاً ابتدا استعلام شناسه یکتا واحد را انجام دهید!", "error"); + return; + } try { await mutation.mutateAsync({ addresses: addresses.filter( @@ -187,6 +230,7 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => { keyField="id" secondaryKey="is_repeatable" tertiaryKey="org_type_field" + quaternaryKey="key" valueField="name" error={!!errors.organizationType} errorMessage={errors.organizationType?.message} @@ -197,6 +241,12 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => { trigger(["organizationType"]); }} onChangeValue={(r) => { + if (r.key4 === "U" || r.key4 === "CO") { + setIsInquiryRequired(true); + } else { + setIsInquiryRequired(false); + setInquiryPassed(false); + } if (!r.key2) { setValue("name", r.value); } else { @@ -261,14 +311,55 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => { name="unique_unit_identity" control={control} render={({ field }) => ( - +
+ { + field.onChange(e); + setInquiryPassed(false); + }} + error={ + !!errors.unique_unit_identity || + (isInquiryRequired && !inquiryPassed && !!field.value) + } + helperText={ + errors.unique_unit_identity?.message || + (isInquiryRequired && inquiryPassed + ? "استعلام تایید شده" + : undefined) + } + /> + {isInquiryRequired && ( + + )} +
)} />