Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry form add more button #130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions e2e/tests/entry.mobile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ test.describe("Add entry mobile", () => {
await page.getByRole("banner").getByLabel(t("controls.openMenu")).click();
await page.getByRole("button", { name: t("entryDialog.title.create") }).click();
await expect(page).toHaveURL(/.*\/create$/);
// Fill entry fields
await fillEntryFormMobile(page, t, entries[0]);
// Submit
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
await submitEntryMobile(page, t, entries[0]);
await expect(page).not.toHaveURL(/.*\/create$/);
});

test("Should add entry from entry row", async ({ page, t }) => {
Expand All @@ -61,9 +58,29 @@ test.describe("Add entry mobile", () => {
.first()
.click();
await expect(page).toHaveURL(/.*\/create$/);
await fillEntryFormMobile(page, t, entries[0]);
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
await submitEntryMobile(page, t, entries[0]);
await expect(page).not.toHaveURL(/.*\/create$/);
});
});

test.describe("Add More entry mobile", () => {
test("Should add more entry from app bar", async ({ page, t }) => {
await page.getByRole("banner").getByLabel(t("controls.openMenu")).click();
await page.getByRole("button", { name: t("entryDialog.title.create") }).click();
await expect(page).toHaveURL(/.*\/create$/);
await addMoreEntryMobile(page, t, entries[0]);
await expect(page).toHaveURL(/.*\/create$/);
});

test("Should add more entry from entry row", async ({ page, t }) => {
await page
.getByRole("button")
.getByRole("button", { name: t("entryDialog.title.create") })
.first()
.click();
await expect(page).toHaveURL(/.*\/create$/);
await addMoreEntryMobile(page, t, entries[0]);
await expect(page).toHaveURL(/.*\/create$/);
});
});

Expand Down Expand Up @@ -148,3 +165,15 @@ const setDefaultValuesMobile = async (
activity,
);
};

const submitEntryMobile = async (page: Page, t: TFunction, entry: TestEntry) => {
await fillEntryFormMobile(page, t, entry);
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
};

const addMoreEntryMobile = async (page: Page, t: TFunction, entry: TestEntry) => {
await fillEntryFormMobile(page, t, entry);
await page.getByRole("button", { name: t("entryDialog.addMore") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
};
48 changes: 39 additions & 9 deletions e2e/tests/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ test.beforeEach(async ({ page }) => {

test.describe("Add entry", () => {
test("Should add entry from app bar", async ({ page, t }) => {
// Open entry dialog
await page
.getByRole("banner")
.getByRole("button", { name: t("entryDialog.title.create") })
.click();
await expect(page).toHaveURL(/.*\/create$/);
// Fill entry fields
await fillEntryForm(page, t, entries[0]);
// Submit
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
await submitEntry(page, t, entries[0]);
await expect(page).not.toHaveURL(/.*\/create$/);
});

test("Should add entry from entry row", async ({ page, t }) => {
Expand All @@ -63,9 +59,31 @@ test.describe("Add entry", () => {
.first()
.click();
await expect(page).toHaveURL(/.*\/create$/);
await fillEntryForm(page, t, entries[0]);
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
await submitEntry(page, t, entries[0]);
await expect(page).not.toHaveURL(/.*\/create$/);
});
});

test.describe("Add More entry", () => {
test("Should add more entry from app bar", async ({ page, t }) => {
await page
.getByRole("banner")
.getByRole("button", { name: t("entryDialog.title.create") })
.click();
await expect(page).toHaveURL(/.*\/create$/);
await addMoreEntry(page, t, entries[0]);
await expect(page).toHaveURL(/.*\/create$/);
});

test("Should add more entry from entry row", async ({ page, t }) => {
await page
.getByRole("button")
.getByRole("button", { name: t("entryDialog.title.create") })
.first()
.click();
await expect(page).toHaveURL(/.*\/create$/);
await addMoreEntry(page, t, entries[0]);
await expect(page).toHaveURL(/.*\/create$/);
});
});

Expand Down Expand Up @@ -148,3 +166,15 @@ const setDefaultValues = async (page: Page, t: TFunction, product: string, activ
activity,
);
};

const submitEntry = async (page: Page, t: TFunction, entry: TestEntry) => {
await fillEntryForm(page, t, entry);
await page.getByRole("button", { name: t("entryDialog.submit") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
};

const addMoreEntry = async (page: Page, t: TFunction, entry: TestEntry) => {
await fillEntryForm(page, t, entry);
await page.getByRole("button", { name: t("entryDialog.addMore") }).click();
await expect(page.getByText(t("notifications.addEntry.success"))).toBeAttached();
};
40 changes: 38 additions & 2 deletions web/src/components/entry-dialog/EntryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
useTheme,
} from "@mui/material";
import { Dayjs } from "dayjs";
import { useEffect } from "react";
import { useEffect, useRef } from "react";
import { Controller, UseFormReturn } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useLocation, useNavigate } from "react-router-dom";
Expand Down Expand Up @@ -46,13 +46,20 @@ type LocationState = {
editEntry?: Entry;
};

enum SubmitTypes {
addMore = "addMore",
submit = "submit",
}
type SubmitType = SubmitTypes | null;

const EntryForm = () => {
const { state } = useLocation();
const dayjs = useDayjs();
// state is possibly null
const { date: originalDate, editEntry }: LocationState = state || {};
const { form, onSubmit, loading } = useEntryForm({ editEntry, date: dayjs(originalDate) });
const navigate = useNavigate();
const submitter = useRef<SubmitType>(null);

const {
handleSubmit,
Expand All @@ -63,7 +70,8 @@ const EntryForm = () => {
useEffect(() => {
if (isSubmitSuccessful) {
reset();
navigate("..");
submitter.current !== SubmitTypes.addMore && navigate("..");
submitter.current = null;
}
}, [isSubmitSuccessful, navigate, reset]);

Expand All @@ -75,6 +83,11 @@ const EntryForm = () => {
const dateWatch = dayjs(watch("date")).locale(dayjs.locale());
const { isJiraAuth } = useIsJiraAuthenticated();

const handleAddMore = () => {
handleSubmit(onSubmit)();
submitter.current = SubmitTypes.addMore;
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Grid container spacing={3}>
Expand Down Expand Up @@ -180,6 +193,19 @@ const EntryForm = () => {
{t("entryDialog.submit")}
</LoadingButton>
</Grid>
{!editEntry && (
<Grid item xs={12}>
<LoadingButton
loading={loading}
onClick={() => handleAddMore()}
variant="contained"
size="large"
fullWidth
>
{t("entryDialog.addMore")}
</LoadingButton>
</Grid>
)}
<Grid item xs={12}>
<Button
type="reset"
Expand Down Expand Up @@ -238,6 +264,16 @@ const EntryForm = () => {
<Button type="reset" variant="outlined" size="large" onClick={() => reset()}>
{editEntry ? t("entryDialog.reset") : t("entryDialog.clear")}
</Button>
{!editEntry && (
<LoadingButton
loading={loading}
onClick={() => handleAddMore()}
variant="contained"
size="large"
>
{t("entryDialog.addMore")}
</LoadingButton>
)}
<LoadingButton loading={loading} type="submit" variant="contained" size="large">
{t("entryDialog.submit")}
</LoadingButton>
Expand Down
1 change: 1 addition & 0 deletions web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const en = {
duration: "Duration",
reset: "Reset",
submit: "Save",
addMore: "Add more",
product: "Product",
activity: "Function",
issue: "Issue",
Expand Down
1 change: 1 addition & 0 deletions web/src/i18n/fi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const fi = {
duration: "Kesto",
reset: "Palauta",
submit: "Tallenna",
addMore: "Lisää seuraava",
product: "Tuote",
activity: "Toiminto",
issue: "Tiketti",
Expand Down