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 { useUserProfileStore } from "../../context/zustand-store/userStore";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import Checkbox from "../../components/CheckBox/CheckBox";
|
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({
|
const schema = z.object({
|
||||||
name: zValidateString("نام سازمان"),
|
name: zValidateString("نام سازمان"),
|
||||||
@@ -106,6 +112,10 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => {
|
|||||||
: [{ postal_code: "", address: "" }],
|
: [{ postal_code: "", address: "" }],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [isInquiryRequired, setIsInquiryRequired] = useState(false);
|
||||||
|
const [inquiryPassed, setInquiryPassed] = useState(false);
|
||||||
|
const [inquiryLoading, setInquiryLoading] = useState(false);
|
||||||
|
|
||||||
const handleAddAddress = () => {
|
const handleAddAddress = () => {
|
||||||
setAddresses((prev) => [...prev, { postal_code: "", address: "" }]);
|
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) => {
|
const onSubmit = async (data: FormValues) => {
|
||||||
|
if (isInquiryRequired && !data.unique_unit_identity) {
|
||||||
|
showToast("شناسه یکتا واحد الزامی است!", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isInquiryRequired && !inquiryPassed) {
|
||||||
|
showToast("لطفاً ابتدا استعلام شناسه یکتا واحد را انجام دهید!", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await mutation.mutateAsync({
|
await mutation.mutateAsync({
|
||||||
addresses: addresses.filter(
|
addresses: addresses.filter(
|
||||||
@@ -187,6 +230,7 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => {
|
|||||||
keyField="id"
|
keyField="id"
|
||||||
secondaryKey="is_repeatable"
|
secondaryKey="is_repeatable"
|
||||||
tertiaryKey="org_type_field"
|
tertiaryKey="org_type_field"
|
||||||
|
quaternaryKey="key"
|
||||||
valueField="name"
|
valueField="name"
|
||||||
error={!!errors.organizationType}
|
error={!!errors.organizationType}
|
||||||
errorMessage={errors.organizationType?.message}
|
errorMessage={errors.organizationType?.message}
|
||||||
@@ -197,6 +241,12 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => {
|
|||||||
trigger(["organizationType"]);
|
trigger(["organizationType"]);
|
||||||
}}
|
}}
|
||||||
onChangeValue={(r) => {
|
onChangeValue={(r) => {
|
||||||
|
if (r.key4 === "U" || r.key4 === "CO") {
|
||||||
|
setIsInquiryRequired(true);
|
||||||
|
} else {
|
||||||
|
setIsInquiryRequired(false);
|
||||||
|
setInquiryPassed(false);
|
||||||
|
}
|
||||||
if (!r.key2) {
|
if (!r.key2) {
|
||||||
setValue("name", r.value);
|
setValue("name", r.value);
|
||||||
} else {
|
} else {
|
||||||
@@ -261,14 +311,55 @@ export const AddOrganization = ({ getData, item }: AddPageProps) => {
|
|||||||
name="unique_unit_identity"
|
name="unique_unit_identity"
|
||||||
control={control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
|
<div className="flex items-start gap-2 w-full">
|
||||||
<Textfield
|
<Textfield
|
||||||
fullWidth
|
fullWidth
|
||||||
placeholder="شناسه یکتا واحد (اختیاری)"
|
placeholder={
|
||||||
|
isInquiryRequired
|
||||||
|
? "شناسه یکتا واحد"
|
||||||
|
: "شناسه یکتا واحد (اختیاری)"
|
||||||
|
}
|
||||||
value={field.value}
|
value={field.value}
|
||||||
onChange={field.onChange}
|
onChange={(e) => {
|
||||||
error={!!errors.unique_unit_identity}
|
field.onChange(e);
|
||||||
helperText={errors.unique_unit_identity?.message}
|
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