Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace age field with DOB while Updating User #7322

Merged
merged 9 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Components/Users/UserAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type UserForm = {
phone_number: string;
alt_phone_number: string;
phone_number_is_whatsapp: boolean;
age: number;
date_of_birth: Date | null;
state: number;
district: number;
Expand All @@ -91,7 +90,6 @@ const initForm: UserForm = {
phone_number: "+91",
alt_phone_number: "+91",
phone_number_is_whatsapp: true,
age: 0,
date_of_birth: null,
state: 0,
district: 0,
Expand Down Expand Up @@ -471,7 +469,12 @@ export const UserAdd = (props: UserProps) => {
return;
case "date_of_birth":
if (!state.form[field]) {
errors[field] = "Please enter date in YYYY/MM/DD format";
errors[field] = "Field is required";
invalidForm = true;
} else if (
dayjs(state.form[field]).isAfter(dayjs().subtract(1, "year"))
) {
errors[field] = "Enter a valid date of birth";
invalidForm = true;
}
return;
Expand Down Expand Up @@ -543,7 +546,6 @@ export const UserAdd = (props: UserProps) => {
: state.form.alt_phone_number
) ?? "",
date_of_birth: dateQueryString(state.form.date_of_birth),
age: Number(dayjs().diff(state.form.date_of_birth, "years", false)),
doctor_qualification:
state.form.user_type === "Doctor"
? state.form.doctor_qualification
Expand Down
51 changes: 33 additions & 18 deletions src/Components/Users/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { PhoneNumberValidator } from "../Form/FieldValidators";
import useQuery from "../../Utils/request/useQuery";
import routes from "../../Redux/api";
import request from "../../Utils/request/request";

import DateFormField from "../Form/FormFields/DateFormField";
const Loading = lazy(() => import("../Common/Loading"));

type EditForm = {
firstName: string;
lastName: string;
age: string;
date_of_birth: Date | null;
gender: GenderType;
email: string;
video_connect_link: string | undefined;
Expand All @@ -39,7 +39,7 @@ type EditForm = {
type ErrorForm = {
firstName: string;
lastName: string;
age: string;
date_of_birth: string | null;
gender: string;
email: string;
video_connect_link: string | undefined;
Expand All @@ -62,7 +62,7 @@ type Action =
const initForm: EditForm = {
firstName: "",
lastName: "",
age: "",
date_of_birth: null,
gender: "Male",
video_connect_link: "",
email: "",
Expand Down Expand Up @@ -145,7 +145,7 @@ export default function UserProfile() {
const formData: EditForm = {
firstName: result.data.first_name,
lastName: result.data.last_name,
age: result.data.age?.toString() || "",
date_of_birth: result.data.date_of_birth || null,
gender: result.data.gender || "Male",
email: result.data.email,
video_connect_link: result.data.video_connect_link,
Expand Down Expand Up @@ -188,15 +188,14 @@ export default function UserProfile() {
invalidForm = true;
}
return;
case "age":
case "date_of_birth":
if (!states.form[field]) {
errors[field] = "This field is required";
errors[field] = "Field is required";
invalidForm = true;
} else if (
Number(states.form[field]) <= 0 ||
!/^\d+$/.test(states.form[field])
dayjs(states.form[field]).isAfter(dayjs().subtract(1, "year"))
) {
errors[field] = "Age must be a number greater than 0";
errors[field] = "Enter a valid date of birth";
invalidForm = true;
}
return;
Expand Down Expand Up @@ -295,6 +294,20 @@ export default function UserProfile() {
form: { ...states.form, [event.name]: event.value },
});
};
const handleDateChange = (e: FieldChangeEvent<Date>) => {
if (dayjs(e.value).isValid()) {
dispatch({
type: "set_form",
form: {
...states.form,
[e.name]: dayjs(e.value).format("YYYY-MM-DD"),
},
});
}
};

const getDate = (value: any) =>
value && dayjs(value).isValid() && dayjs(value).toDate();

const fieldProps = (name: string) => {
return {
Expand All @@ -319,7 +332,7 @@ export default function UserProfile() {
phone_number: parsePhoneNumber(states.form.phoneNumber) ?? "",
alt_phone_number: parsePhoneNumber(states.form.altPhoneNumber) ?? "",
gender: states.form.gender,
age: +states.form.age,
date_of_birth: states.form.date_of_birth,
doctor_qualification:
states.form.user_type === "Doctor"
? states.form.doctor_qualification
Expand Down Expand Up @@ -520,10 +533,10 @@ export default function UserProfile() {
</div>
<div className="my-2 sm:col-span-1" id="age-profile-details">
<dt className="text-sm font-medium leading-5 text-black">
Age
Date of Birth
</dt>
<dd className="mt-1 text-sm leading-5 text-gray-900">
{userData?.age || "-"}
{userData?.date_of_birth?.toString() || "-"}
</dd>
</div>
<div className="my-2 sm:col-span-1">
Expand Down Expand Up @@ -647,13 +660,15 @@ export default function UserProfile() {
label="Last name"
className="col-span-6 sm:col-span-3"
/>
<TextFormField
{...fieldProps("age")}
<DateFormField
{...fieldProps("date_of_birth")}
label="Date of Birth"
required
label="Age"
className="col-span-6 sm:col-span-3"
type="number"
min={1}
value={getDate(states.form.date_of_birth)}
onChange={handleDateChange}
position="LEFT"
disableFuture={true}
/>
<SelectFormField
{...fieldProps("gender")}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Users/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type UserModel = UserBareMinimum & {
phone_number?: string;
alt_phone_number?: string;
gender?: GenderType;
age?: number;
date_of_birth: Date | null;
is_superuser?: boolean;
verified?: boolean;
home_facility?: string;
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface UserAssignedModel extends UserBareMinimum {
alt_phone_number?: string;
video_connect_link: string;
gender?: number;
age?: number;
date_of_birth: Date | null;
is_superuser?: boolean;
verified?: boolean;
home_facility?: string;
Expand Down
Loading