mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-13 06:24:54 +10:00
5cd16a62d9
* refactor to @base-ui/react * fix all * fixes to accordion * more updates * switch to chat/completions api from openai * update version to v5.0.12
228 lines
6.5 KiB
TypeScript
228 lines
6.5 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 { Switch } from "@/components/ui/switch";
|
|
import type { DialogProps } from "@/dialogs/store";
|
|
import { useDialogStore } from "@/dialogs/store";
|
|
import { useFormBlocker } from "@/hooks/use-form-blocker";
|
|
import { projectItemSchema } from "@/schema/resume/data";
|
|
import { generateId } from "@/utils/string";
|
|
|
|
const formSchema = projectItemSchema;
|
|
|
|
type FormValues = z.infer<typeof formSchema>;
|
|
|
|
export function CreateProjectDialog({ data }: DialogProps<"resume.sections.projects.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,
|
|
options: data?.item?.options ?? { showLinkInTitle: false },
|
|
name: data?.item?.name ?? "",
|
|
period: data?.item?.period ?? "",
|
|
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.projects.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 project</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form className="grid gap-4 sm:grid-cols-2" onSubmit={form.handleSubmit(onSubmit)}>
|
|
<ProjectForm />
|
|
|
|
<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 UpdateProjectDialog({ data }: DialogProps<"resume.sections.projects.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,
|
|
options: data.item.options ?? { showLinkInTitle: false },
|
|
name: data.item.name,
|
|
period: data.item.period,
|
|
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.projects.items.findIndex((item) => item.id === formData.id);
|
|
if (index !== -1) draft.sections.projects.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 project</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form className="grid gap-4 sm:grid-cols-2" onSubmit={form.handleSubmit(onSubmit)}>
|
|
<ProjectForm />
|
|
|
|
<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 ProjectForm() {
|
|
const form = useFormContext<FormValues>();
|
|
|
|
return (
|
|
<>
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Name</Trans>
|
|
</FormLabel>
|
|
<FormControl render={<Input {...field} />} />
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="period"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
<Trans>Period</Trans>
|
|
</FormLabel>
|
|
<FormControl render={<Input {...field} />} />
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="website"
|
|
render={({ field }) => (
|
|
<FormItem className="sm:col-span-full">
|
|
<FormLabel>
|
|
<Trans>Website</Trans>
|
|
</FormLabel>
|
|
<URLInput
|
|
{...field}
|
|
value={field.value}
|
|
onChange={field.onChange}
|
|
hideLabelButton={form.watch("options.showLinkInTitle")}
|
|
/>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="options.showLinkInTitle"
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center gap-x-2 sm:col-span-full">
|
|
<FormControl render={<Switch checked={field.value} onCheckedChange={field.onChange} />} />
|
|
<FormLabel className="mt-0!">
|
|
<Trans>Show link in title</Trans>
|
|
</FormLabel>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<FormItem className="sm:col-span-full">
|
|
<FormLabel>
|
|
<Trans>Description</Trans>
|
|
</FormLabel>
|
|
<FormControl render={<RichInput {...field} value={field.value} onChange={field.onChange} />} />
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</>
|
|
);
|
|
}
|