mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-14 06:47:00 +10:00
3d1c2d1fb6
* feat: add useFormBlocker hook for dialog dirty state protection * feat: add useFormBlocker hook for dialog dirty state protection - Create useFormBlocker hook that blocks dialog closing when forms have unsaved changes - Use onPointerDownOutside and onEscapeKeyDown to intercept close attempts - Show confirmation dialog with Leave/Stay options using useConfirm - Integrate with CreateResumeDialog, UpdateResumeDialog, and DuplicateResumeDialog - All strings are translatable via Lingui * Feature: Create a new `useFormBlocker` hook to block the user from closing a dialog or navigating away from a page if the form is dirty.
233 lines
6.3 KiB
TypeScript
233 lines
6.3 KiB
TypeScript
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { Trans } from "@lingui/react/macro";
|
|
import { PencilSimpleLineIcon, PlusIcon } from "@phosphor-icons/react";
|
|
import { useForm, useFormContext } from "react-hook-form";
|
|
import type z from "zod";
|
|
import { RichInput } from "@/components/input/rich-input";
|
|
import { URLInput } from "@/components/input/url-input";
|
|
import { useResumeStore } from "@/components/resume/store/resume";
|
|
import { Button } from "@/components/ui/button";
|
|
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
|
|
import { Input } from "@/components/ui/input";
|
|
import type { DialogProps } from "@/dialogs/store";
|
|
import { useDialogStore } from "@/dialogs/store";
|
|
import { useFormBlocker } from "@/hooks/use-form-blocker";
|
|
import { certificationItemSchema } from "@/schema/resume/data";
|
|
import { generateId } from "@/utils/string";
|
|
|
|
const formSchema = certificationItemSchema;
|
|
|
|
type FormValues = z.infer<typeof formSchema>;
|
|
|
|
export function CreateCertificationDialog({ data }: DialogProps<"resume.sections.certifications.create">) {
|
|
const closeDialog = useDialogStore((state) => state.closeDialog);
|
|
const updateResumeData = useResumeStore((state) => state.updateResumeData);
|
|
|
|
const form = useForm<FormValues>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
id: generateId(),
|
|
hidden: data?.item?.hidden ?? false,
|
|
title: data?.item?.title ?? "",
|
|
issuer: data?.item?.issuer ?? "",
|
|
date: data?.item?.date ?? "",
|
|
website: data?.item?.website ?? { url: "", label: "" },
|
|
description: data?.item?.description ?? "",
|
|
},
|
|
});
|
|
|
|
const onSubmit = (formData: FormValues) => {
|
|
updateResumeData((draft) => {
|
|
if (data?.customSectionId) {
|
|
const section = draft.customSections.find((s) => s.id === data.customSectionId);
|
|
if (section) section.items.push(formData);
|
|
} else {
|
|
draft.sections.certifications.items.push(formData);
|
|
}
|
|
});
|
|
closeDialog();
|
|
};
|
|
|
|
const { blockEvents, requestClose } = useFormBlocker(form);
|
|
|
|
return (
|
|
<DialogContent {...blockEvents}>
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-x-2">
|
|
<PlusIcon />
|
|
<Trans>Create a new certification</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form className="grid gap-4 sm:grid-cols-2" onSubmit={form.handleSubmit(onSubmit)}>
|
|
<CertificationForm />
|
|
|
|
<DialogFooter className="sm:col-span-full">
|
|
<Button variant="ghost" onClick={requestClose}>
|
|
<Trans>Cancel</Trans>
|
|
</Button>
|
|
|
|
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
<Trans>Create</Trans>
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
);
|
|
}
|
|
|
|
export function UpdateCertificationDialog({ data }: DialogProps<"resume.sections.certifications.update">) {
|
|
const closeDialog = useDialogStore((state) => state.closeDialog);
|
|
const updateResumeData = useResumeStore((state) => state.updateResumeData);
|
|
|
|
const form = useForm<FormValues>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
id: data.item.id,
|
|
hidden: data.item.hidden,
|
|
title: data.item.title,
|
|
issuer: data.item.issuer,
|
|
date: data.item.date,
|
|
website: data.item.website,
|
|
description: data.item.description,
|
|
},
|
|
});
|
|
|
|
const onSubmit = (formData: FormValues) => {
|
|
updateResumeData((draft) => {
|
|
if (data?.customSectionId) {
|
|
const section = draft.customSections.find((s) => s.id === data.customSectionId);
|
|
if (!section) return;
|
|
const index = section.items.findIndex((item) => item.id === formData.id);
|
|
if (index !== -1) section.items[index] = formData;
|
|
} else {
|
|
const index = draft.sections.certifications.items.findIndex((item) => item.id === formData.id);
|
|
if (index !== -1) draft.sections.certifications.items[index] = formData;
|
|
}
|
|
});
|
|
closeDialog();
|
|
};
|
|
|
|
const { blockEvents, requestClose } = useFormBlocker(form);
|
|
|
|
return (
|
|
<DialogContent {...blockEvents}>
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-x-2">
|
|
<PencilSimpleLineIcon />
|
|
<Trans>Update an existing certification</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form className="grid gap-4 sm:grid-cols-2" onSubmit={form.handleSubmit(onSubmit)}>
|
|
<CertificationForm />
|
|
|
|
<DialogFooter className="sm:col-span-full">
|
|
<Button variant="ghost" onClick={requestClose}>
|
|
<Trans>Cancel</Trans>
|
|
</Button>
|
|
|
|
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
<Trans>Save Changes</Trans>
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
);
|
|
}
|
|
|
|
function CertificationForm() {
|
|
const form = useFormContext<FormValues>();
|
|
|
|
return (
|
|
<>
|
|
<FormField
|
|
control={form.control}
|
|
name="title"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Title</Trans>
|
|
</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="issuer"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Issuer</Trans>
|
|
</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="date"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Date</Trans>
|
|
</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="website"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Website</Trans>
|
|
</FormLabel>
|
|
<FormControl>
|
|
<URLInput {...field} value={field.value} onChange={field.onChange} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<FormItem className="sm:col-span-full">
|
|
<FormLabel>
|
|
<Trans>Description</Trans>
|
|
</FormLabel>
|
|
<FormControl>
|
|
<RichInput {...field} value={field.value} onChange={field.onChange} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</>
|
|
);
|
|
}
|