feat: inquiry unique id
This commit is contained in:
@@ -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 }) => (
|
||||
<Textfield
|
||||
fullWidth
|
||||
placeholder="شناسه یکتا واحد (اختیاری)"
|
||||
value={field.value}
|
||||
onChange={field.onChange}
|
||||
error={!!errors.unique_unit_identity}
|
||||
helperText={errors.unique_unit_identity?.message}
|
||||
/>
|
||||
<div className="flex items-start gap-2 w-full">
|
||||
<Textfield
|
||||
fullWidth
|
||||
placeholder={
|
||||
isInquiryRequired
|
||||
? "شناسه یکتا واحد"
|
||||
: "شناسه یکتا واحد (اختیاری)"
|
||||
}
|
||||
value={field.value}
|
||||
onChange={(e) => {
|
||||
field.onChange(e);
|
||||
setInquiryPassed(false);
|
||||
}}
|
||||
error={
|
||||
!!errors.unique_unit_identity ||
|
||||
(isInquiryRequired && !inquiryPassed && !!field.value)
|
||||
}
|
||||
helperText={
|
||||
errors.unique_unit_identity?.message ||
|
||||
(isInquiryRequired && inquiryPassed
|
||||
? "استعلام تایید شده"
|
||||
: undefined)
|
||||
}
|
||||
/>
|
||||
{isInquiryRequired && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleInquiry}
|
||||
disabled={inquiryLoading || !field.value}
|
||||
className={`shrink-0 flex items-center gap-1 mt-[2px] px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
|
||||
inquiryPassed
|
||||
? "bg-green-500 text-white hover:bg-green-600"
|
||||
: "bg-blue-500 text-white hover:bg-blue-600"
|
||||
} disabled:opacity-50 disabled:cursor-not-allowed`}
|
||||
>
|
||||
{inquiryLoading
|
||||
? "..."
|
||||
: inquiryPassed
|
||||
? "تایید شده"
|
||||
: "استعلام"}
|
||||
|
||||
{inquiryPassed && !inquiryLoading ? (
|
||||
<CheckBadgeIcon className="w-4 h-4 text-white" />
|
||||
) : (
|
||||
<ArrowPathIcon className="w-4 h-4 text-white" />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user