mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-06-22 04:11:55 +10:00
231 lines
6.6 KiB
TypeScript
231 lines
6.6 KiB
TypeScript
import type z from "zod";
|
|
import type { DialogProps } from "@/dialogs/store";
|
|
import { Trans } from "@lingui/react/macro";
|
|
import { PencilSimpleLineIcon, PlusIcon } from "@phosphor-icons/react";
|
|
import { useStore } from "@tanstack/react-form";
|
|
import { awardItemSchema } from "@reactive-resume/schema/resume/data";
|
|
import { Button } from "@reactive-resume/ui/components/button";
|
|
import {
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@reactive-resume/ui/components/dialog";
|
|
import { FormControl, FormItem, FormLabel, FormMessage } from "@reactive-resume/ui/components/form";
|
|
import { Input } from "@reactive-resume/ui/components/input";
|
|
import { Switch } from "@reactive-resume/ui/components/switch";
|
|
import { RichInput } from "@/components/input/rich-input";
|
|
import { URLInput } from "@/components/input/url-input";
|
|
import { useDialogStore } from "@/dialogs/store";
|
|
import { useUpdateResumeData } from "@/features/resume/builder/draft";
|
|
import { useFormBlocker } from "@/hooks/use-form-blocker";
|
|
import { makeSectionItem } from "@/libs/resume/make-section-item";
|
|
import { createSectionItem, updateSectionItem } from "@/libs/resume/section-actions";
|
|
import { useAppForm, withForm } from "@/libs/tanstack-form";
|
|
|
|
const formSchema = awardItemSchema;
|
|
|
|
type FormValues = z.infer<typeof formSchema>;
|
|
|
|
const defaultValues: FormValues = {
|
|
id: "",
|
|
hidden: false,
|
|
title: "",
|
|
awarder: "",
|
|
date: "",
|
|
website: { url: "", label: "", inlineLink: false },
|
|
description: "",
|
|
};
|
|
|
|
export function CreateAwardDialog({ data }: DialogProps<"resume.sections.awards.create">) {
|
|
const closeDialog = useDialogStore((state) => state.closeDialog);
|
|
const updateResumeData = useUpdateResumeData();
|
|
|
|
const form = useAppForm({
|
|
defaultValues: makeSectionItem(defaultValues, data?.item),
|
|
validators: { onSubmit: formSchema },
|
|
onSubmit: async ({ value }) => {
|
|
updateResumeData((draft) => {
|
|
createSectionItem(draft, "awards", value, data?.customSectionId);
|
|
});
|
|
closeDialog();
|
|
},
|
|
});
|
|
|
|
const { requestClose } = useFormBlocker(form);
|
|
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
|
|
|
|
return (
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-x-2">
|
|
<PlusIcon />
|
|
<Trans>Create a new award</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<form
|
|
className="grid gap-4 sm:grid-cols-2"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
void form.handleSubmit();
|
|
}}
|
|
>
|
|
<AwardForm form={form} />
|
|
|
|
<DialogFooter className="sm:col-span-full">
|
|
<Button variant="ghost" onClick={requestClose}>
|
|
<Trans>Cancel</Trans>
|
|
</Button>
|
|
|
|
<Button type="submit" disabled={isSubmitting}>
|
|
<Trans>Create</Trans>
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
);
|
|
}
|
|
|
|
export function UpdateAwardDialog({ data }: DialogProps<"resume.sections.awards.update">) {
|
|
const closeDialog = useDialogStore((state) => state.closeDialog);
|
|
const updateResumeData = useUpdateResumeData();
|
|
|
|
const form = useAppForm({
|
|
defaultValues: data.item,
|
|
validators: { onSubmit: formSchema },
|
|
onSubmit: async ({ value }) => {
|
|
updateResumeData((draft) => {
|
|
updateSectionItem(draft, "awards", value, data?.customSectionId);
|
|
});
|
|
closeDialog();
|
|
},
|
|
});
|
|
|
|
const { requestClose } = useFormBlocker(form);
|
|
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
|
|
|
|
return (
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-x-2">
|
|
<PencilSimpleLineIcon />
|
|
<Trans>Update an existing award</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
|
|
<form
|
|
className="grid gap-4 sm:grid-cols-2"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
void form.handleSubmit();
|
|
}}
|
|
>
|
|
<AwardForm form={form} />
|
|
|
|
<DialogFooter className="sm:col-span-full">
|
|
<Button variant="ghost" onClick={requestClose}>
|
|
<Trans>Cancel</Trans>
|
|
</Button>
|
|
|
|
<Button type="submit" disabled={isSubmitting}>
|
|
<Trans>Save Changes</Trans>
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
);
|
|
}
|
|
|
|
const AwardForm = withForm({
|
|
defaultValues,
|
|
render: function AwardFormRenderer({ form }) {
|
|
const inlineLink = useStore(form.store, (s) => s.values.website.inlineLink);
|
|
|
|
return (
|
|
<>
|
|
<form.AppField name="title">{(field) => <field.TextField label={<Trans>Title</Trans>} />}</form.AppField>
|
|
|
|
<form.Field name="awarder">
|
|
{(field) => (
|
|
<FormItem hasError={field.state.meta.isTouched && field.state.meta.errors.length > 0}>
|
|
<FormLabel>
|
|
<Trans context="(noun) person, organization, or entity that gives an award">Awarder</Trans>
|
|
</FormLabel>
|
|
<FormControl
|
|
render={
|
|
<Input
|
|
name={field.name}
|
|
value={field.state.value}
|
|
onBlur={field.handleBlur}
|
|
onChange={(event) => field.handleChange(event.target.value)}
|
|
/>
|
|
}
|
|
/>
|
|
<FormMessage errors={field.state.meta.errors} />
|
|
</FormItem>
|
|
)}
|
|
</form.Field>
|
|
|
|
<form.AppField name="date">{(field) => <field.TextField label={<Trans>Date</Trans>} />}</form.AppField>
|
|
|
|
<form.Field name="website">
|
|
{(field) => (
|
|
<FormItem hasError={field.state.meta.isTouched && field.state.meta.errors.length > 0}>
|
|
<FormLabel>
|
|
<Trans>Website</Trans>
|
|
</FormLabel>
|
|
<URLInput
|
|
value={field.state.value}
|
|
onChange={(v) => field.handleChange(v)}
|
|
hideLabelButton={inlineLink}
|
|
/>
|
|
<FormMessage errors={field.state.meta.errors} />
|
|
</FormItem>
|
|
)}
|
|
</form.Field>
|
|
|
|
<form.Field name="website.inlineLink">
|
|
{(field) => (
|
|
<FormItem className="flex items-center gap-x-2">
|
|
<FormControl
|
|
render={
|
|
<Switch
|
|
checked={field.state.value}
|
|
onCheckedChange={(checked: boolean) => {
|
|
field.handleChange(checked);
|
|
}}
|
|
/>
|
|
}
|
|
/>
|
|
<FormLabel className="mt-0!">
|
|
<Trans>Show link in title</Trans>
|
|
</FormLabel>
|
|
</FormItem>
|
|
)}
|
|
</form.Field>
|
|
|
|
<form.Field name="description">
|
|
{(field) => (
|
|
<FormItem
|
|
className="sm:col-span-full"
|
|
hasError={field.state.meta.isTouched && field.state.meta.errors.length > 0}
|
|
>
|
|
<FormLabel>
|
|
<Trans>Description</Trans>
|
|
</FormLabel>
|
|
<FormControl render={<RichInput value={field.state.value} onChange={(v) => field.handleChange(v)} />} />
|
|
<FormMessage errors={field.state.meta.errors} />
|
|
</FormItem>
|
|
)}
|
|
</form.Field>
|
|
</>
|
|
);
|
|
},
|
|
});
|