With the backend now returning 200 (lazy-created profile) instead of 404, align the consumers: the profile lives at res.data.data (the endpoint double-nests), so the booking detail and dashboard read that instead of res.data / the raw envelope. Drop the obsolete 404 special handling (empty editable form now comes from the 200 payload), seed the dashboard empty state when the profile has no real data yet, and make the profile POST fall back to PATCH on 409 (already lazy-created). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
111 lines
3.0 KiB
JavaScript
111 lines
3.0 KiB
JavaScript
import { useEffect, useState } from "react";
|
|
import Form from "./form";
|
|
import { request } from "@/services/response";
|
|
import ButtonSendData from "./ButtonSendData";
|
|
import { changeDateType, getParsedUserInfo } from "@/helper";
|
|
import { disease } from "./form/disease";
|
|
import LoadingComponent from "@/app/component/LoadingComponent";
|
|
|
|
function Information({ information, setInformation }) {
|
|
const [loading, setLoading] = useState(false);
|
|
const [insurance, setInsurance] = useState();
|
|
const [errors, setErrors] = useState({});
|
|
const [loadingInfo, setLoadingInfo] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const parsedUserInfo = getParsedUserInfo();
|
|
|
|
request.getInsuranceType().then((res) => {
|
|
const items = res?.data?.data;
|
|
if (Array.isArray(items)) {
|
|
setInsurance(items);
|
|
}
|
|
});
|
|
|
|
if (parsedUserInfo) {
|
|
request
|
|
.getUserProfile(parsedUserInfo.uuid)
|
|
.then((res) => {
|
|
setLoadingInfo(false);
|
|
const profile = res?.data?.data;
|
|
|
|
const hasProfileData = Boolean(
|
|
profile && (profile.label || profile.family || profile.national_code)
|
|
);
|
|
|
|
if (hasProfileData) {
|
|
let changedData = { ...profile };
|
|
changedData.basic_insurance = [Number(profile?.basic_insurance?.id)];
|
|
changedData.prev_data = true;
|
|
changedData = changeDateType(changedData, false);
|
|
setInformation(changedData);
|
|
} else {
|
|
setInformation({
|
|
prev_data: false,
|
|
other: {
|
|
disease: disease,
|
|
allergies: [],
|
|
medications: [],
|
|
surgeries: [],
|
|
family_history: [],
|
|
relatives: [],
|
|
},
|
|
});
|
|
}
|
|
})
|
|
.catch(() => {
|
|
setLoadingInfo(false);
|
|
setInformation({
|
|
prev_data: false,
|
|
other: {
|
|
disease: disease,
|
|
allergies: [],
|
|
medications: [],
|
|
surgeries: [],
|
|
family_history: [],
|
|
relatives: [],
|
|
},
|
|
});
|
|
});
|
|
} else {
|
|
setLoadingInfo(false);
|
|
}
|
|
}, []);
|
|
|
|
const changeData = (value, name) => {
|
|
setInformation({
|
|
...information,
|
|
[name]: value,
|
|
});
|
|
if (errors && errors[name]) {
|
|
setErrors((prevErrors) => ({
|
|
...prevErrors,
|
|
[name]: false,
|
|
}));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<LoadingComponent loading={loadingInfo}>
|
|
<div className="opacity-page">
|
|
<Form
|
|
errors={errors}
|
|
insurance={insurance}
|
|
changeData={changeData}
|
|
information={information}
|
|
/>
|
|
<ButtonSendData
|
|
setInformation={setInformation}
|
|
information={information}
|
|
setLoading={setLoading}
|
|
setErrors={setErrors}
|
|
loading={loading}
|
|
errors={errors}
|
|
/>
|
|
</div>
|
|
</LoadingComponent>
|
|
);
|
|
}
|
|
|
|
export default Information;
|