Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 10 additions & 9 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ 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`; `HashRouter` provides the client-side routes, including Explore query parameters after the hash. All pages except the start page are lazy-loaded.

| Route | Page | Purpose |
| ----------------- | ---------------- | ----------------------------------------------------------------- |
| `/` | `PlantMatchPage` | Swipe through plants (`getPlants`), best preference matches first |
| `/explorePlants` | `ExplorePlants` | Search, filter, sort and paginate all plants (`explorePlants`) |
| `/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 |
- **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`; `HashRouter` provides the client-side routes, including Explore query parameters after the hash. 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 |
| ----------------- | ---------------- | -------------------------------------------------------------- |
| `/` | `PlantMatchPage` | Swipe through random plants (`getPlants`) and match with them |
| `/explorePlants` | `ExplorePlants` | Search, filter, sort and paginate all plants (`explorePlants`) |
| `/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), `useSearchParams` keeps Explore search, filters, sorting and page in the URL, and `useSessionState` stores state in `sessionStorage` for features that still use it.
Expand Down
2 changes: 2 additions & 0 deletions docs/quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 4 additions & 2 deletions docs/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -238,7 +240,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).
Expand Down
1 change: 1 addition & 0 deletions e2e/uiAccessibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,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,
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/AppRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -18,6 +19,7 @@ export const ROUTES = {
myMatches: "/my-matches",
profile: "/profile",
plantOwnerTest: "/plantOwnerTest",
notFound: "*",
} as const;

function AppRoutes() {
Expand All @@ -29,6 +31,7 @@ function AppRoutes() {
<Route path={ROUTES.myMatches} element={<MyMatchesPage />} />
<Route path={ROUTES.profile} element={<Profile />} />
<Route path={ROUTES.plantOwnerTest} element={<PlantOwnerTest />} />
<Route path={ROUTES.notFound} element={<NotFound />} />
</Routes>
</Suspense>
);
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const pageTitles: Record<string, string> = {
[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 (
<header className="header" data-sidebar-open={isSidebarOpen}>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/plantCard/PlantCard.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
7 changes: 7 additions & 0 deletions frontend/src/i18n/locales/no/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
4 changes: 2 additions & 2 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<StrictMode>
<ApolloProvider client={clienten}>
<ApolloProvider client={client}>
<UserProvider>
<HashRouter>
<App />
Expand Down
72 changes: 72 additions & 0 deletions frontend/src/pages/notFound/NotFound.css
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 26 additions & 0 deletions frontend/src/pages/notFound/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<main className="not-found-page">
<h2 className="not-found-code">404</h2>
<p className="not-found-message">{t("notFound.message")}</p>
<Link to={ROUTES.plantMatch} className="not-found-link">
{t("notFound.backToPlantMatch")}
</Link>
{/* Desktop shows the sidebar, mobile has a menu button, so the hint differs */}
<p className="not-found-hint not-found-hint--desktop">
{t("notFound.orChooseInSidebar")}
</p>
<p className="not-found-hint not-found-hint--mobile">
{t("notFound.orChooseInMenu")}
</p>
</main>
);
}
3 changes: 2 additions & 1 deletion frontend/src/tests/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 24 additions & 0 deletions frontend/src/tests/NotFound.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<MemoryRouter initialEntries={["/does-not-exist"]}>
<AppRoutes />
</MemoryRouter>,
);

// 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);
});
});
Loading