change fields component & handle edit, delete, add data and save & change functional page doctor

This commit is contained in:
Ehsan
2025-07-02 01:58:44 +03:30
parent 43eb9c2b78
commit 04d3e38859
22 changed files with 290 additions and 313 deletions
@@ -0,0 +1,109 @@
import ArrowLeftWhiteD from "@/components/icons/ArrowLeftWhiteD";
import {
handleTimeExpiresToken,
removeAdditionalKeysDashboard,
} from "@/helper";
import { request } from "@/services/response";
import { Button } from "@mui/material";
import Cookies from "js-cookie";
function ButtonSendData({
loading,
errors,
information,
setInformation,
setLoading,
setErrors,
}) {
const handleErrReq = (err) => {
const errors = err?.response?.data;
if (errors) {
setErrors(typeof errors === "string" ? {} : errors);
}
setLoading(false);
};
const postData = () => {
request
.postUserProfile(information)
.then((response) => {
if (response.uuid) {
const formData = new URLSearchParams();
formData.append("grant_type", "refresh_token");
formData.append("client_id", process.env.NEXT_PUBLIC_CLIENT_ID);
formData.append(
"client_secret",
process.env.NEXT_PUBLIC_CLIENT_SECRET
);
formData.append("refresh_token", Cookies.get("refresh_token"));
request
.postRefreshToken(formData)
.then((res) => {
const expiresTime = handleTimeExpiresToken(res.expires_in);
Cookies.set("access_token", res.access_token, {
expires: expiresTime.accessTokenExpires,
path: "/",
secure: true,
sameSite: "strict",
});
setInformation({
...information,
prev_data: true,
uuid: response.uuid,
});
setLoading(false);
})
.catch((err) => {
setLoading(false);
});
}
})
.catch((err) => {
handleErrReq(err);
});
};
const patchData = () => {
const usedKays = removeAdditionalKeysDashboard(information);
request
.patchUserProfile(
usedKays,
information?.uuid,
Cookies.get("access_token")
)
.then(() => {
setLoading(false);
})
.catch((err) => handleErrReq(err));
};
const sendReq = () => {
setLoading(true);
information.prev_data ? patchData() : postData();
};
const isDisabled = () => {
return loading || Object.values(errors).some(Boolean) || !information;
};
return (
<Button
variant="contained"
onClick={sendReq}
loading={loading}
disabled={isDisabled()}
className={`!gap-[4px] !mr-auto !text-nowrap !flex !flex-nowrap !py-[8px]
!rounded-[4px] !text-[14px] md:!text-[16px] !font-medium
!mt-[32px] sm:!mt-[38px] md:!mt-[43px] lg:!mt-[48px] !px-[16px] sm:!px-[19px] md:!px-[22px] lg:!px-[24px]
`}
>
ثبت اطلاعات
<ArrowLeftWhiteD />
</Button>
);
}
export default ButtonSendData;
@@ -11,20 +11,14 @@ import {
job,
} from "./listSelector";
function Form({
data,
userInfo,
insurance,
errors,
prevInfo,
changeData,
prevData,
}) {
function Form({ insurance, errors, changeData, information }) {
const findVal = (list, name) =>
data && list && list.length && list.find((g) => g.id === data[name]);
console.log(errors);
list &&
list.length &&
information &&
information[name] &&
list.find((g) => g.id === information[name]);
const parsedUserInfo = getParsedUserInfo();
console.log(parsedUserInfo);
return (
<div className="grid grid-cols-1 lg:grid-cols-2 gap-x-[24px] gap-y-[10px]">
@@ -36,7 +30,7 @@ function Form({
name="work_phone"
isTransparent={true}
changeData={changeData}
data={userInfo?.username}
data={parsedUserInfo?.username}
/>
</Content>
<Content error={errors?.national_code} title="کد ملی" isConfirmed={false}>
@@ -47,8 +41,8 @@ function Form({
name="national_code"
isTransparent={true}
changeData={changeData}
data={data?.national_code}
disable={prevInfo && prevInfo.national_code}
data={information?.national_code}
disable={information?.prev_data}
/>
</Content>
<Content error={errors?.name} title="نام" isConfirmed={false}>
@@ -57,7 +51,7 @@ function Form({
isTransparent={true}
iconGray={true}
changeData={changeData}
data={data?.name}
data={information?.name}
name="name"
/>
</Content>
@@ -67,7 +61,7 @@ function Form({
isTransparent={true}
iconGray={true}
changeData={changeData}
data={data?.family}
data={information?.family}
name="family"
/>
</Content>
@@ -86,7 +80,7 @@ function Form({
<Content error={errors?.birthday} title="تاریخ تولد">
<DateBirthday
error={errors?.birthday}
data={data?.birthday}
data={information?.birthday}
isConfirmed={false}
changeData={changeData}
/>
@@ -97,18 +91,23 @@ function Form({
isTransparent={true}
iconGray={true}
changeData={changeData}
data={data?.fathers_name}
data={information?.fathers_name}
name="fathers_name"
/>
</Content>
<Content title="نوع بیمه">
<Content error={errors?.basic_insurance} title="نوع بیمه">
<AutoSelector
error={errors?.basic_insurance}
updateData={(name, val) => {
changeData([Number(val.id)], name);
}}
// value={findVal(insurance, "basic_insurance", true)?.name}
value={insurance?.find(
(g) => Number(g.id) === data.basic_insurance[0]
(g) =>
Number(g.id) ===
(information?.basic_insurance
? information?.basic_insurance[0]
: false)
)}
name="basic_insurance"
label="نوع بیمه"
@@ -126,9 +125,9 @@ function Form({
iconGray={true}
type="number"
changeData={changeData}
data={data?.insurance_id}
data={information?.insurance_id}
name="insurance_id"
// data={data?.supplementary_insurance}
// data={information?.supplementary_insurance}
// name="supplementary_insurance"
/>
</Content>
@@ -162,7 +161,7 @@ function Form({
iconGray={true}
type="number"
changeData={changeData}
data={data?.home_phone}
data={information?.home_phone}
name="home_phone"
/>
</Content>
@@ -170,10 +169,7 @@ function Form({
<AutoSelector
error={errors?.education}
updateData={(name, value) =>
changeData(
education.find((g) => g.label === value).id,
name
)
changeData(education.find((g) => g.label === value).id, name)
}
value={findVal(education, "education")}
label="تحصیلات"
@@ -199,7 +195,7 @@ function Form({
type="number"
iconGray={true}
changeData={changeData}
data={data?.work_phone}
data={information?.work_phone}
name="work_phone"
/>
</Content>
@@ -1,4 +1,4 @@
export const dieses = [
export const disease = [
{
id: 1,
name: "فشار خون",
@@ -1,34 +1,49 @@
import { useEffect, useState } from "react";
import Form from "./Form";
import { Button } from "@mui/material";
import ArrowLeftWhiteD from "@/components/icons/ArrowLeftWhiteD";
import { request } from "@/services/response";
import Cookies from "js-cookie";
import ButtonSendData from "./ButtonSendData";
import { getParsedUserInfo } from "@/helper";
import { disease } from "../information/disease";
function Information({
user,
data,
setData,
userInfo,
prevInfo,
dataUser,
initialData,
information,
setInformation,
updateDataUser,
}) {
const [disable, setDisable] = useState(true);
const [insurance, setInsurance] = useState();
function Information({ information, setInformation }) {
const [loading, setLoading] = useState(false);
const [insurance, setInsurance] = useState();
const [errors, setErrors] = useState({});
useEffect(() => {
const parsedUserInfo = getParsedUserInfo();
request.getInsuranceType().then((res) => {
if (res && res.data) {
setInsurance(res.data);
}
});
// .catch((err) => {});
if (parsedUserInfo) {
request
.getUserProfile(parsedUserInfo.uuid)
.then((res) => {
const changedData = res;
changedData.basic_insurance = [Number(res?.basic_insurance?.id)];
changedData.prev_data = true;
setInformation(changedData);
})
.catch((err) => {
if (err?.response?.data === "Entity not found") {
setInformation({
prev_data: false,
other: {
disease: disease,
allergies: [],
medications: [],
surgeries: [],
family_history: [],
relatives: [],
},
});
}
});
}
}, []);
const changeData = (value, name) => {
@@ -37,70 +52,29 @@ function Information({
[name]: value,
});
if (errors && errors[name]) {
setErrors({
...errors,
setErrors((prevErrors) => ({
...prevErrors,
[name]: false,
});
}));
}
};
console.log(information);
const validationInfo = () => {};
const sendReq = () => {
setLoading(true);
console.log(prevInfo);
console.log(prevInfo ? true : false);
console.log(information);
if (!validationInfo())
request[prevInfo ? "patchUserProfile" : "postUserProfile"](
information,
prevInfo ? prevInfo.uuid : Cookies.get("uuid")
)
.then((res) => {
console.log(res);
if (!dataUser) {
updateDataUser();
}
setLoading(false);
})
.catch((err) => {
console.log(err);
console.log(err?.response?.data);
if (err?.response?.data) {
setErrors(err.response.data);
}
setLoading(false);
});
};
return (
<div className="opacity-page">
<Form
data={data}
errors={errors}
userInfo={userInfo}
prevInfo={prevInfo}
prevData={dataUser}
insurance={insurance}
changeData={changeData}
information={information}
/>
<Button
variant="contained"
onClick={sendReq}
<ButtonSendData
setInformation={setInformation}
information={information}
setLoading={setLoading}
setErrors={setErrors}
loading={loading}
disabled={Object.values(errors).find((error) => error)}
className={`!gap-[4px] !mr-auto !text-nowrap !flex !flex-nowrap !py-[8px]
!rounded-[4px] !text-[14px] md:!text-[16px] !font-medium
!mt-[32px] sm:!mt-[38px] md:!mt-[43px] lg:!mt-[48px] !px-[16px] sm:!px-[19px] md:!px-[22px] lg:!px-[24px]
`}
>
ثبت اطلاعات
<ArrowLeftWhiteD />
</Button>
errors={errors}
/>
</div>
);
}