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

Modify CreateUserForm and add dirtyState to all react-hook-form #9749

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5c43c3e
modify CreateUserForm and add dirtyState to all react-hook-form
rajku-dev Jan 4, 2025
dbe957c
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 4, 2025
332ca54
fine dob field | drop destructuring
rajku-dev Jan 5, 2025
ec6e72e
use DatePicker instead
rajku-dev Jan 5, 2025
ad412ce
pass disable callback to date-picker
rajku-dev Jan 5, 2025
ababd0d
adjust disable logic
rajku-dev Jan 5, 2025
e924e69
handle api errors gracefully
rajku-dev Jan 5, 2025
3a7772e
fix negative age
rajku-dev Jan 6, 2025
33d899a
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 6, 2025
e421cfc
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 6, 2025
9d41d9d
fix `createEncounterForm` lint
rajku-dev Jan 6, 2025
804feff
Merge branch 'issue/9733/dirty-state' of https://github.com/rajku-dev…
rajku-dev Jan 6, 2025
4124583
use `useMutation` | add translations | refactor asterisk
rajku-dev Jan 7, 2025
5cf7fc3
use `toast` instead
rajku-dev Jan 7, 2025
0750df8
move schema out of component
rajku-dev Jan 7, 2025
88a4bce
Revert "move schema out of component"
rajku-dev Jan 7, 2025
642f7c1
extract precise API errors for fields
rajku-dev Jan 7, 2025
84dafdb
add translations for all field errors
rajku-dev Jan 7, 2025
3bb013c
reuse existing translations
rajku-dev Jan 7, 2025
42c1d58
switch to mutate
rajku-dev Jan 7, 2025
4299e3d
remove async
rajku-dev Jan 7, 2025
9382901
change TBody for UserApi.create route
rajku-dev Jan 7, 2025
9f9a10b
drop explicit type-safing
rajku-dev Jan 7, 2025
0908316
use relative imports
rajku-dev Jan 7, 2025
e73b85f
move new UserCreateRequest type to src/types/user
rajku-dev Jan 8, 2025
af44829
infer schema type directly
rajku-dev Jan 8, 2025
e134196
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 8, 2025
add37a5
refactor `UserCreateRequest` type
rajku-dev Jan 8, 2025
fb1d00b
Merge branch 'issue/9733/dirty-state' of https://github.com/rajku-dev…
rajku-dev Jan 8, 2025
743c430
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 8, 2025
5392459
edit schema | manually pick API payload fields from formValues
rajku-dev Jan 8, 2025
ad9b270
Merge branch 'develop' into issue/9733/dirty-state
rajku-dev Jan 8, 2025
aaa7d41
Revert "Merge branch 'develop' into issue/9733/dirty-state"
rajku-dev Jan 8, 2025
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
6 changes: 5 additions & 1 deletion src/components/Encounter/CreateEncounterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export default function CreateEncounterForm({
},
});

const {
formState: { isDirty },
} = form;

rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
const { mutate: createEncounter } = useMutation({
mutationFn: mutate(routes.encounter.create),
onSuccess: (data: Encounter) => {
Expand Down Expand Up @@ -318,7 +322,7 @@ export default function CreateEncounterForm({
}}
/>

<Button type="submit" className="w-full">
<Button type="submit" disabled={!isDirty} className="w-full">
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
Create Encounter
</Button>
</form>
Expand Down
10 changes: 9 additions & 1 deletion src/components/Facility/FacilityCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export const FacilityCreate = (props: FacilityProps) => {
},
});

const {
formState: { isDirty },
} = form;

rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
// Update form when facility data is loaded
useEffect(() => {
if (facilityData) {
Expand Down Expand Up @@ -469,7 +473,11 @@ export const FacilityCreate = (props: FacilityProps) => {
>
{t("cancel")}
</Button>
<Button variant="primary" type="submit" disabled={isLoading}>
<Button
variant="primary"
type="submit"
disabled={isLoading || !isDirty}
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
>
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
{isLoading ? (
<Loading />
) : facilityId ? (
Expand Down
10 changes: 9 additions & 1 deletion src/components/Schedule/ScheduleExceptionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export default function ScheduleExceptionForm({ user, onRefresh }: Props) {
},
});

const {
formState: { isDirty },
} = form;

rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
const {
mutate: createException,
isPending,
Expand Down Expand Up @@ -252,7 +256,11 @@ export default function ScheduleExceptionForm({ user, onRefresh }: Props) {
Cancel
</Button>
</SheetClose>
<Button variant="primary" type="submit" disabled={isPending}>
<Button
variant="primary"
type="submit"
disabled={!isDirty || isPending}
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
>
Confirm Unavailability
</Button>
</SheetFooter>
Expand Down
54 changes: 40 additions & 14 deletions src/components/Users/CreateUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
SelectValue,
} from "@/components/ui/select";

import DateFormField from "@/components/Form/FormFields/DateFormField";

import { GENDER_TYPES } from "@/common/constants";

import * as Notification from "@/Utils/Notifications";
Expand Down Expand Up @@ -74,8 +76,10 @@ const userFormSchema = z
)
.optional(),
phone_number_is_whatsapp: z.boolean().default(true),
date_of_birth: z.string().min(1, "Date of birth is required"),
gender: z.enum(["male", "female", "other"]),
date_of_birth: z.date({
required_error: "Date of birth is required",
}),
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
gender: z.enum(["male", "female", "transgender", "non_binary"]),
qualification: z.string().optional(),
doctor_experience_commenced_on: z.string().optional(),
doctor_medical_council_registration: z.string().optional(),
Expand All @@ -96,6 +100,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
const { t } = useTranslation();

const form = useForm<UserFormValues>({
mode: "onChange",
resolver: zodResolver(userFormSchema),
defaultValues: {
user_type: "staff",
Expand All @@ -106,6 +111,10 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
},
});

const {
formState: { isDirty, errors, isValid },
} = form;

rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
const userType = form.watch("user_type");
const phoneNumber = form.watch("phone_number");
const isWhatsApp = form.watch("phone_number_is_whatsapp");
Expand Down Expand Up @@ -146,6 +155,8 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
});
}
};
console.log(!!errors, errors);
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
const required = <span className="text-red-500">*</span>;
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved

return (
<Form {...form}>
Expand Down Expand Up @@ -180,7 +191,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="first_name"
render={({ field }) => (
<FormItem>
<FormLabel>First Name</FormLabel>
<FormLabel>First Name{required}</FormLabel>
<FormControl>
<Input placeholder="First name" {...field} />
</FormControl>
Expand All @@ -194,7 +205,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="last_name"
render={({ field }) => (
<FormItem>
<FormLabel>Last Name</FormLabel>
<FormLabel>Last Name{required}</FormLabel>
<FormControl>
<Input placeholder="Last name" {...field} />
</FormControl>
Expand All @@ -209,7 +220,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormLabel>Username{required}</FormLabel>
<FormControl>
<Input placeholder="Username" {...field} />
</FormControl>
Expand All @@ -224,7 +235,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormLabel>Password{required}</FormLabel>
<FormControl>
<Input type="password" placeholder="Password" {...field} />
</FormControl>
Expand All @@ -238,7 +249,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="c_password"
render={({ field }) => (
<FormItem>
<FormLabel>Confirm Password</FormLabel>
<FormLabel>Confirm Password{required}</FormLabel>
<FormControl>
<Input
type="password"
Expand All @@ -257,7 +268,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormLabel>Email{required}</FormLabel>
<FormControl>
<Input type="email" placeholder="Email" {...field} />
</FormControl>
Expand All @@ -272,9 +283,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="phone_number"
render={({ field }) => (
<FormItem>
<FormLabel>Phone Number</FormLabel>
<FormLabel>Phone Number{required}</FormLabel>
<FormControl>
<Input type="tel" placeholder="+91XXXXXXXXXX" {...field} />
<Input
type="tel"
placeholder="+91XXXXXXXXXX"
maxLength={13}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand All @@ -286,12 +302,13 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="alt_phone_number"
render={({ field }) => (
<FormItem>
<FormLabel>Alternative Phone Number</FormLabel>
<FormLabel>WhatsApp Number{required}</FormLabel>
<FormControl>
<Input
placeholder="+91XXXXXXXXXX"
type="tel"
{...field}
maxLength={13}
disabled={isWhatsApp}
/>
</FormControl>
Expand Down Expand Up @@ -325,9 +342,14 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
name="date_of_birth"
render={({ field }) => (
<FormItem>
<FormLabel>Date of Birth</FormLabel>
<FormLabel>Date of Birth{required}</FormLabel>
<FormControl>
<Input type="date" {...field} />
<DateFormField
name="date_of_birth"
disableFuture
value={field.value}
onChange={(date) => field.onChange(date.value)}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand Down Expand Up @@ -437,7 +459,11 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) {
)}
/>

<Button type="submit" className="w-full">
<Button
type="submit"
className="w-full"
disabled={!isDirty || !isValid}
>
Create User
</Button>
</form>
Expand Down
5 changes: 5 additions & 0 deletions src/pages/Appoinments/PatientRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export function PatientRegistration(props: PatientRegistrationProps) {
defaultValues: initialForm,
});

const {
formState: { isDirty },
} = form;

rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
const { mutate: createAppointment } = useMutation({
mutationFn: (body: AppointmentCreate) =>
mutate(PublicAppointmentApi.createAppointment, {
Expand Down Expand Up @@ -448,6 +452,7 @@ export function PatientRegistration(props: PatientRegistrationProps) {
variant="primary_gradient"
className="sm:w-1/5"
type="submit"
disabled={!isDirty}
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved
>
<span className="bg-gradient-to-b from-white/15 to-transparent" />
{t("register_patient")}
Expand Down
Loading