From 3c0961994542233d9f98d37e38b15387e94f4cc1 Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Fri, 6 Nov 2026 11:01:29 +0100 Subject: [PATCH 1/4] rename the Apollo client from clienten to client --- frontend/src/main.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index a16dc44c..73994d24 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -8,14 +8,14 @@ import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client"; import { ApolloProvider } from "@apollo/client/react"; import { UserProvider } from "./context/UserContext.tsx"; -const clienten = new ApolloClient({ +const client = new ApolloClient({ link: new HttpLink({ uri: import.meta.env.VITE_GRAPHQL_URL ?? "/graphql" }), cache: new InMemoryCache(), }); createRoot(document.getElementById("root")!).render( - + From 20b94978451a2a60606900f3cd1e85d64c333dbe Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Fri, 6 Nov 2026 11:54:22 +0100 Subject: [PATCH 2/4] add a 404 page for invalid routes, create and update related tests and add translation --- frontend/src/AppRoutes.tsx | 3 + frontend/src/components/header/Header.tsx | 4 +- frontend/src/i18n/locales/en/translation.json | 7 ++ frontend/src/i18n/locales/no/translation.json | 7 ++ frontend/src/pages/notFound/NotFound.css | 72 +++++++++++++++++++ frontend/src/pages/notFound/NotFound.tsx | 26 +++++++ frontend/src/tests/Header.test.tsx | 3 +- frontend/src/tests/NotFound.test.tsx | 24 +++++++ 8 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 frontend/src/pages/notFound/NotFound.css create mode 100644 frontend/src/pages/notFound/NotFound.tsx create mode 100644 frontend/src/tests/NotFound.test.tsx diff --git a/frontend/src/AppRoutes.tsx b/frontend/src/AppRoutes.tsx index 470cee92..82905aa2 100644 --- a/frontend/src/AppRoutes.tsx +++ b/frontend/src/AppRoutes.tsx @@ -10,6 +10,7 @@ const Profile = lazy(() => import("./pages/profile/Profile.tsx")); const PlantOwnerTest = lazy( () => import("./pages/plantOwnerTest/PlantOwnerTest.tsx"), ); +const NotFound = lazy(() => import("./pages/notFound/NotFound.tsx")); // eslint-disable-next-line react-refresh/only-export-components export const ROUTES = { @@ -18,6 +19,7 @@ export const ROUTES = { myMatches: "/my-matches", profile: "/profile", plantOwnerTest: "/plantOwnerTest", + notFound: "*", } as const; function AppRoutes() { @@ -29,6 +31,7 @@ function AppRoutes() { } /> } /> } /> + } /> ); diff --git a/frontend/src/components/header/Header.tsx b/frontend/src/components/header/Header.tsx index da90960c..d0ebc0e0 100644 --- a/frontend/src/components/header/Header.tsx +++ b/frontend/src/components/header/Header.tsx @@ -14,13 +14,15 @@ const pageTitles: Record = { [ROUTES.explore]: "sidebar.explorePlants", [ROUTES.myMatches]: "sidebar.myMatches", [ROUTES.profile]: "sidebar.profile", + [ROUTES.plantOwnerTest]: "sidebar.plantOwnerTest", }; export default function Header({ isSidebarOpen, onOpenSidebar }: HeaderProps) { const { t } = useTranslation(); const { pathname } = useLocation(); - const titleKey = pageTitles[pathname] ?? "sidebar.plantMatch"; + // Paths that are not in pageTitles are shown by the 404 page + const titleKey = pageTitles[pathname] ?? "notFound.title"; return (
diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index 6d468c2e..57ce6640 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -174,5 +174,12 @@ "saved": "This is now your profile picture!", "failed": "Could not save the profile picture. Try again later." } + }, + "notFound": { + "title": "Page not found", + "message": "The page you are looking for does not exist.", + "backToPlantMatch": "Go to Plant Match", + "orChooseInSidebar": "Or choose another page in the sidebar.", + "orChooseInMenu": "Or choose another page in the menu." } } diff --git a/frontend/src/i18n/locales/no/translation.json b/frontend/src/i18n/locales/no/translation.json index ab65d1a8..8f968051 100644 --- a/frontend/src/i18n/locales/no/translation.json +++ b/frontend/src/i18n/locales/no/translation.json @@ -174,5 +174,12 @@ "saved": "Dette er nå profilbildet ditt!", "failed": "Kunne ikke lagre profilbildet. Prøv igjen senere." } + }, + "notFound": { + "title": "Fant ikke siden", + "message": "Siden du leter etter finnes ikke.", + "backToPlantMatch": "Gå til Plante-Matching", + "orChooseInSidebar": "Eller velg en annen side i sidemenyen.", + "orChooseInMenu": "Eller velg en annen side i menyen." } } diff --git a/frontend/src/pages/notFound/NotFound.css b/frontend/src/pages/notFound/NotFound.css new file mode 100644 index 00000000..a62a0553 --- /dev/null +++ b/frontend/src/pages/notFound/NotFound.css @@ -0,0 +1,72 @@ +.not-found-page { + margin-left: var(--sidebar-width); + width: calc(100% - var(--sidebar-width)); + min-height: 60vh; + padding: 40px; + box-sizing: border-box; + + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 1rem; + text-align: center; + font-family: Georgia, serif; +} + +.not-found-code { + margin: 0; + font-size: 4rem; + color: var(--pink-text); +} + +.not-found-message { + margin: 0; + font-size: 1.25rem; + color: var(--text-dark); +} + +.not-found-link { + padding: 0.5rem 1rem; + border: 1px solid var(--dark-pink); + border-radius: 8px; + background-color: var(--light-background); + color: var(--text-dark); + text-decoration: none; +} + +.not-found-link:hover { + background-color: var(--dark-pink); + color: var(--text-light); +} + +.not-found-link:focus-visible { + outline: none; + box-shadow: 0 0 0 2px var(--dark-pink); +} + +.not-found-hint { + margin: 0; + font-size: 1.25rem; + color: var(--text-dark); +} + +.not-found-hint--mobile { + display: none; +} + +@media (max-width: 767px) { + .not-found-page { + margin-left: 0; + width: 100%; + padding: 20px; + } + + .not-found-hint--desktop { + display: none; + } + + .not-found-hint--mobile { + display: block; + } +} diff --git a/frontend/src/pages/notFound/NotFound.tsx b/frontend/src/pages/notFound/NotFound.tsx new file mode 100644 index 00000000..b72885c5 --- /dev/null +++ b/frontend/src/pages/notFound/NotFound.tsx @@ -0,0 +1,26 @@ +import { Link } from "react-router-dom"; +import { useTranslation } from "react-i18next"; +import { ROUTES } from "../../AppRoutes"; +import "./NotFound.css"; + +// Shown for every path that does not match a route in AppRoutes +export default function NotFound() { + const { t } = useTranslation(); + + return ( +
+

404

+

{t("notFound.message")}

+ + {t("notFound.backToPlantMatch")} + + {/* Desktop shows the sidebar, mobile has a menu button, so the hint differs */} +

+ {t("notFound.orChooseInSidebar")} +

+

+ {t("notFound.orChooseInMenu")} +

+
+ ); +} diff --git a/frontend/src/tests/Header.test.tsx b/frontend/src/tests/Header.test.tsx index 7293b4c3..f7c3f3c5 100644 --- a/frontend/src/tests/Header.test.tsx +++ b/frontend/src/tests/Header.test.tsx @@ -24,7 +24,8 @@ describe("Header", () => { [ROUTES.explore, "sidebar.explorePlants"], [ROUTES.myMatches, "sidebar.myMatches"], [ROUTES.profile, "sidebar.profile"], - ["/unknown-page", "sidebar.plantMatch"], + [ROUTES.plantOwnerTest, "sidebar.plantOwnerTest"], + ["/unknown-page", "notFound.title"], ])("shows the title for %s", (route, title) => { renderHeader(route); diff --git a/frontend/src/tests/NotFound.test.tsx b/frontend/src/tests/NotFound.test.tsx new file mode 100644 index 00000000..c0dfea63 --- /dev/null +++ b/frontend/src/tests/NotFound.test.tsx @@ -0,0 +1,24 @@ +import { render, screen } from "@testing-library/react"; +import { MemoryRouter } from "react-router-dom"; +import { describe, it, expect, vi } from "vitest"; +import AppRoutes, { ROUTES } from "../AppRoutes"; + +vi.mock("react-i18next", () => ({ + useTranslation: () => ({ t: (key: string) => key }), +})); + +describe("NotFound", () => { + it("is shown for an unknown path and links back to PlantMatch", async () => { + render( + + + , + ); + + // The page is lazy loaded, so findBy waits until it is shown + expect(await screen.findByText("notFound.message")).toBeInTheDocument(); + expect( + screen.getByRole("link", { name: "notFound.backToPlantMatch" }), + ).toHaveAttribute("href", ROUTES.plantMatch); + }); +}); From 194b36826d03b6d8842573c638bd4881a6ddcd5e Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Fri, 6 Nov 2026 12:16:05 +0100 Subject: [PATCH 3/4] Update documentation for the 404 page --- README.md | 5 +++-- docs/architecture.md | 3 ++- docs/quality.md | 2 ++ docs/tests.md | 6 ++++-- e2e/uiAccessibility.spec.ts | 7 ++++++- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a62a00f7..47bcae09 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ http://it2810-03.idi.ntnu.no/project2/ - **Plant Owner Test:** A short quiz that tells you what kind of plant owner you are. Each result has its own picture, which you can only get through the test, and you can save it as your profile picture. - **Language:** The app is available in English and Norwegian. - **Theme:** The app has a light and a dark theme. +- **Page not found:** Unknown addresses show a 404 page with a link back to Plant Match. ## Tech stack @@ -93,9 +94,9 @@ Browser ──> Apache (port 80, /project2) serves the built frontend The backend runs with pm2, which keeps it running in the background and restarts it if it crashes or the VM reboots. The backend reads `MONGODB_URI=mongodb://localhost:27017`, `DB_NAME=iloveplants` and `PORT=3001` from `backend/.env` on the VM (see `.env.example`). If the backend stops, see [How to start the backend if it stops](../T03-Project-2/docs/architecture.md) . - ### Frontend -The frontend runs on the VM by using Apache serves. It was copied to the folder Apache serves for `/project2`. The build reads `VITE_GRAPHQL_URL` from [frontend/.env.production](frontend/.env.production), so the deployed app sends its requests to the backend on port 3001 on the VM. + +The frontend runs on the VM by using Apache serves. It was copied to the folder Apache serves for `/project2`. The build reads `VITE_GRAPHQL_URL` from [frontend/.env.production](frontend/.env.production), so the deployed app sends its requests to the backend on port 3001 on the VM. ### Database diff --git a/docs/architecture.md b/docs/architecture.md index 4281db03..2f597af4 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -36,7 +36,7 @@ The sections below describe each part in more detail. The frontend is built with React and TypeScript (Vite). -- **Routing:** Routes are defined in `AppRoutes.tsx` with React Router, and the app is served under the base path `/project2/`. Vite defines this base in `frontend/vite.config.ts`; `BrowserRouter` uses the same value through `import.meta.env.BASE_URL`. All pages except the start page are lazy-loaded. +- **Routing:** Routes are defined in `AppRoutes.tsx` with React Router, and the app is served under the base path `/project2/`. Vite defines this base in `frontend/vite.config.ts`; `BrowserRouter` uses the same value through `import.meta.env.BASE_URL`. All pages except the start page are lazy-loaded. Any path that does not match a route shows the 404 page, and the header shows the "Page not found" title for it. | Route | Page | Purpose | | ----------------- | ---------------- | -------------------------------------------------------------- | @@ -45,6 +45,7 @@ The frontend is built with React and TypeScript (Vite). | `/my-matches` | `MyMatchesPage` | List and remove the user's matched plants | | `/profile` | `Profile` | Edit name, description, profile picture and preferences | | `/plantOwnerTest` | `PlantOwnerTest` | A quiz whose result can be set as the profile picture | + | `*` | `NotFound` | 404 page for unknown paths, with a link back to Plant Match | - **User state:** `UserProvider` (`context/UserContext.tsx`) loads the current user from the backend and makes it available through `useUser()`. The hooks `useProfile` and `useMatchedPlants` wrap the user mutations, so pages do not call GraphQL directly for user data. All user queries and mutations live in `context/userQueries.ts`. - **Reusable logic in hooks:** Logic without UI is kept in hooks. `useDebounce` delays updates while the user is typing (used for search and for saving the profile text), and `useSessionState` stores state in `sessionStorage`. diff --git a/docs/quality.md b/docs/quality.md index 09916052..844aee4f 100644 --- a/docs/quality.md +++ b/docs/quality.md @@ -89,3 +89,5 @@ and our approach to data handling. - **Reusable components:** We used reusable React components to maintain a consistent interface and avoid duplicating code. For example, the Header and Sidebar are shared across pages, while components such as plant cards are used to present plant information consistently. This makes the application easier to maintain and allows changes to be made in one place. - **Responsive design:** We designed the application for both desktop and mobile screens. The layout adapts to different screen sizes to ensure that the content and navigation remain usable across devices. + +- **Page not found:** Unknown addresses show a 404 page instead of a blank page. It links back to Plant Match, and tells the user they can choose another page in the sidebar on desktop or the menu on mobile, so the hint matches the navigation they actually see. diff --git a/docs/tests.md b/docs/tests.md index a967dc07..91761efb 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -119,13 +119,15 @@ The tests are in `frontend/src/tests`. Snapshot tests are used for the explore p - Accessibility: a search landmark, a heading and labelled controls. - Snapshots of the page with results, with no results and with the sort menu open. - `Header.test.tsx` - - Shows the right title for each page, and opens the sidebar with the menu button. + - Shows the right title for each page, including the "Page not found" title for unknown paths, and opens the sidebar with the menu button. - `LanguageSettings.test.tsx` - Switching language changes the content at once and is kept after a reload, an unsupported saved language falls back to Norwegian, and switching works when browser storage is blocked. - `LanguageSwitcher.test.tsx` - The button text and accessible name in both languages, that clicking switches to the other language, and snapshots for both languages. - `MyMatchesPage.test.tsx` - A message when there are no matches, the loading state instead of that message, and that saved matches are shown and can be removed. +- `NotFound.test.tsx` + - An unknown path shows the 404 page, with a link back to Plant Match. - `PlantCard.test.tsx` - The back of the card shows the whole plant after clicking information, missing facts and images are shown as unknown, and watering, sunlight and care level are turned into the right preference chips. - `PlantMatchPage.test.tsx` @@ -236,7 +238,7 @@ Saving the profile is debounced by 800 ms. The test waits until the save request `uiAccessibility.spec.ts` runs **axe** through `@axe-core/playwright` against WCAG 2.1 level A and AA, the level required for the public sector in Norway. It checks: -- all five pages (PlantMatch, ExplorePlants, MyMatches, Profile and PlantOwnerTest), in both light and dark mode, since colour contrast has to meet the requirements in both themes +- all six pages (PlantMatch, ExplorePlants, MyMatches, Profile, PlantOwnerTest and the 404 page), in both light and dark mode, since colour contrast has to meet the requirements in both themes - content that only appears after an interaction: a flipped plant card, the edit tab on the profile page, MyMatches with a matched plant, and the result of the plant owner test The tests run on both desktop and mobile, since the layout, and the elements that are visible, differ between them. A failing test lists each element that breaks a rule, with axe's explanation (for contrast: the colours and the ratio). diff --git a/e2e/uiAccessibility.spec.ts b/e2e/uiAccessibility.spec.ts index b9ed05ea..00628492 100644 --- a/e2e/uiAccessibility.spec.ts +++ b/e2e/uiAccessibility.spec.ts @@ -38,7 +38,11 @@ async function setTheme(page: Page, theme: "light" | "dark") { // Each page and an element that shows the page has finished loading const PAGES = [ { name: "PlantMatch", path: "", loaded: ".plant-card-front" }, - { name: "ExplorePlants", path: "#/explorePlants", loaded: ".plant-grid > li" }, + { + name: "ExplorePlants", + path: "#/explorePlants", + loaded: ".plant-grid > li", + }, { name: "MyMatches", path: "#/my-matches", loaded: ".matches-grid" }, { name: "Profile", path: "#/profile", loaded: "main.profile" }, { @@ -46,6 +50,7 @@ const PAGES = [ path: "#/plantOwnerTest", loaded: ".plant-owner-test", }, + { name: "NotFound", path: "#/does-not-exist", loaded: ".not-found-page" }, ]; // Colour contrast is the only check that differs between the themes, From 1ea2ebac2d7a474bbe9f6ac206a1059a6aea643e Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Fri, 6 Nov 2026 16:05:34 +0100 Subject: [PATCH 4/4] fix consistent naming convention for component folders --- frontend/src/components/plantCard/PlantCard.tsx | 2 +- .../PlantPreferenceChips.css | 0 .../PlantPreferenceChips.tsx | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename frontend/src/components/{PlantPreferenceChips => plantPreferenceChips}/PlantPreferenceChips.css (100%) rename frontend/src/components/{PlantPreferenceChips => plantPreferenceChips}/PlantPreferenceChips.tsx (100%) diff --git a/frontend/src/components/plantCard/PlantCard.tsx b/frontend/src/components/plantCard/PlantCard.tsx index b40bac11..1863d4d4 100644 --- a/frontend/src/components/plantCard/PlantCard.tsx +++ b/frontend/src/components/plantCard/PlantCard.tsx @@ -1,7 +1,7 @@ import { useState } from "react"; import { useTranslation } from "react-i18next"; import "./PlantCard.css"; -import PlantPreferenceChips from "../PlantPreferenceChips/PlantPreferenceChips"; +import PlantPreferenceChips from "../plantPreferenceChips/PlantPreferenceChips"; type Props = { name: string | null; diff --git a/frontend/src/components/PlantPreferenceChips/PlantPreferenceChips.css b/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css similarity index 100% rename from frontend/src/components/PlantPreferenceChips/PlantPreferenceChips.css rename to frontend/src/components/plantPreferenceChips/PlantPreferenceChips.css diff --git a/frontend/src/components/PlantPreferenceChips/PlantPreferenceChips.tsx b/frontend/src/components/plantPreferenceChips/PlantPreferenceChips.tsx similarity index 100% rename from frontend/src/components/PlantPreferenceChips/PlantPreferenceChips.tsx rename to frontend/src/components/plantPreferenceChips/PlantPreferenceChips.tsx