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: add Save & Start functionality and update task creation flow

Khuram Niazb32b23927aea
diff --git a/__tests__/JobsAppliedCard.spec.tsx b/__tests__/JobsAppliedCard.spec.tsx
index 6a5fbbd..89e0889 100644
--- a/__tests__/JobsAppliedCard.spec.tsx
+++ b/__tests__/JobsAppliedCard.spec.tsx
@@ -24,12 +24,12 @@ describe("JobsAppliedCard", () => {
expect(mockPush).toHaveBeenCalledWith("/dashboard/myjobs?add-job=true");
});
- it("should navigate to tasks page when New Task button is clicked", async () => {
+ it("should navigate to tasks page with add-task=true when New Task button is clicked", async () => {
const user = userEvent.setup();
render(<JobsAppliedCard />);
await user.click(screen.getByRole("button", { name: /new task/i }));
- expect(mockPush).toHaveBeenCalledWith("/dashboard/tasks");
+ expect(mockPush).toHaveBeenCalledWith("/dashboard/tasks?add-task=true");
});
});
diff --git a/__tests__/TaskForm.spec.tsx b/__tests__/TaskForm.spec.tsx
index 1ea2284..7f202c9 100644
--- a/__tests__/TaskForm.spec.tsx
+++ b/__tests__/TaskForm.spec.tsx
@@ -10,8 +10,8 @@ vi.mock("@/utils/user.utils", () => ({
}));
vi.mock("@/actions/task.actions", () => ({
- createTask: vi.fn().mockResolvedValue({ success: true }),
- updateTask: vi.fn().mockResolvedValue({ success: true }),
+ createTask: vi.fn().mockResolvedValue({ success: true, data: { id: "new-task-id" } }),
+ updateTask: vi.fn().mockResolvedValue({ success: true, data: { id: "task-id" } }),
}));
global.ResizeObserver = class ResizeObserver {
@@ -64,6 +64,7 @@ describe("TaskForm Component", () => {
const mockResetEditTask = vi.fn();
const mockOnTaskSaved = vi.fn();
const mockSetDialogOpen = vi.fn();
+ const mockOnSaveAndStart = vi.fn();
const user = userEvent.setup({ skipHover: true });
window.HTMLElement.prototype.scrollIntoView = vi.fn();
@@ -408,6 +409,169 @@ describe("TaskForm Component", () => {
});
});
+ describe("Save and Start", () => {
+ beforeEach(() => {
+ (createTask as any).mockResolvedValue({
+ success: true,
+ data: { id: "new-task-id" },
+ });
+ (updateTask as any).mockResolvedValue({
+ success: true,
+ data: { id: "task-id" },
+ });
+ });
+
+ it("should not show the Save & Start button when onSaveAndStart is not provided", () => {
+ render(
+ <TaskForm
+ activityTypes={mockActivityTypes}
+ editTask={null}
+ resetEditTask={mockResetEditTask}
+ onTaskSaved={mockOnTaskSaved}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(
+ screen.queryByTestId("save-and-start-task-btn")
+ ).not.toBeInTheDocument();
+ });
+
+ it("should create the task and invoke onSaveAndStart with the new task id", async () => {
+ render(
+ <TaskForm
+ activityTypes={mockActivityTypes}
+ editTask={null}
+ resetEditTask={mockResetEditTask}
+ onTaskSaved={mockOnTaskSaved}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ onSaveAndStart={mockOnSaveAndStart}
+ />
+ );
+
+ const titleInput = screen.getByLabelText(/Title/i);
+ await user.type(titleInput, "New Test Task");
+
+ const activityTypeCombobox = screen.getByLabelText(/Activity Type/i);
+ await user.click(activityTypeCombobox);
+ const developmentOption = screen.getByRole("option", {
+ name: "Development",
+ });
+ await user.click(developmentOption);
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ expect(mockOnTaskSaved).toHaveBeenCalledTimes(1);
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ expect(mockOnSaveAndStart).toHaveBeenCalledWith("new-task-id");
+ });
+ });
+
+ it("should update the task and invoke onSaveAndStart with the existing task id", async () => {
+ const mockEditTask: Task = {
+ id: "task-id",
+ userId: "user-id",
+ title: "Existing Task",
+ description: "Existing description",
+ status: "in-progress",
+ priority: 5,
+ percentComplete: 0,
+ dueDate: null,
+ activityTypeId: "type-1",
+ activityType: {
+ id: "type-1",
+ label: "Development",
+ value: "development",
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ },
+ activities: [],
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ };
+
+ render(
+ <TaskForm
+ activityTypes={mockActivityTypes}
+ editTask={mockEditTask}
+ resetEditTask={mockResetEditTask}
+ onTaskSaved={mockOnTaskSaved}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ onSaveAndStart={mockOnSaveAndStart}
+ />
+ );
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(updateTask).toHaveBeenCalledTimes(1);
+ expect(mockOnSaveAndStart).toHaveBeenCalledWith("task-id");
+ });
+ });
+
+ it("should not invoke onSaveAndStart when create fails", async () => {
+ (createTask as any).mockResolvedValueOnce({
+ success: false,
+ message: "Failed to create task",
+ });
+
+ render(
+ <TaskForm
+ activityTypes={mockActivityTypes}
+ editTask={null}
+ resetEditTask={mockResetEditTask}
+ onTaskSaved={mockOnTaskSaved}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ onSaveAndStart={mockOnSaveAndStart}
+ />
+ );
+
+ const titleInput = screen.getByLabelText(/Title/i);
+ await user.type(titleInput, "New Test Task");
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ });
+ expect(mockOnSaveAndStart).not.toHaveBeenCalled();
+ });
+
+ it("should not invoke onSaveAndStart when the regular Save button is used", async () => {
+ render(
+ <TaskForm
+ activityTypes={mockActivityTypes}
+ editTask={null}
+ resetEditTask={mockResetEditTask}
+ onTaskSaved={mockOnTaskSaved}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ onSaveAndStart={mockOnSaveAndStart}
+ />
+ );
+
+ const titleInput = screen.getByLabelText(/Title/i);
+ await user.type(titleInput, "New Test Task");
+
+ const saveBtn = screen.getByTestId("save-task-btn");
+ await user.click(saveBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ });
+ expect(mockOnSaveAndStart).not.toHaveBeenCalled();
+ });
+ });
+
describe("Dialog State Management", () => {
it("should not render when dialogOpen is false", () => {
render(
diff --git a/__tests__/TasksContainer.spec.tsx b/__tests__/TasksContainer.spec.tsx
index 02672a2..a5e05b5 100644
--- a/__tests__/TasksContainer.spec.tsx
+++ b/__tests__/TasksContainer.spec.tsx
@@ -7,6 +7,7 @@ import {
deleteTaskById,
updateTaskStatus,
startActivityFromTask,
+ createTask,
} from "@/actions/task.actions";
import { Task } from "@/models/task.model";
import { useRouter } from "next/navigation";
@@ -48,10 +49,13 @@ vi.mock("@/actions/task.actions", () => ({
deleteTaskById: vi.fn(),
updateTaskStatus: vi.fn(),
startActivityFromTask: vi.fn(),
+ createTask: vi.fn(),
+ updateTask: vi.fn(),
}));
vi.mock("next/navigation", () => ({
useRouter: vi.fn(),
+ useSearchParams: vi.fn(() => new URLSearchParams()),
}));
vi.mock("@/context/ActivityContext", () => ({
@@ -557,6 +561,127 @@ describe("TasksContainer Component", () => {
});
});
+ describe("Save & Start from the Add Task dialog", () => {
+ beforeEach(() => {
+ (getTasksList as any).mockResolvedValue({
+ success: true,
+ data: mockTasks,
+ total: 2,
+ });
+ });
+
+ const openAddTaskDialogAndFillForm = async () => {
+ const addButton = screen.getByTestId("add-task-btn");
+ await user.click(addButton);
+
+ await screen.findByTestId("task-form-dialog-title");
+
+ const titleInput = screen.getByLabelText(/Title/i);
+ await user.type(titleInput, "Brand New Task");
+
+ const activityTypeCombobox = screen.getByLabelText(/Activity Type/i);
+ await user.click(activityTypeCombobox);
+ const developmentOption = screen.getByRole("option", {
+ name: "Development",
+ });
+ await user.click(developmentOption);
+ };
+
+ it("creates the task and starts its activity when nothing is in progress", async () => {
+ (createTask as any).mockResolvedValue({
+ success: true,
+ data: { id: "new-task-id" },
+ });
+ (startActivityFromTask as any).mockResolvedValue({ success: true });
+
+ render(<TasksContainer activityTypes={mockActivityTypes} />);
+
+ await waitFor(() => {
+ expect(screen.getByText("Task 1")).toBeInTheDocument();
+ });
+
+ await openAddTaskDialogAndFillForm();
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ expect(startActivityFromTask).toHaveBeenCalledWith("new-task-id");
+ expect(mockRefreshCurrentActivity).toHaveBeenCalled();
+ });
+ expect(
+ screen.queryByTestId("task-form-dialog-title")
+ ).not.toBeInTheDocument();
+ });
+
+ it("shows the confirm dialog instead of starting immediately when an activity is already running", async () => {
+ const mockStopActivity = vi.fn();
+ (useActivity as any).mockReturnValue({
+ refreshCurrentActivity: mockRefreshCurrentActivity,
+ currentActivity: { id: "current-1", activityName: "Ongoing Task" },
+ stopActivity: mockStopActivity,
+ });
+ (createTask as any).mockResolvedValue({
+ success: true,
+ data: { id: "new-task-id" },
+ });
+
+ render(<TasksContainer activityTypes={mockActivityTypes} />);
+
+ await waitFor(() => {
+ expect(screen.getByText("Task 1")).toBeInTheDocument();
+ });
+
+ await openAddTaskDialogAndFillForm();
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ });
+ expect(startActivityFromTask).not.toHaveBeenCalled();
+ expect(
+ await screen.findByText(/"Ongoing Task" is currently in progress/i)
+ ).toBeInTheDocument();
+
+ (startActivityFromTask as any).mockResolvedValue({ success: true });
+ const confirmButton = screen.getByRole("button", {
+ name: "Stop & Start",
+ });
+ await user.click(confirmButton);
+
+ await waitFor(() => {
+ expect(mockStopActivity).toHaveBeenCalledTimes(1);
+ expect(startActivityFromTask).toHaveBeenCalledWith("new-task-id");
+ });
+ });
+
+ it("does not start an activity when task creation fails", async () => {
+ (createTask as any).mockResolvedValue({
+ success: false,
+ message: "Failed to create task",
+ });
+
+ render(<TasksContainer activityTypes={mockActivityTypes} />);
+
+ await waitFor(() => {
+ expect(screen.getByText("Task 1")).toBeInTheDocument();
+ });
+
+ await openAddTaskDialogAndFillForm();
+
+ const saveAndStartBtn = screen.getByTestId("save-and-start-task-btn");
+ await user.click(saveAndStartBtn);
+
+ await waitFor(() => {
+ expect(createTask).toHaveBeenCalledTimes(1);
+ });
+ expect(startActivityFromTask).not.toHaveBeenCalled();
+ });
+ });
+
describe("Filtering and Grouping", () => {
beforeEach(async () => {
(getTasksList as any).mockResolvedValue({
diff --git a/__tests__/TasksPageClient.spec.tsx b/__tests__/TasksPageClient.spec.tsx
index d88be4e..3d285ab 100644
--- a/__tests__/TasksPageClient.spec.tsx
+++ b/__tests__/TasksPageClient.spec.tsx
@@ -46,6 +46,7 @@ vi.mock("next/navigation", () => ({
replace: vi.fn(),
prefetch: vi.fn(),
}),
+ useSearchParams: vi.fn().mockReturnValue(new URLSearchParams()),
}));
vi.mock("@/context/ActivityContext", () => ({
diff --git a/src/components/tasks/TaskForm.tsx b/src/components/tasks/TaskForm.tsx
index 927b2d3..acc58eb 100644
--- a/src/components/tasks/TaskForm.tsx
+++ b/src/components/tasks/TaskForm.tsx
@@ -41,6 +41,7 @@ type TaskFormProps = {
onTaskSaved: () => void;
dialogOpen: boolean;
setDialogOpen: (open: boolean) => void;
+ onSaveAndStart?: (taskId: string) => void;
};
const statusOptions = Object.entries(TASK_STATUSES).map(([value, label]) => ({
@@ -56,6 +57,7 @@ export function TaskForm({
onTaskSaved,
dialogOpen,
setDialogOpen,
+ onSaveAndStart,
}: TaskFormProps) {
const [isPending, startTransition] = useTransition();
const form = useForm<z.infer<typeof AddTaskFormSchema>>({
@@ -102,21 +104,32 @@ export function TaskForm({
}
}, [editTask, reset]);
- function onSubmit(data: z.infer<typeof AddTaskFormSchema>) {
+ function onSubmit(
+ data: z.infer<typeof AddTaskFormSchema>,
+ startAfterSave = false,
+ ) {
startTransition(async () => {
const result = editTask ? await updateTask(data) : await createTask(data);
- toastActionResult(result, {
+ toastActionResult<Task>(result, {
success: `Task has been ${editTask ? "updated" : "created"} successfully`,
- onSuccess: () => {
+ onSuccess: (task) => {
reset();
setDialogOpen(false);
resetEditTask();
onTaskSaved();
+ if (startAfterSave && task?.id) {
+ onSaveAndStart?.(task.id);
+ }
},
});
});
}
+ const onSaveClick = form.handleSubmit((data) => onSubmit(data, false));
+ const onSaveAndStartClick = form.handleSubmit((data) =>
+ onSubmit(data, true),
+ );
+
const pageTitle = editTask ? "Edit Task" : "Add Task";
const closeDialog = () => {
@@ -136,7 +149,7 @@ export function TaskForm({
</DialogHeader>
<Form {...form}>
<form
- onSubmit={form.handleSubmit(onSubmit)}
+ onSubmit={onSaveClick}
className="grid grid-cols-1 md:grid-cols-2 gap-4 p-4"
>
{/* Title */}
@@ -331,7 +344,23 @@ export function TaskForm({
Cancel
</Button>
</div>
- <Button type="submit" data-testid="save-task-btn">
+ {onSaveAndStart && (
+ <Button
+ type="button"
+ variant="secondary"
+ className="mt-2 md:mt-0"
+ onClick={onSaveAndStartClick}
+ disabled={isPending}
+ data-testid="save-and-start-task-btn"
+ >
+ Save & Start
+ </Button>
+ )}
+ <Button
+ type="submit"
+ disabled={isPending}
+ data-testid="save-task-btn"
+ >
Save
{isPending && (
<Loader className="h-4 w-4 shrink-0 spinner ml-2" />
diff --git a/src/components/tasks/TasksContainer.tsx b/src/components/tasks/TasksContainer.tsx
index e270286..02157c6 100644
--- a/src/components/tasks/TasksContainer.tsx
+++ b/src/components/tasks/TasksContainer.tsx
@@ -429,6 +429,7 @@ function TasksContainer({
onTaskSaved={reloadTasks}
dialogOpen={dialogOpen}
setDialogOpen={setDialogOpen}
+ onSaveAndStart={onStartActivity}
/>
{confirmDialog}
</>