fix: erros for Create guilds
This commit is contained in:
@@ -130,21 +130,23 @@ export const CreateGuilds = ({ guild, updateTable }) => {
|
||||
hasInquiry
|
||||
);
|
||||
});
|
||||
dispatch(updateGuildByNationalIdNewService(guildsDataArray)).then(
|
||||
(result) => {
|
||||
if (result.payload.error) {
|
||||
handleSubmitError(openNotif, result.payload.error);
|
||||
} else {
|
||||
handleSubmitSuccess(
|
||||
dispatch,
|
||||
openNotif,
|
||||
updateTable,
|
||||
values,
|
||||
result.payload?.data
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
console.log(guildsDataArray);
|
||||
// dispatch(updateGuildByNationalIdNewService(guildsDataArray)).then(
|
||||
// (result) => {
|
||||
// if (result.payload.error) {
|
||||
// handleSubmitError(openNotif, result.payload.error);
|
||||
// } else {
|
||||
// handleSubmitSuccess(
|
||||
// dispatch,
|
||||
// openNotif,
|
||||
// updateTable,
|
||||
// values,
|
||||
// result.payload?.data
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
},
|
||||
});
|
||||
|
||||
@@ -183,6 +185,7 @@ export const CreateGuilds = ({ guild, updateTable }) => {
|
||||
return {
|
||||
steward: initialValues.steward || false,
|
||||
guild: initialValues.guild || false,
|
||||
license_number: initialValues.license_number ?? "",
|
||||
};
|
||||
});
|
||||
formik.setFieldValue("guilds", guildsForFormik, false).then(() => {
|
||||
@@ -266,20 +269,11 @@ export const CreateGuilds = ({ guild, updateTable }) => {
|
||||
});
|
||||
setGuildsFormValues(initialGuildValues);
|
||||
|
||||
// Update Formik's guilds array for validation
|
||||
const guildsForFormik = guildsData.map((guildItem) => ({
|
||||
steward:
|
||||
typeof guildItem?.steward === "boolean"
|
||||
? guildItem.steward
|
||||
: typeof guildItem?.isSteward === "boolean"
|
||||
? guildItem.isSteward
|
||||
: false,
|
||||
guild:
|
||||
typeof guildItem?.guild === "boolean"
|
||||
? guildItem.guild
|
||||
: typeof guildItem?.isGuild === "boolean"
|
||||
? guildItem.isGuild
|
||||
: false,
|
||||
// Update Formik's guilds array for validation (include validated fields)
|
||||
const guildsForFormik = initialGuildValues.map((g) => ({
|
||||
steward: g.steward ?? false,
|
||||
guild: g.guild ?? false,
|
||||
license_number: g.license_number ?? "",
|
||||
}));
|
||||
formik.setFieldValue("guilds", guildsForFormik, true).then(() => {
|
||||
formik.validateField("guilds");
|
||||
@@ -563,8 +557,12 @@ export const CreateGuilds = ({ guild, updateTable }) => {
|
||||
return newValues;
|
||||
});
|
||||
|
||||
// Sync with Formik's guilds array for steward and guild fields
|
||||
if (fieldName === "steward" || fieldName === "guild") {
|
||||
// Sync validated guild fields to Formik's guilds array (schema: license_number, steward, guild)
|
||||
if (
|
||||
fieldName === "steward" ||
|
||||
fieldName === "guild" ||
|
||||
fieldName === "license_number"
|
||||
) {
|
||||
const currentGuilds = formik.values.guilds || [];
|
||||
const updatedGuilds = [...currentGuilds];
|
||||
if (!updatedGuilds[index]) {
|
||||
@@ -683,6 +681,7 @@ export const CreateGuilds = ({ guild, updateTable }) => {
|
||||
onGuildValuesChange={handleGuildValuesChange}
|
||||
expanded={expandedAccordion === index}
|
||||
onChange={handleAccordionChange(index)}
|
||||
mainFormik={formik}
|
||||
/>
|
||||
</Box>
|
||||
))}
|
||||
|
||||
@@ -74,7 +74,9 @@ export const FormActions = ({
|
||||
color="error"
|
||||
sx={{ mt: 1, display: "block" }}
|
||||
>
|
||||
{formik.errors.guilds}
|
||||
{typeof formik.errors.guilds === "string"
|
||||
? formik.errors.guilds
|
||||
: "برای هر واحد صنفی، حداقل یکی از گزینههای مباشر یا صنف باید انتخاب شود"}
|
||||
</Typography>
|
||||
)}
|
||||
</Grid>
|
||||
|
||||
@@ -26,7 +26,12 @@ export const GuildInfoAccordionItem = ({
|
||||
onGuildValuesChange,
|
||||
expanded,
|
||||
onChange,
|
||||
mainFormik,
|
||||
}) => {
|
||||
// Errors and touched for this guild from main formik (formik.errors.guilds[i], formik.touched.guilds[i])
|
||||
const guildErrors = mainFormik?.errors?.guilds?.[guildIndex] ?? {};
|
||||
const guildTouched = mainFormik?.touched?.guilds?.[guildIndex] ?? {};
|
||||
|
||||
// Create a formik-like object for this guild's values
|
||||
const guildFormik = {
|
||||
values: guildFormValues || {},
|
||||
@@ -36,9 +41,14 @@ export const GuildInfoAccordionItem = ({
|
||||
handleChange: (e) => {
|
||||
onGuildValuesChange(guildIndex, e.target.name, e.target.value);
|
||||
},
|
||||
handleBlur: () => {},
|
||||
errors: {},
|
||||
touched: {},
|
||||
handleBlur: (e) => {
|
||||
const name = e?.target?.name;
|
||||
if (name && mainFormik?.setFieldTouched) {
|
||||
mainFormik.setFieldTouched(`guilds.${guildIndex}.${name}`, true);
|
||||
}
|
||||
},
|
||||
errors: guildErrors,
|
||||
touched: guildTouched,
|
||||
};
|
||||
const getGuildTitle = () => {
|
||||
if (guildData?.guildsName) {
|
||||
@@ -92,6 +102,7 @@ export const GuildInfoAccordionItem = ({
|
||||
typeActivities={typeActivities}
|
||||
hideTitle={true}
|
||||
noGridWrapper={true}
|
||||
guildIndex={guildIndex}
|
||||
/>
|
||||
</AccordionDetails>
|
||||
</Accordion>
|
||||
|
||||
@@ -242,6 +242,8 @@ export const GuildInfoSection = ({
|
||||
value={formik.values.license_number}
|
||||
onChange={formik.handleChange}
|
||||
onBlur={formik.handleBlur}
|
||||
error={Boolean(formik.errors?.license_number)}
|
||||
helperText={formik.errors?.license_number}
|
||||
/>
|
||||
) : (
|
||||
<InfoBox
|
||||
|
||||
@@ -436,12 +436,20 @@ export const PersonalInfoSection = ({
|
||||
label="شماره همراه"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
required
|
||||
id="mobile"
|
||||
name="mobile"
|
||||
value={formik.values.mobile}
|
||||
onChange={formik.handleChange}
|
||||
error={formik.touched.mobile && Boolean(formik.errors.mobile)}
|
||||
helperText={formik.touched.mobile && formik.errors.mobile}
|
||||
onBlur={formik.handleBlur}
|
||||
error={
|
||||
Boolean(formik.errors.mobile) &&
|
||||
(formik.touched.mobile || formik.submitCount > 0)
|
||||
}
|
||||
helperText={
|
||||
(formik.touched.mobile || formik.submitCount > 0) &&
|
||||
formik.errors.mobile
|
||||
}
|
||||
disabled={
|
||||
isKillHouse &&
|
||||
formik.values.mobile &&
|
||||
|
||||
@@ -3,26 +3,18 @@ import { normalizeDatabaseDate } from "./dateUtils";
|
||||
|
||||
/**
|
||||
* Creates validation schema for the form
|
||||
* @param {boolean} isEditMode - Whether we're editing an existing guild
|
||||
* @param {boolean} isEditMode - Whether we're editing an existing guild (kept for API compatibility)
|
||||
*/
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
export const getValidationSchema = (isEditMode) =>
|
||||
yup.object({
|
||||
national_id: yup
|
||||
.string()
|
||||
.required("کد ملی الزامی است")
|
||||
.matches(/^[0-9]{10}$/, "کد ملی باید 10 رقم باشد"),
|
||||
mobile: isEditMode
|
||||
? yup
|
||||
mobile: yup
|
||||
.string()
|
||||
.nullable()
|
||||
.test(
|
||||
"mobile-format",
|
||||
"شماره تلفن باید 11 رقم باشد",
|
||||
(value) => !value || /^[0-9]{11}$/.test(value)
|
||||
)
|
||||
: yup
|
||||
.string()
|
||||
.required("شماره تلفن الزامی است")
|
||||
.required("شماره همراه الزامی است")
|
||||
.matches(/^[0-9]{11}$/, "شماره تلفن باید 11 رقم باشد"),
|
||||
first_name: yup.string(),
|
||||
last_name: yup.string(),
|
||||
@@ -51,22 +43,18 @@ export const getValidationSchema = (isEditMode) =>
|
||||
.array()
|
||||
.min(1, "حداقل یک واحد صنفی باید وجود داشته باشد")
|
||||
.of(
|
||||
yup.object({
|
||||
steward: yup.boolean().default(false),
|
||||
guild: yup.boolean().default(false),
|
||||
yup
|
||||
.object({
|
||||
license_number: yup.string().required("شماره مجوز الزامی است"),
|
||||
|
||||
steward: yup.boolean(),
|
||||
guild: yup.boolean(),
|
||||
})
|
||||
)
|
||||
.test(
|
||||
"steward-guild-required",
|
||||
"برای هر واحد صنفی، حداقل یکی از گزینههای مباشر یا صنف باید انتخاب شود",
|
||||
function (guilds) {
|
||||
if (!guilds || guilds.length === 0) {
|
||||
return false;
|
||||
}
|
||||
return guilds.every(
|
||||
(guild) => guild?.steward === true || guild?.guild === true
|
||||
);
|
||||
}
|
||||
(value) => value?.steward === true || value?.guild === true
|
||||
)
|
||||
),
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import React, {
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
useCallback,
|
||||
useRef,
|
||||
} from "react";
|
||||
import React, { useContext, useEffect, useState, useCallback } from "react";
|
||||
import { useFormik } from "formik";
|
||||
import * as yup from "yup";
|
||||
import { useDispatch } from "react-redux";
|
||||
@@ -28,7 +22,6 @@ import {
|
||||
CLOSE_MODAL,
|
||||
LOADING_END,
|
||||
LOADING_START,
|
||||
OPEN_MODAL,
|
||||
} from "../../../../lib/redux/slices/appSlice";
|
||||
import { AppContext } from "../../../../contexts/AppContext";
|
||||
import { provinceGetLegalPersonUnitInfoService } from "../../services/province-get-legal-person-unit-info";
|
||||
@@ -38,9 +31,8 @@ import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
|
||||
import { cityGetProvinces } from "../../../city/services/CityGetProvinces";
|
||||
import { cityGetCity } from "../../../city/services/city-get-city";
|
||||
|
||||
let preservedUserData = null;
|
||||
let preservedUserFound = false;
|
||||
let preservedNationalCode = "";
|
||||
const preservedUserData = null;
|
||||
const preservedNationalCode = "";
|
||||
|
||||
const extractCityFromAddress = (address) => {
|
||||
if (!address || typeof address !== "string") {
|
||||
@@ -386,12 +378,7 @@ const InquiryForm = ({ onInquiry, nationalCode, setNationalCode }) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const ProvinceLegalGuildsForm = ({
|
||||
onClose,
|
||||
updateTable,
|
||||
item,
|
||||
userType,
|
||||
}) => {
|
||||
export const ProvinceLegalGuildsForm = ({ updateTable, item, userType }) => {
|
||||
const [provinceData, setProvinceData] = useState();
|
||||
const [cityData, setCityData] = useState();
|
||||
|
||||
@@ -424,13 +411,17 @@ export const ProvinceLegalGuildsForm = ({
|
||||
const [userData, setUserData] = useState(
|
||||
item ? userInitialValue : preservedUserData
|
||||
);
|
||||
const [userFound, setUserFound] = useState(item ? true : preservedUserFound);
|
||||
const [typeActivities, setTypeActivities] = useState([]);
|
||||
|
||||
const currentRole = getRoleFromUrl();
|
||||
const isAdmin = currentRole === "AdminX";
|
||||
|
||||
useEffect(() => {
|
||||
if (preservedUserData) {
|
||||
setUserData(preservedUserData);
|
||||
setNationalCode(preservedNationalCode);
|
||||
}
|
||||
|
||||
dispatch(LOADING_START());
|
||||
dispatch(cityGetProvinces())
|
||||
?.then((r) => {
|
||||
@@ -444,51 +435,12 @@ export const ProvinceLegalGuildsForm = ({
|
||||
.finally(() => {
|
||||
dispatch(LOADING_END());
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(provinceGetTypeActivity()).then((r) => {
|
||||
setTypeActivities(r.payload.data || []);
|
||||
});
|
||||
}, [dispatch]);
|
||||
|
||||
// Restore preserved data on mount
|
||||
useEffect(() => {
|
||||
if (preservedUserData) {
|
||||
setUserData(preservedUserData);
|
||||
setUserFound(preservedUserFound);
|
||||
setNationalCode(preservedNationalCode);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Update modal size when userData is available (only once)
|
||||
const sizeUpdatedRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (userData && !sizeUpdatedRef.current) {
|
||||
sizeUpdatedRef.current = true;
|
||||
// Preserve state before remount
|
||||
preservedUserData = userData;
|
||||
preservedUserFound = userFound;
|
||||
preservedNationalCode = nationalCode;
|
||||
// Update modal with larger size
|
||||
dispatch(
|
||||
OPEN_MODAL({
|
||||
title: `${item ? "ویرایش" : "ثبت"} ${
|
||||
IS_STEWARD ? "مباشر" : "صنف"
|
||||
} حقوقی`,
|
||||
content: (
|
||||
<ProvinceLegalGuildsForm
|
||||
onClose={onClose}
|
||||
updateTable={updateTable}
|
||||
userType={userType}
|
||||
/>
|
||||
),
|
||||
size: { xs: "96vw", md: "90vw", lg: "600px" },
|
||||
})
|
||||
);
|
||||
}
|
||||
}, [userData, dispatch, onClose, updateTable, userFound, nationalCode]);
|
||||
|
||||
const handleInquiry = useCallback(() => {
|
||||
if (!nationalCode || nationalCode.length !== 11) {
|
||||
openNotif({
|
||||
@@ -502,7 +454,6 @@ export const ProvinceLegalGuildsForm = ({
|
||||
|
||||
dispatch(provinceGetLegalPersonUnitInfoService(nationalCode)).then((r) => {
|
||||
if (r.payload.error) {
|
||||
setUserFound(false);
|
||||
if (isAdmin) {
|
||||
openNotif({
|
||||
vertical: "top",
|
||||
@@ -543,7 +494,6 @@ export const ProvinceLegalGuildsForm = ({
|
||||
});
|
||||
}
|
||||
} else if (r.payload.data) {
|
||||
setUserFound(true);
|
||||
const extractedCity = extractCityFromAddress(
|
||||
r.payload.data.address || ""
|
||||
);
|
||||
|
||||
@@ -155,7 +155,19 @@ export const ProvinceLegalGuildsInProvince = ({ userType }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleOpenModal = () => {
|
||||
return (
|
||||
<Grid item gap={SPACING.SMALL} p={2} xs={12}>
|
||||
<Grid
|
||||
container
|
||||
justifyContent="start"
|
||||
alignItems="center"
|
||||
gap={SPACING.SMALL}
|
||||
mb={SPACING.SMALL}
|
||||
>
|
||||
<Button
|
||||
variant="contained"
|
||||
color="primary"
|
||||
onClick={() =>
|
||||
dispatch(
|
||||
OPEN_MODAL({
|
||||
title: `ثبت ${IS_STEWARD ? "مباشر جدید" : "صنف جدید"}`,
|
||||
@@ -166,21 +178,11 @@ export const ProvinceLegalGuildsInProvince = ({ userType }) => {
|
||||
userType={userType}
|
||||
/>
|
||||
),
|
||||
size: 400,
|
||||
size: "auto",
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Grid item gap={SPACING.SMALL} p={2} xs={12}>
|
||||
<Grid
|
||||
container
|
||||
justifyContent="start"
|
||||
alignItems="center"
|
||||
gap={SPACING.SMALL}
|
||||
mb={SPACING.SMALL}
|
||||
)
|
||||
}
|
||||
>
|
||||
<Button variant="contained" color="primary" onClick={handleOpenModal}>
|
||||
{`ثبت ${IS_STEWARD ? "مباشر" : "واحد صنفی"}`}
|
||||
</Button>
|
||||
<form onSubmit={handleSubmit}>
|
||||
|
||||
@@ -96,7 +96,6 @@ export const ProvinceLegalGuildsOperations = ({
|
||||
|
||||
dispatch(LOADING_END());
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
dispatch(LOADING_END());
|
||||
const errorMessage =
|
||||
error.response?.data?.result ||
|
||||
|
||||
Reference in New Issue
Block a user