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(dashboard): set daily goal targets from a dialog opened by the calendar's settings button

Khuram Niazd5d2c9dfcf07
diff --git a/__tests__/DailyGoalsCalendar.spec.tsx b/__tests__/DailyGoalsCalendar.spec.tsx
index 02bbe6a..287a5a2 100644
--- a/__tests__/DailyGoalsCalendar.spec.tsx
+++ b/__tests__/DailyGoalsCalendar.spec.tsx
@@ -1,15 +1,24 @@
-import { act, render, waitFor, within } from "@testing-library/react";
+import { act, render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import DailyGoalsCalendar from "@/components/dashboard/DailyGoalsCalendar";
-import { getDailyGoalValues } from "@/actions/dailyGoals.actions";
+import {
+ getDailyGoalValues,
+ updateGoalTargets,
+} from "@/actions/dailyGoals.actions";
import { toastError } from "@/lib/toast";
vi.mock("@/actions/dailyGoals.actions", () => ({
getDailyGoalValues: vi.fn(),
+ updateGoalTargets: vi.fn(),
}));
vi.mock("@/lib/toast", () => ({
+ toastSuccess: vi.fn(),
toastError: vi.fn(),
+ toastActionResult: vi.fn((result, opts) => {
+ if (result?.success) opts.onSuccess?.(result.data);
+ else (toastError as any)(result?.message);
+ }),
}));
const deferred = <T,>() => {
@@ -256,4 +265,80 @@ describe("DailyGoalsCalendar", () => {
// Sep has no entry: carried from March, not the 4 h default
expect(calendar().getByText("7 h")).toBeInTheDocument();
});
+
+ const setGoals = () => calendar().getByRole("button", { name: "Set goals" });
+
+ it("places Set goals inside the calendar", () => {
+ renderCalendar();
+ // data-gc-actions wraps the package's settings button
+ expect(cards().at(-1)!.querySelector("[data-gc-actions]")).toContainElement(
+ setGoals(),
+ );
+ });
+
+ it("applies saved targets and streaks without refetching values", async () => {
+ (updateGoalTargets as any).mockResolvedValue({
+ success: true,
+ data: {
+ targets: { "2026-10": { jobs: 3, hours: 2 } },
+ streaks: { current: 6, best: 11 },
+ },
+ });
+ renderCalendar();
+
+ await user.click(setGoals());
+ const jobs = await screen.findByLabelText("Jobs applied per day");
+ await user.clear(jobs);
+ await user.type(jobs, "3");
+ const hours = screen.getByLabelText("Activity hours per day");
+ await user.clear(hours);
+ await user.type(hours, "2");
+ await user.click(screen.getByRole("button", { name: "Save goals" }));
+
+ expect(
+ await calendar().findByText(/October 1: Jobs applied 3 of 3, Activity 2\.5 of 2 h\. All goals met\./),
+ ).toBeInTheDocument();
+ // "Both goals met" is now 1 day; "6 days" is the new current streak
+ expect(summary().getByText("6 days")).toBeInTheDocument();
+ expect(getDailyGoalValues).not.toHaveBeenCalled();
+ });
+
+ it("hides Set goals when no targets have loaded", () => {
+ render(
+ <DailyGoalsCalendar today={today} initialData={null} initialStreaks={null} />,
+ );
+ expect(
+ calendar().queryByRole("button", { name: "Set goals" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("keeps saved targets when an older month fetch lands", async () => {
+ const sep = deferred<any>();
+ (getDailyGoalValues as any).mockReturnValueOnce(sep.promise);
+ (updateGoalTargets as any).mockResolvedValue({
+ success: true,
+ data: {
+ targets: { "2026-10": { jobs: 3, hours: 2 } },
+ streaks: { current: 2, best: 11 },
+ },
+ });
+ renderCalendar();
+
+ await user.click(previous());
+ await user.click(setGoals());
+ const jobs = await screen.findByLabelText("Jobs applied per day");
+ await user.clear(jobs);
+ await user.type(jobs, "3");
+ const hours = screen.getByLabelText("Activity hours per day");
+ await user.clear(hours);
+ await user.type(hours, "2");
+ await user.click(screen.getByRole("button", { name: "Save goals" }));
+ // This read happened before the save, so it carries the old (empty) map
+ await act(async () => sep.resolve(ok()));
+
+ await user.click(next());
+ expect(
+ calendar().getByText(/October 1: Jobs applied 3 of 3, Activity 2\.5 of 2 h\. All goals met\./),
+ ).toBeInTheDocument();
+ });
});
diff --git a/__tests__/SetGoalsDialog.spec.tsx b/__tests__/SetGoalsDialog.spec.tsx
new file mode 100644
index 0000000..ca3caad
--- /dev/null
+++ b/__tests__/SetGoalsDialog.spec.tsx
@@ -0,0 +1,160 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import SetGoalsDialog from "@/components/dashboard/SetGoalsDialog";
+import { updateGoalTargets } from "@/actions/dailyGoals.actions";
+import { toastError, toastSuccess } from "@/lib/toast";
+
+vi.mock("@/actions/dailyGoals.actions", () => ({
+ updateGoalTargets: vi.fn(),
+}));
+
+vi.mock("@/lib/toast", () => ({
+ toastSuccess: vi.fn(),
+ toastError: vi.fn(),
+ toastActionResult: vi.fn((result, opts) => {
+ if (result?.success) opts.onSuccess?.(result.data);
+ else (toastError as any)(result?.message);
+ }),
+}));
+
+describe("SetGoalsDialog", () => {
+ const user = userEvent.setup();
+ const onSaved = vi.fn();
+ const onOpenChange = vi.fn();
+
+ const renderDialog = () =>
+ render(
+ <SetGoalsDialog
+ open
+ onOpenChange={onOpenChange}
+ month="2026-10"
+ targets={{ jobs: 5, hours: 4 }}
+ onSaved={onSaved}
+ />,
+ );
+ const jobs = () => screen.getByLabelText("Jobs applied per day");
+ const hours = () => screen.getByLabelText("Activity hours per day");
+ const save = () => screen.getByRole("button", { name: "Save goals" });
+
+ it("pre-fills the current targets and names the month", () => {
+ renderDialog();
+ expect(jobs()).toHaveValue(5);
+ expect(hours()).toHaveValue(4);
+ expect(
+ screen.getByText(/Applies from October 2026 and carries forward/),
+ ).toBeInTheDocument();
+ // The pill; the description holds the month inside a longer sentence
+ expect(screen.getByText("October 2026")).toBeInTheDocument();
+ expect(screen.getByText("1 to 50")).toBeInTheDocument();
+ expect(screen.getByText("1 to 24")).toBeInTheDocument();
+ });
+
+ it("disables Save until something changes", async () => {
+ renderDialog();
+ expect(save()).toBeDisabled();
+ await user.clear(jobs());
+ await user.type(jobs(), "3");
+ expect(save()).toBeEnabled();
+ });
+
+ it("steps a value and shows what it was", async () => {
+ renderDialog();
+ await user.click(screen.getByRole("button", { name: "Fewer jobs" }));
+ await user.click(screen.getByRole("button", { name: "Fewer jobs" }));
+ expect(jobs()).toHaveValue(3);
+ expect(screen.getByText("Was 5 · now 3")).toBeInTheDocument();
+ expect(screen.queryByText("1 to 50")).not.toBeInTheDocument();
+ expect(save()).toBeEnabled();
+
+ await user.click(screen.getByRole("button", { name: "More jobs" }));
+ await user.click(screen.getByRole("button", { name: "More jobs" }));
+ // Back to the pre-fill: the range returns
+ expect(screen.getByText("1 to 50")).toBeInTheDocument();
+ });
+
+ it("clamps the stepper at the limits", async () => {
+ render(
+ <SetGoalsDialog
+ open
+ onOpenChange={onOpenChange}
+ month="2026-10"
+ targets={{ jobs: 1, hours: 24 }}
+ onSaved={onSaved}
+ />,
+ );
+ expect(screen.getByRole("button", { name: "Fewer jobs" })).toBeDisabled();
+ expect(screen.getByRole("button", { name: "More hours" })).toBeDisabled();
+ expect(screen.getByRole("button", { name: "More jobs" })).toBeEnabled();
+ });
+
+ it("steps a cleared field from the minimum", async () => {
+ renderDialog();
+ await user.clear(hours());
+ await user.click(screen.getByRole("button", { name: "More hours" }));
+ expect(hours()).toHaveValue(1);
+ });
+
+ it("shows the error as soon as a field is invalid", async () => {
+ renderDialog();
+ await user.clear(jobs());
+ expect(await screen.findByText("Enter a number.")).toBeInTheDocument();
+ expect(screen.queryByText(/now NaN/)).not.toBeInTheDocument();
+ await user.type(jobs(), "51");
+ expect(await screen.findByText("Must be at most 50.")).toBeInTheDocument();
+ expect(screen.queryByText(/now 51/)).not.toBeInTheDocument();
+ });
+
+ it("saves the current month and hands back the result", async () => {
+ const data = {
+ targets: { "2026-10": { jobs: 3, hours: 4 } },
+ streaks: { current: 1, best: 4 },
+ };
+ (updateGoalTargets as any).mockResolvedValue({ success: true, data });
+ renderDialog();
+
+ await user.clear(jobs());
+ await user.type(jobs(), "3");
+ await user.click(save());
+
+ await waitFor(() => expect(onSaved).toHaveBeenCalledWith(data));
+ expect(updateGoalTargets).toHaveBeenCalledWith({
+ month: "2026-10",
+ jobs: 3,
+ hours: 4,
+ });
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+
+ it.each([
+ ["", "Enter a number."],
+ ["0", "Must be at least 1."],
+ ["51", "Must be at most 50."],
+ ["2.5", "Use a whole number."],
+ ])("blocks invalid input without saving (%s)", async (input, message) => {
+ renderDialog();
+ await user.clear(jobs());
+ if (input) await user.type(jobs(), input);
+ await user.click(save());
+ expect(await screen.findByText(message)).toBeInTheDocument();
+ expect(updateGoalTargets).not.toHaveBeenCalled();
+ });
+
+ it("stays open with the input kept when the save fails", async () => {
+ (updateGoalTargets as any).mockResolvedValue({
+ success: false,
+ message: "The month has changed — reload the page.",
+ });
+ renderDialog();
+ await user.clear(hours());
+ await user.type(hours(), "6");
+ await user.click(save());
+
+ await waitFor(() =>
+ expect(toastError).toHaveBeenCalledWith(
+ "The month has changed — reload the page.",
+ ),
+ );
+ expect(onOpenChange).not.toHaveBeenCalledWith(false);
+ expect(hours()).toHaveValue(6);
+ });
+});
diff --git a/src/components/dashboard/DailyGoalsCalendar.tsx b/src/components/dashboard/DailyGoalsCalendar.tsx
index 7b0acf5..264a494 100644
--- a/src/components/dashboard/DailyGoalsCalendar.tsx
+++ b/src/components/dashboard/DailyGoalsCalendar.tsx
@@ -8,6 +8,7 @@ import {
} from "react-goal-calendar";
import { getDailyGoalValues } from "@/actions/dailyGoals.actions";
import { Card } from "@/components/ui/card";
+import SetGoalsDialog from "@/components/dashboard/SetGoalsDialog";
import {
GOAL_IDS,
buildMonthSummary,
@@ -31,6 +32,8 @@ const RINGS = [
{ id: GOAL_IDS.HOURS, label: "Activity" },
];
+const CALENDAR_LABELS = { settings: "Set goals" };
+
const goalsFor = (targets: GoalTargets): GcGoal[] => [
{ ...RINGS[0], target: targets.jobs },
{ ...RINGS[1], target: targets.hours, unit: "h" },
@@ -59,6 +62,12 @@ export default function DailyGoalsCalendar({
const targetsFor = (key: string) => resolveTargets(key, storedTargets ?? {});
const [streaks, setStreaks] = useState(initialStreaks);
const [loadingMonth, setLoadingMonth] = useState<string | null>(null);
+ const [dialogOpen, setDialogOpen] = useState(false);
+ // Stable identity: the dialog re-applies its pre-fill when this changes
+ const currentTargets = useMemo(
+ () => resolveTargets(maxMonth, storedTargets ?? {}),
+ [maxMonth, storedTargets],
+ );
// Only cached months get an entry; the rest show the package's "—"
const summaries = useMemo(() => {
@@ -114,9 +123,24 @@ export default function DailyGoalsCalendar({
goals={goalsFor(targetsFor(month))}
values={valuesByMonth[month]}
legend
+ labels={CALENDAR_LABELS}
+ // No button until targets load: the pre-fill would be defaults
+ onSettingsClick={
+ storedTargets ? () => setDialogOpen(true) : undefined
+ }
{...monthProps}
/>
</Card>
+ <SetGoalsDialog
+ open={dialogOpen}
+ onOpenChange={setDialogOpen}
+ month={maxMonth}
+ targets={currentTargets}
+ onSaved={(data) => {
+ setStoredTargets(data.targets);
+ setStreaks(data.streaks);
+ }}
+ />
</>
);
}
diff --git a/src/components/dashboard/SetGoalsDialog.tsx b/src/components/dashboard/SetGoalsDialog.tsx
new file mode 100644
index 0000000..ebaf69e
--- /dev/null
+++ b/src/components/dashboard/SetGoalsDialog.tsx
@@ -0,0 +1,148 @@
+"use client";
+import { useEffect, useTransition } from "react";
+import { useForm } from "react-hook-form";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { format } from "date-fns";
+import { Info, Loader } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+import { Form } from "@/components/ui/form";
+import GoalRow from "@/components/dashboard/set-goals-dialog/GoalRow";
+import { updateGoalTargets } from "@/actions/dailyGoals.actions";
+import { DAILY_GOALS } from "@/lib/constants";
+import { toastActionResult } from "@/lib/toast";
+import {
+ GoalTargetsSchema,
+ type GoalStreaks,
+ type GoalTargets,
+ type GoalTargetsByMonth,
+} from "@/models/dailyGoals.model";
+
+interface SetGoalsDialogProps {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ month: string;
+ targets: GoalTargets;
+ onSaved: (data: {
+ targets: GoalTargetsByMonth;
+ streaks: GoalStreaks | null;
+ }) => void;
+}
+
+const FIELDS = [
+ {
+ name: "jobs",
+ label: "Jobs applied per day",
+ noun: "jobs",
+ ring: "var(--gc-ring-1)",
+ min: DAILY_GOALS.JOBS_MIN,
+ max: DAILY_GOALS.JOBS_MAX,
+ },
+ {
+ name: "hours",
+ label: "Activity hours per day",
+ noun: "hours",
+ ring: "var(--gc-ring-2)",
+ min: DAILY_GOALS.HOURS_MIN,
+ max: DAILY_GOALS.HOURS_MAX,
+ },
+] as const;
+
+export default function SetGoalsDialog({
+ open,
+ onOpenChange,
+ month,
+ targets,
+ onSaved,
+}: SetGoalsDialogProps) {
+ const [isPending, startTransition] = useTransition();
+ const form = useForm<GoalTargets>({
+ resolver: zodResolver(GoalTargetsSchema),
+ defaultValues: targets,
+ // The sub-line shows the error while typing, not only after Save
+ mode: "onChange",
+ });
+ const { reset } = form;
+
+ // Re-open on the latest targets, not a previous edit
+ useEffect(() => {
+ if (open) reset(targets);
+ }, [open, targets, reset]);
+
+ const onSubmit = (values: GoalTargets) =>
+ startTransition(async () => {
+ const result = await updateGoalTargets({ month, ...values });
+ toastActionResult(result, {
+ success: "Daily goals saved.",
+ onSuccess: (data) => {
+ if (!data) return;
+ onSaved(data);
+ onOpenChange(false);
+ },
+ });
+ });
+
+ const monthName = format(new Date(`${month}-01T00:00:00`), "MMMM yyyy");
+
+ return (
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ {/* The class supplies --gc-ring-*; the dialog is portaled out */}
+ <DialogContent className="daily-goals-calendar sm:max-w-[440px]">
+ <DialogHeader>
+ <div className="flex items-center gap-2.5">
+ <DialogTitle>Daily goals</DialogTitle>
+ <span className="rounded-full bg-primary/10 px-2.5 py-0.5 text-xs font-medium text-primary">
+ {monthName}
+ </span>
+ </div>
+ </DialogHeader>
+ <Form {...form}>
+ <form
+ noValidate
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="space-y-3"
+ >
+ {FIELDS.map((f) => (
+ <GoalRow
+ key={f.name}
+ control={form.control}
+ previous={targets[f.name]}
+ {...f}
+ />
+ ))}
+ <div className="flex items-start gap-2.5 rounded-lg bg-muted p-3">
+ <Info className="mt-0.5 h-4 w-4 shrink-0 text-muted-foreground" />
+ <DialogDescription className="text-[13px]">
+ Applies from {monthName} and carries forward until you change
+ it. Earlier months keep their own targets.
+ </DialogDescription>
+ </div>
+ <DialogFooter>
+ <Button
+ type="button"
+ variant="outline"
+ onClick={() => onOpenChange(false)}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="submit"
+ disabled={isPending || !form.formState.isDirty}
+ >
+ {isPending && <Loader className="mr-2 h-4 w-4 animate-spin" />}
+ Save goals
+ </Button>
+ </DialogFooter>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/components/dashboard/set-goals-dialog/GoalRow.tsx b/src/components/dashboard/set-goals-dialog/GoalRow.tsx
new file mode 100644
index 0000000..90093b9
--- /dev/null
+++ b/src/components/dashboard/set-goals-dialog/GoalRow.tsx
@@ -0,0 +1,120 @@
+import type { Control } from "react-hook-form";
+import { Minus, Plus } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import {
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form";
+import { Input } from "@/components/ui/input";
+import { cn } from "@/lib/utils";
+import type { GoalTargets } from "@/models/dailyGoals.model";
+
+interface GoalRowProps {
+ control: Control<GoalTargets>;
+ name: keyof GoalTargets;
+ label: string;
+ noun: string;
+ ring: string;
+ min: number;
+ max: number;
+ previous: number;
+}
+
+const clamp = (n: number, min: number, max: number) =>
+ Math.min(max, Math.max(min, n));
+
+export default function GoalRow({
+ control,
+ name,
+ label,
+ noun,
+ ring,
+ min,
+ max,
+ previous,
+}: GoalRowProps) {
+ return (
+ <FormField
+ control={control}
+ name={name}
+ render={({ field, fieldState }) => {
+ const value = field.value;
+ const changed = !fieldState.error && value !== previous;
+ // A cleared field (NaN) steps from the minimum
+ const step = (delta: number) =>
+ field.onChange(
+ Number.isNaN(value)
+ ? min
+ : clamp(Math.round(value) + delta, min, max),
+ );
+ return (
+ <FormItem
+ className={cn(
+ "flex items-center gap-3 space-y-0 rounded-lg border p-3 pl-4",
+ fieldState.error && "border-destructive",
+ changed && "border-primary/40",
+ )}
+ >
+ <span
+ aria-hidden="true"
+ className="size-3 shrink-0 rounded-full border-[3px]"
+ style={{ borderColor: ring }}
+ />
+ <div className="flex grow flex-col gap-0.5">
+ <FormLabel>{label}</FormLabel>
+ {fieldState.error ? (
+ <FormMessage className="text-xs" />
+ ) : changed ? (
+ <span className="text-xs font-medium text-primary">
+ Was {previous} · now {value}
+ </span>
+ ) : (
+ <span className="text-xs text-muted-foreground">
+ {min} to {max}
+ </span>
+ )}
+ </div>
+ <div className="flex items-center overflow-hidden rounded-md border">
+ <Button
+ type="button"
+ variant="ghost"
+ className="size-11 rounded-none"
+ aria-label={`Fewer ${noun}`}
+ disabled={value <= min}
+ onClick={() => step(-1)}
+ >
+ <Minus className="h-4 w-4" />
+ </Button>
+ <FormControl>
+ <Input
+ type="number"
+ inputMode="numeric"
+ min={min}
+ max={max}
+ step={1}
+ className="h-11 w-14 rounded-none border-0 border-x text-center text-base font-semibold"
+ {...field}
+ value={Number.isNaN(value) ? "" : value}
+ onChange={(e) => field.onChange(e.target.valueAsNumber)}
+ />
+ </FormControl>
+ <Button
+ type="button"
+ variant="ghost"
+ className="size-11 rounded-none"
+ aria-label={`More ${noun}`}
+ disabled={value >= max}
+ onClick={() => step(1)}
+ >
+ <Plus className="h-4 w-4" />
+ </Button>
+ </div>
+ </FormItem>
+ );
+ }}
+ />
+ );
+}