Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 13 additions & 11 deletions src/components/ui/form-datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,21 @@ export function FormDateTimeInput({
</PopoverTrigger>
<PopoverContent>

<IonDatetime name={name}
<IonDatetime name={name}
presentation="date"
value={
form.getValues(name) ? form.getValues(name).toISOString() : new Date().toISOString()
}
max={maxDate ? maxDate.toISOString() : undefined}
value={
field.value instanceof Date
? field.value.toISOString().split('T')[0]
: field.value
? new Date(field.value).toISOString().split('T')[0]
: new Date().toISOString().split('T')[0]
}
max={maxDate ? maxDate.toISOString().split('T')[0] : undefined}
onIonChange={(e) => {
const date = new Date(e.detail.value as string || "");

if (date) {
form.setValue(name, date);
form.trigger(name);

const raw = e.detail.value as string;
if (raw) {
const date = new Date(raw);
field.onChange(date);
if (onChange)
onChange(e);
}
Expand Down
32 changes: 21 additions & 11 deletions src/pages/(auth)/dob/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { z } from "zod"
import {
Form,
} from "@/components/ui/form";
import { Controller } from "react-hook-form";
import OnboardFooter from "@/components/on-board/layout/footer";
import SubmitButton from "@/components/ui/submit-button";
import { Suspense, useEffect, useState } from "react";
Expand Down Expand Up @@ -132,17 +133,26 @@ export default function DobPage() {
</p>
}

<IonDatetime name='candidate_birth_date'
presentation="date"
value={form.getValues('candidate_birth_date')?.toISOString()}
onIonChange={(e) => {
const date = new Date(e.detail.value as string || "");
if (date)
form.setValue('candidate_birth_date', date);
form.trigger('candidate_birth_date');
}}
className="m-auto block"
></IonDatetime>
<Controller
control={form.control}
name="candidate_birth_date"
render={({ field }) => (
<IonDatetime
name="candidate_birth_date"
presentation="date"
value={field.value instanceof Date
? field.value.toISOString().split('T')[0]
: undefined}
Comment on lines +143 to +145

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The date of birth field isn't pre-populated on refresh because form.setValue is called with a string, but the IonDatetime component expects a Date object.
Severity: MEDIUM

Suggested Fix

Convert the date string received from the API into a Date object before calling form.setValue. For example, use new Date(res.candidate_birth_date) when setting the value for the candidate_birth_date field in the useEffect hook.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/pages/(auth)/dob/page.tsx#L143-L145

Potential issue: On page refresh, when the user profile is not in the Redux store, the
`useEffect` hook fetches the profile and sets the `candidate_birth_date` form value with
a string from the API response. The `IonDatetime` component's `value` is determined by
`field.value instanceof Date`. Since `field.value` is a string, this check fails, the
component's `value` prop becomes `undefined`, and the input field appears empty instead
of showing the user's saved date of birth.

Did we get this right? 👍 / 👎 to inform future reviews.

onIonChange={(e) => {
const raw = e.detail.value as string;
if (raw) {
field.onChange(new Date(raw));
}
}}
className="m-auto block"
/>
)}
/>


<SubmitButton disabled={!form.formState.isValid || loading } loading={loading}></SubmitButton>
Expand Down