Compare commits
2 Commits
e51f998a91
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 84ee3f68fd | |||
| f53021aca3 |
@@ -124,10 +124,7 @@ const LadingInfo: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Lading info error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message || "خطا در دریافت اطلاعات بارنامه",
|
||||
"error"
|
||||
);
|
||||
showToast("ارتباط با سرور برقرار نشد!", "error");
|
||||
setLadingData([]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -111,7 +111,7 @@ const NationalInfo: React.FC = () => {
|
||||
const [personRegistryData, setPersonRegistryData] =
|
||||
useState<PersonRegistryData | null>(null);
|
||||
const [unitRegistryData, setUnitRegistryData] = useState<UnitRegistryData[]>(
|
||||
[]
|
||||
[],
|
||||
);
|
||||
const showToast = useToast();
|
||||
|
||||
@@ -146,7 +146,7 @@ const NationalInfo: React.FC = () => {
|
||||
} else {
|
||||
showToast(
|
||||
"لطفا 10 رقم (کد ملی) یا 11 رقم (شماره موبایل) وارد کنید",
|
||||
"error"
|
||||
"error",
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -170,7 +170,7 @@ const NationalInfo: React.FC = () => {
|
||||
nationalCodeForRegistry = trimmedValue;
|
||||
} else if (result?.data && result.data.length > 0) {
|
||||
const personWithNationalCode = result.data.find(
|
||||
(person) => person.nationalcode && person.nationalcode.length === 10
|
||||
(person) => person.nationalcode && person.nationalcode.length === 10,
|
||||
);
|
||||
if (personWithNationalCode) {
|
||||
nationalCodeForRegistry = personWithNationalCode.nationalcode;
|
||||
@@ -195,10 +195,7 @@ const NationalInfo: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Person registry error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message || "خطا در استعلام ثبت احوال",
|
||||
"error"
|
||||
);
|
||||
showToast("خطا در استعلام ثبت احوال", "error");
|
||||
setPersonRegistryData(null);
|
||||
}
|
||||
}
|
||||
@@ -219,14 +216,11 @@ const NationalInfo: React.FC = () => {
|
||||
const unitsCandidate =
|
||||
unitResult?.data ?? (Array.isArray(unitResult) ? unitResult : []);
|
||||
setUnitRegistryData(
|
||||
Array.isArray(unitsCandidate) ? unitsCandidate : []
|
||||
Array.isArray(unitsCandidate) ? unitsCandidate : [],
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error("Unit registry error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message || "خطا در استعلام واحد صنفی",
|
||||
"error"
|
||||
);
|
||||
showToast("خطا در استعلام واحد صنفی", "error");
|
||||
setUnitRegistryData([]);
|
||||
}
|
||||
}
|
||||
@@ -235,7 +229,7 @@ const NationalInfo: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Search error:", error);
|
||||
showToast(error?.response?.data?.message || "خطا در جستجو", "error");
|
||||
showToast("خطا در جستجو", "error");
|
||||
setSearchResults([]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -84,10 +84,7 @@ const PostcodeInquiry: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Postcode inquiry error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message || "خطا در استعلام کد پستی",
|
||||
"error",
|
||||
);
|
||||
showToast("ارتباط با سرور برقرار نشد!", "error");
|
||||
setInquiryData(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -78,7 +78,7 @@ interface InquiryFarmResponse {
|
||||
const VeterinaryTransfer: React.FC = () => {
|
||||
const [trIDCode, setTrIDCode] = useState("");
|
||||
const [transferData, setTransferData] = useState<VeterinaryTransferItem[]>(
|
||||
[]
|
||||
[],
|
||||
);
|
||||
const [farmInfoByPartId, setFarmInfoByPartId] = useState<
|
||||
Record<string, InquiryFarmResponse | null>
|
||||
@@ -117,11 +117,7 @@ const VeterinaryTransfer: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Veterinary transfer error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message ||
|
||||
"خطا در دریافت اطلاعات گواهی بهداشتی حمل",
|
||||
"error"
|
||||
);
|
||||
showToast("خطا در دریافت اطلاعات گواهی بهداشتی حمل", "error");
|
||||
setTransferData([]);
|
||||
}
|
||||
};
|
||||
@@ -145,10 +141,7 @@ const VeterinaryTransfer: React.FC = () => {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Inquiry farm error:", error);
|
||||
showToast(
|
||||
error?.response?.data?.message || "خطا در استعلام واحد کشاورزی",
|
||||
"error"
|
||||
);
|
||||
showToast("خطا در استعلام واحد کشاورزی", "error");
|
||||
setFarmInfoByPartId((prev) => ({ ...prev, [code]: null }));
|
||||
} finally {
|
||||
setFarmLoadingByPartId((prev) => ({ ...prev, [code]: false }));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { useMemo } from "react";
|
||||
import React, { useMemo, useEffect, useRef } from "react";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm, Controller } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
@@ -33,12 +33,15 @@ interface SubmitNewUserProps {
|
||||
item?: UserItem | null;
|
||||
}
|
||||
|
||||
const getSchema = (isEdit: boolean) =>
|
||||
const getSchema = (isEdit: boolean, isAdmin: boolean) =>
|
||||
z.object({
|
||||
mobile: zValidateMobile("موبایل"),
|
||||
password: isEdit ? z.string().optional() : zValidateString("کلمه عبور"),
|
||||
fullname: zValidateString("نام کامل"),
|
||||
permissions: zValidateAutoComplete("دسترسیها"),
|
||||
province: isAdmin
|
||||
? zValidateAutoComplete("استان")
|
||||
: z.array(z.union([z.string(), z.number()])).optional(),
|
||||
city: zValidateAutoComplete("شهر"),
|
||||
});
|
||||
|
||||
@@ -52,12 +55,17 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
const showToast = useToast();
|
||||
const { closeDrawer } = useDrawerStore();
|
||||
const isEdit = !!item;
|
||||
const schema = useMemo(() => getSchema(isEdit), [isEdit]);
|
||||
const hasAdminPermission = profile?.permissions?.includes("admin");
|
||||
const schema = useMemo(
|
||||
() => getSchema(isEdit, !!hasAdminPermission),
|
||||
[isEdit, hasAdminPermission],
|
||||
);
|
||||
|
||||
const {
|
||||
control,
|
||||
handleSubmit,
|
||||
setValue,
|
||||
watch,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(schema),
|
||||
@@ -67,6 +75,7 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
password: "",
|
||||
fullname: item.fullname ?? "",
|
||||
permissions: Array.isArray(item.permissions) ? item.permissions : [],
|
||||
province: item.province ? [item.province] : [],
|
||||
city: item.city ? [item.city] : [],
|
||||
}
|
||||
: {
|
||||
@@ -74,6 +83,7 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
password: "",
|
||||
fullname: "",
|
||||
permissions: [],
|
||||
province: [],
|
||||
city: [],
|
||||
},
|
||||
});
|
||||
@@ -88,14 +98,32 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
method: "put",
|
||||
});
|
||||
|
||||
const watchedProvince = watch("province");
|
||||
const selectedProvince = hasAdminPermission
|
||||
? Array.isArray(watchedProvince) && watchedProvince.length > 0
|
||||
? String(watchedProvince[0])
|
||||
: ""
|
||||
: profile?.province || "";
|
||||
|
||||
const prevProvinceRef = useRef<string>(selectedProvince);
|
||||
useEffect(() => {
|
||||
if (hasAdminPermission && prevProvinceRef.current !== selectedProvince) {
|
||||
setValue("city", []);
|
||||
prevProvinceRef.current = selectedProvince;
|
||||
}
|
||||
}, [selectedProvince, hasAdminPermission, setValue]);
|
||||
|
||||
const provinceOptions = [
|
||||
{ key: "hamedan", value: "همدان" },
|
||||
{ key: "markazi", value: "مرکزی" },
|
||||
];
|
||||
|
||||
const cityOptions = useMemo(() => {
|
||||
return getCitiesOfProvinceInfo(profile?.province || "").map((item) => ({
|
||||
return getCitiesOfProvinceInfo(selectedProvince).map((item) => ({
|
||||
key: item.en,
|
||||
value: item.fa,
|
||||
}));
|
||||
}, [profile?.province]);
|
||||
|
||||
const hasAdminPermission = profile?.permissions?.includes("admin");
|
||||
}, [selectedProvince]);
|
||||
|
||||
const permissionOptions = useMemo(() => {
|
||||
const options = [
|
||||
@@ -115,7 +143,11 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
fullname: data.fullname,
|
||||
pic: "",
|
||||
permissions: data.permissions as string[],
|
||||
province: profile?.province,
|
||||
province: hasAdminPermission
|
||||
? Array.isArray(data.province) && data.province.length > 0
|
||||
? String(data.province[0])
|
||||
: ""
|
||||
: profile?.province,
|
||||
city:
|
||||
Array.isArray(data.city) && data.city.length > 0
|
||||
? String(data.city[0])
|
||||
@@ -202,6 +234,31 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||
)}
|
||||
/>
|
||||
|
||||
{hasAdminPermission && (
|
||||
<div className="w-full">
|
||||
<Controller
|
||||
name="province"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<AutoComplete
|
||||
data={provinceOptions}
|
||||
selectedKeys={
|
||||
(Array.isArray(field.value) ? field.value : []) as (
|
||||
| string
|
||||
| number
|
||||
)[]
|
||||
}
|
||||
onChange={(keys) => setValue("province", keys as any)}
|
||||
title="استان را انتخاب کنید"
|
||||
error={!!errors.province}
|
||||
helperText={errors.province?.message}
|
||||
multiselect={false}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="w-full">
|
||||
<Controller
|
||||
name="city"
|
||||
|
||||
Reference in New Issue
Block a user