fix(appointment): improve error handling and validation for national code in SubmitData component
This commit is contained in:
@@ -26,6 +26,20 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, isForAnother,
|
||||
}
|
||||
};
|
||||
|
||||
// پاسخ خطای backend: { success:false, errors:[{ code, field, message }] }
|
||||
// به نگاشت field→message تبدیل میشود تا inline در فرم نمایش داده شود.
|
||||
const fieldErrorsFromResponse = (error) => {
|
||||
const list = error?.response?.data?.errors;
|
||||
if (!Array.isArray(list)) return {};
|
||||
return list.reduce((acc, e) => {
|
||||
if (e?.field && e?.message) acc[e.field] = e.message;
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
const isNationalCodeTaken = (error) =>
|
||||
error?.response?.data?.errors?.some((e) => e?.code === "ERR_PROFILE_001");
|
||||
|
||||
const validate = () => {
|
||||
const errs = {};
|
||||
const nationalCode = (data?.national_code?.value || "").trim();
|
||||
@@ -101,8 +115,9 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, isForAnother,
|
||||
try {
|
||||
await request.postUserProfile(payload);
|
||||
} catch (err) {
|
||||
// 409 با کد ERR_PROFILE_001 یعنی کد ملی تکراری، نه «پروفایل موجود» → باید throw شود تا inline نمایش یابد
|
||||
// پروفایل از قبل ساخته شده (lazy-create در سمت backend) → بهجای ساخت، ویرایش
|
||||
if (err?.response?.status === 409 && userUuid) {
|
||||
if (err?.response?.status === 409 && userUuid && !isNationalCodeTaken(err)) {
|
||||
await request.patchUserProfile(payload, userUuid);
|
||||
} else {
|
||||
throw err;
|
||||
@@ -148,13 +163,17 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, isForAnother,
|
||||
newStep();
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
|
||||
const fieldErrors = fieldErrorsFromResponse(error);
|
||||
if (Object.keys(fieldErrors).length > 0) {
|
||||
setErrors(fieldErrors);
|
||||
return;
|
||||
}
|
||||
|
||||
if (error?.response?.status === 409) {
|
||||
setStep(0);
|
||||
return;
|
||||
}
|
||||
if (error?.response?.data) {
|
||||
setErrors(error.response.data);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user