80 lines
2.7 KiB
JavaScript
80 lines
2.7 KiB
JavaScript
import { useState } from "react";
|
|
import ButtonFixed from "../paying/ButtonFixed";
|
|
import { Button } from "@mui/material";
|
|
import ArrowLeftB from "@/components/icons/ArrowLeftB";
|
|
import { request } from "@/services/response";
|
|
|
|
function SubmitData({ setStep, data, prevData, setErrors }) {
|
|
const [loading, setLoading] = useState(false);
|
|
const newStep = () => setStep((prev) => prev + 1);
|
|
|
|
const handleSubmit = async () => {
|
|
const isChanged = JSON.stringify(prevData) !== JSON.stringify(data);
|
|
|
|
// پاک کردن error های قبلی
|
|
setErrors({});
|
|
|
|
if (isChanged) {
|
|
setLoading(true);
|
|
const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data;
|
|
|
|
const payload = {
|
|
name: name?.value,
|
|
family: family?.value,
|
|
national_code: national_code?.value,
|
|
gender: gender?.value,
|
|
insurance_id: insurance_id?.value,
|
|
basic_insurance: basic_insurance?.value?.id ? [basic_insurance.value.id] : [],
|
|
supplementary_insurance: supplementary_insurance?.value?.id ? [supplementary_insurance.value.id] : [],
|
|
};
|
|
|
|
try {
|
|
if (uuid) {
|
|
// اگر uuid داریم، پروفایل وجود داره و باید PATCH کنیم
|
|
console.log('📤 ارسال درخواست PATCH - UUID:', uuid, 'Payload:', payload);
|
|
await request.patchUserProfile(payload, uuid);
|
|
} else {
|
|
// اگر uuid نداریم، پروفایل وجود نداره و باید POST کنیم
|
|
console.log('📤 ارسال درخواست POST - Payload:', payload);
|
|
await request.postUserProfile(payload);
|
|
}
|
|
console.log('✅ پروفایل با موفقیت ذخیره شد');
|
|
setLoading(false);
|
|
newStep();
|
|
} catch (error) {
|
|
console.log('❌ خطا در ذخیره پروفایل:', error?.response?.data || error);
|
|
setLoading(false);
|
|
// اگر خطای validation بود، error ها را ست میکنیم
|
|
if (error?.response?.data) {
|
|
setErrors(error.response.data);
|
|
}
|
|
}
|
|
} else {
|
|
newStep();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ButtonFixed>
|
|
<Button
|
|
loading={loading}
|
|
className={`
|
|
!flex !w-full md:!w-fit !mt-0 md:!mt-[32px]
|
|
!gap-[4px] md:!mr-auto !px-[12px] !py-[8px] md:!py-[9px] !min-w-[150px]
|
|
`}
|
|
variant="contained"
|
|
onClick={handleSubmit}
|
|
>
|
|
<p
|
|
className={`${loading && "opacity-0"} text-[#EFEFEF] text-[16px] font-medium`}
|
|
>
|
|
ثبت اطلاعات
|
|
</p>
|
|
<ArrowLeftB />
|
|
</Button>
|
|
</ButtonFixed>
|
|
);
|
|
}
|
|
|
|
export default SubmitData;
|