Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(profile): add copy resume dialog

Khuram Niaz2209b82340f8
diff --git a/__tests__/CopyResumeDialog.spec.tsx b/__tests__/CopyResumeDialog.spec.tsx
new file mode 100644
index 0000000..a7ae7aa
--- /dev/null
+++ b/__tests__/CopyResumeDialog.spec.tsx
@@ -0,0 +1,121 @@
+import CopyResumeDialog from "@/components/profile/CopyResumeDialog";
+import { ProfileDocument } from "@/models/profile.model";
+import { render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import {
+ copyResume,
+ getResumeCopyTitleSuggestion,
+} from "@/actions/profile.actions";
+
+vi.mock("@/actions/profile.actions", () => ({
+ copyResume: vi.fn(),
+ getResumeCopyTitleSuggestion: vi.fn(),
+}));
+
+const mockToast = vi.fn();
+vi.mock("@/components/ui/use-toast", () => ({
+ toast: (args: any) => mockToast(args),
+}));
+
+const sourceDoc: ProfileDocument = {
+ id: "resume-1",
+ title: "Backend Resume",
+ type: "resume",
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ jobCount: 0,
+ sectionCount: 4,
+};
+
+describe("CopyResumeDialog", () => {
+ const setOpen = vi.fn();
+ const onCopied = vi.fn();
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (getResumeCopyTitleSuggestion as any).mockResolvedValue({
+ success: true,
+ data: "Backend Resume (Copy)",
+ });
+ (copyResume as any).mockResolvedValue({
+ success: true,
+ data: { id: "resume-2", title: "Backend Resume (Copy)" },
+ });
+ });
+
+ const renderDialog = () =>
+ render(
+ <CopyResumeDialog
+ open={true}
+ setOpen={setOpen}
+ sourceDoc={sourceDoc}
+ onCopied={onCopied}
+ />,
+ );
+
+ it("pre-fills the suggested title when opened", async () => {
+ renderDialog();
+
+ expect(getResumeCopyTitleSuggestion).toHaveBeenCalledWith("resume-1");
+ await waitFor(() =>
+ expect(screen.getByTestId("copy-resume-title-input")).toHaveValue(
+ "Backend Resume (Copy)",
+ ),
+ );
+ });
+
+ it("states that the attachment is not copied", async () => {
+ renderDialog();
+ expect(screen.getByText(/attachment is not copied/i)).toBeInTheDocument();
+ });
+
+ it("submits the copy and notifies the parent on success", async () => {
+ const user = userEvent.setup();
+ renderDialog();
+
+ await waitFor(() =>
+ expect(screen.getByTestId("copy-resume-title-input")).toHaveValue(
+ "Backend Resume (Copy)",
+ ),
+ );
+ await user.click(screen.getByRole("button", { name: /create copy/i }));
+
+ await waitFor(() =>
+ expect(copyResume).toHaveBeenCalledWith(
+ "resume-1",
+ "Backend Resume (Copy)",
+ ),
+ );
+ await waitFor(() => expect(onCopied).toHaveBeenCalled());
+ expect(setOpen).toHaveBeenCalledWith(false);
+ expect(mockToast).toHaveBeenCalledWith(
+ expect.objectContaining({ variant: "success" }),
+ );
+ });
+
+ it("shows a destructive toast and stays open when the copy fails", async () => {
+ (copyResume as any).mockResolvedValue({
+ success: false,
+ message: "Failed to copy resume.",
+ });
+ const user = userEvent.setup();
+ renderDialog();
+
+ await waitFor(() =>
+ expect(screen.getByTestId("copy-resume-title-input")).toHaveValue(
+ "Backend Resume (Copy)",
+ ),
+ );
+ await user.click(screen.getByRole("button", { name: /create copy/i }));
+
+ await waitFor(() =>
+ expect(mockToast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ description: "Failed to copy resume.",
+ }),
+ ),
+ );
+ expect(onCopied).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/components/profile/CopyResumeDialog.tsx b/src/components/profile/CopyResumeDialog.tsx
new file mode 100644
index 0000000..22c9847
--- /dev/null
+++ b/src/components/profile/CopyResumeDialog.tsx
@@ -0,0 +1,163 @@
+"use client";
+import { Loader } from "lucide-react";
+import { useEffect, useState, useTransition } from "react";
+import { useForm } from "react-hook-form";
+import { z } from "zod";
+import { zodResolver } from "@hookform/resolvers/zod";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "../ui/dialog";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "../ui/form";
+import { Input } from "../ui/input";
+import { Button } from "../ui/button";
+import { toastError, toastSuccess } from "@/lib/toast";
+import { CopyResumeFormSchema } from "@/models/copyResumeForm.schema";
+import { ProfileDocument } from "@/models/profile.model";
+import {
+ copyResume,
+ getResumeCopyTitleSuggestion,
+} from "@/actions/profile.actions";
+
+type CopyResumeDialogProps = {
+ open: boolean;
+ setOpen: (open: boolean) => void;
+ sourceDoc: ProfileDocument | null;
+ onCopied: () => void;
+};
+
+function CopyResumeDialog({
+ open,
+ setOpen,
+ sourceDoc,
+ onCopied,
+}: CopyResumeDialogProps) {
+ const [isPending, startTransition] = useTransition();
+ const [loadingTitle, setLoadingTitle] = useState(false);
+
+ const form = useForm<z.infer<typeof CopyResumeFormSchema>>({
+ resolver: zodResolver(CopyResumeFormSchema),
+ mode: "onChange",
+ defaultValues: { title: "" },
+ });
+
+ const {
+ reset,
+ formState: { isValid },
+ } = form;
+
+ // The suggestion needs every title the user owns, which the paginated
+ // table does not have — so ask the server each time the dialog opens.
+ useEffect(() => {
+ if (!open || !sourceDoc?.id) return;
+ let cancelled = false;
+ setLoadingTitle(true);
+ reset({ title: "" });
+ (async () => {
+ const { success, data, message } = await getResumeCopyTitleSuggestion(
+ sourceDoc.id,
+ );
+ if (cancelled) return;
+ if (success && data) {
+ reset({ title: data });
+ } else {
+ toastError(message);
+ }
+ setLoadingTitle(false);
+ })();
+ return () => {
+ cancelled = true;
+ };
+ }, [open, sourceDoc?.id, reset]);
+
+ const onSubmit = (values: z.infer<typeof CopyResumeFormSchema>) => {
+ if (!sourceDoc?.id) return;
+ startTransition(async () => {
+ const { success, data, message } = await copyResume(
+ sourceDoc.id,
+ values.title,
+ );
+ if (!success) {
+ toastError(message);
+ return;
+ }
+ reset({ title: "" });
+ setOpen(false);
+ onCopied();
+ toastSuccess(`Copy created: "${data.title}"`);
+ });
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={setOpen}>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Create a copy</DialogTitle>
+ <DialogDescription>
+ Copies the sections and contact info of &quot;{sourceDoc?.title}
+ &quot;. The attachment is not copied.
+ </DialogDescription>
+ </DialogHeader>
+ <Form {...form}>
+ <form
+ onSubmit={(event) => {
+ event.stopPropagation();
+ form.handleSubmit(onSubmit)(event);
+ }}
+ className="grid grid-cols-1 gap-4 p-2"
+ >
+ <FormField
+ control={form.control}
+ name="title"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Resume Title</FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ disabled={loadingTitle}
+ data-testid="copy-resume-title-input"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <DialogFooter>
+ <Button
+ type="button"
+ variant="outline"
+ className="mt-2 md:mt-0"
+ onClick={() => setOpen(false)}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="submit"
+ disabled={!isValid || isPending || loadingTitle}
+ >
+ Create copy
+ {isPending && (
+ <Loader className="h-4 w-4 shrink-0 animate-spin" />
+ )}
+ </Button>
+ </DialogFooter>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ );
+}
+
+export default CopyResumeDialog;
diff --git a/src/models/copyResumeForm.schema.ts b/src/models/copyResumeForm.schema.ts
new file mode 100644
index 0000000..600eb02
--- /dev/null
+++ b/src/models/copyResumeForm.schema.ts
@@ -0,0 +1,8 @@
+import { z } from "zod";
+
+export const CopyResumeFormSchema = z.object({
+ title: z
+ .string()
+ .min(1, "Resume title is required.")
+ .max(100, "Title must be less than 100 characters"),
+});