Skip to content

Commit 5c43c3e

Browse files
committed
modify CreateUserForm and add dirtyState to all react-hook-form
1 parent 68c4c3d commit 5c43c3e

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

src/components/Encounter/CreateEncounterForm.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export default function CreateEncounterForm({
145145
},
146146
});
147147

148+
const {
149+
formState: { isDirty },
150+
} = form;
151+
148152
const { mutate: createEncounter } = useMutation({
149153
mutationFn: mutate(routes.encounter.create),
150154
onSuccess: (data: Encounter) => {
@@ -318,7 +322,7 @@ export default function CreateEncounterForm({
318322
}}
319323
/>
320324

321-
<Button type="submit" className="w-full">
325+
<Button type="submit" disabled={!isDirty} className="w-full">
322326
Create Encounter
323327
</Button>
324328
</form>

src/components/Facility/FacilityCreate.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ export const FacilityCreate = (props: FacilityProps) => {
137137
},
138138
});
139139

140+
const {
141+
formState: { isDirty },
142+
} = form;
143+
140144
// Update form when facility data is loaded
141145
useEffect(() => {
142146
if (facilityData) {
@@ -469,7 +473,11 @@ export const FacilityCreate = (props: FacilityProps) => {
469473
>
470474
{t("cancel")}
471475
</Button>
472-
<Button variant="primary" type="submit" disabled={isLoading}>
476+
<Button
477+
variant="primary"
478+
type="submit"
479+
disabled={isLoading || !isDirty}
480+
>
473481
{isLoading ? (
474482
<Loading />
475483
) : facilityId ? (

src/components/Schedule/ScheduleExceptionForm.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export default function ScheduleExceptionForm({ user, onRefresh }: Props) {
7373
},
7474
});
7575

76+
const {
77+
formState: { isDirty },
78+
} = form;
79+
7680
const {
7781
mutate: createException,
7882
isPending,
@@ -252,7 +256,11 @@ export default function ScheduleExceptionForm({ user, onRefresh }: Props) {
252256
Cancel
253257
</Button>
254258
</SheetClose>
255-
<Button variant="primary" type="submit" disabled={isPending}>
259+
<Button
260+
variant="primary"
261+
type="submit"
262+
disabled={!isDirty || isPending}
263+
>
256264
Confirm Unavailability
257265
</Button>
258266
</SheetFooter>

src/components/Users/CreateUserForm.tsx

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
SelectValue,
2424
} from "@/components/ui/select";
2525

26+
import DateFormField from "@/components/Form/FormFields/DateFormField";
27+
2628
import { GENDER_TYPES } from "@/common/constants";
2729

2830
import * as Notification from "@/Utils/Notifications";
@@ -74,8 +76,10 @@ const userFormSchema = z
7476
)
7577
.optional(),
7678
phone_number_is_whatsapp: z.boolean().default(true),
77-
date_of_birth: z.string().min(1, "Date of birth is required"),
78-
gender: z.enum(["male", "female", "other"]),
79+
date_of_birth: z.date({
80+
required_error: "Date of birth is required",
81+
}),
82+
gender: z.enum(["male", "female", "transgender", "non_binary"]),
7983
qualification: z.string().optional(),
8084
doctor_experience_commenced_on: z.string().optional(),
8185
doctor_medical_council_registration: z.string().optional(),
@@ -96,6 +100,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
96100
const { t } = useTranslation();
97101

98102
const form = useForm<UserFormValues>({
103+
mode: "onChange",
99104
resolver: zodResolver(userFormSchema),
100105
defaultValues: {
101106
user_type: "staff",
@@ -106,6 +111,10 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
106111
},
107112
});
108113

114+
const {
115+
formState: { isDirty, errors, isValid },
116+
} = form;
117+
109118
const userType = form.watch("user_type");
110119
const phoneNumber = form.watch("phone_number");
111120
const isWhatsApp = form.watch("phone_number_is_whatsapp");
@@ -146,6 +155,8 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
146155
});
147156
}
148157
};
158+
console.log(!!errors, errors);
159+
const required = <span className="text-red-500">*</span>;
149160

150161
return (
151162
<Form {...form}>
@@ -180,7 +191,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
180191
name="first_name"
181192
render={({ field }) => (
182193
<FormItem>
183-
<FormLabel>First Name</FormLabel>
194+
<FormLabel>First Name{required}</FormLabel>
184195
<FormControl>
185196
<Input placeholder="First name" {...field} />
186197
</FormControl>
@@ -194,7 +205,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
194205
name="last_name"
195206
render={({ field }) => (
196207
<FormItem>
197-
<FormLabel>Last Name</FormLabel>
208+
<FormLabel>Last Name{required}</FormLabel>
198209
<FormControl>
199210
<Input placeholder="Last name" {...field} />
200211
</FormControl>
@@ -209,7 +220,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
209220
name="username"
210221
render={({ field }) => (
211222
<FormItem>
212-
<FormLabel>Username</FormLabel>
223+
<FormLabel>Username{required}</FormLabel>
213224
<FormControl>
214225
<Input placeholder="Username" {...field} />
215226
</FormControl>
@@ -224,7 +235,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
224235
name="password"
225236
render={({ field }) => (
226237
<FormItem>
227-
<FormLabel>Password</FormLabel>
238+
<FormLabel>Password{required}</FormLabel>
228239
<FormControl>
229240
<Input type="password" placeholder="Password" {...field} />
230241
</FormControl>
@@ -238,7 +249,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
238249
name="c_password"
239250
render={({ field }) => (
240251
<FormItem>
241-
<FormLabel>Confirm Password</FormLabel>
252+
<FormLabel>Confirm Password{required}</FormLabel>
242253
<FormControl>
243254
<Input
244255
type="password"
@@ -257,7 +268,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
257268
name="email"
258269
render={({ field }) => (
259270
<FormItem>
260-
<FormLabel>Email</FormLabel>
271+
<FormLabel>Email{required}</FormLabel>
261272
<FormControl>
262273
<Input type="email" placeholder="Email" {...field} />
263274
</FormControl>
@@ -272,9 +283,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
272283
name="phone_number"
273284
render={({ field }) => (
274285
<FormItem>
275-
<FormLabel>Phone Number</FormLabel>
286+
<FormLabel>Phone Number{required}</FormLabel>
276287
<FormControl>
277-
<Input type="tel" placeholder="+91XXXXXXXXXX" {...field} />
288+
<Input
289+
type="tel"
290+
placeholder="+91XXXXXXXXXX"
291+
maxLength={13}
292+
{...field}
293+
/>
278294
</FormControl>
279295
<FormMessage />
280296
</FormItem>
@@ -286,12 +302,13 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
286302
name="alt_phone_number"
287303
render={({ field }) => (
288304
<FormItem>
289-
<FormLabel>Alternative Phone Number</FormLabel>
305+
<FormLabel>WhatsApp Number{required}</FormLabel>
290306
<FormControl>
291307
<Input
292308
placeholder="+91XXXXXXXXXX"
293309
type="tel"
294310
{...field}
311+
maxLength={13}
295312
disabled={isWhatsApp}
296313
/>
297314
</FormControl>
@@ -325,9 +342,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
325342
name="date_of_birth"
326343
render={({ field }) => (
327344
<FormItem>
328-
<FormLabel>Date of Birth</FormLabel>
345+
<FormLabel>Date of Birth{required}</FormLabel>
329346
<FormControl>
330-
<Input type="date" {...field} />
347+
<DateFormField
348+
name="date_of_birth"
349+
disableFuture
350+
value={field.value}
351+
onChange={(date) => field.onChange(date.value)}
352+
/>
331353
</FormControl>
332354
<FormMessage />
333355
</FormItem>
@@ -437,7 +459,11 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
437459
)}
438460
/>
439461

440-
<Button type="submit" className="w-full">
462+
<Button
463+
type="submit"
464+
className="w-full"
465+
disabled={!isDirty || !isValid}
466+
>
441467
Create User
442468
</Button>
443469
</form>

src/pages/Appoinments/PatientRegistration.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ export function PatientRegistration(props: PatientRegistrationProps) {
139139
defaultValues: initialForm,
140140
});
141141

142+
const {
143+
formState: { isDirty },
144+
} = form;
145+
142146
const { mutate: createAppointment } = useMutation({
143147
mutationFn: (body: AppointmentCreate) =>
144148
mutate(PublicAppointmentApi.createAppointment, {
@@ -448,6 +452,7 @@ export function PatientRegistration(props: PatientRegistrationProps) {
448452
variant="primary_gradient"
449453
className="sm:w-1/5"
450454
type="submit"
455+
disabled={!isDirty}
451456
>
452457
<span className="bg-gradient-to-b from-white/15 to-transparent" />
453458
{t("register_patient")}

0 commit comments

Comments
 (0)