From 9db3a9c921beeffa8d570903630c59a6b995b4b8 Mon Sep 17 00:00:00 2001
From: Ingrihka
Date: Tue, 27 Oct 2026 20:15:18 +0100
Subject: [PATCH 1/3] feat: detaljsiden henter handlelisten fra backend (#156)
---
.../shoppingLists/ShoppingListDetailPage.css | 18 ++
.../shoppingLists/ShoppingListDetailPage.tsx | 165 +++++++++++-------
.../shoppingLists/shoppingListQueries.ts | 88 ++++++++++
frontend/src/types/shoppingList.ts | 4 +-
4 files changed, 207 insertions(+), 68 deletions(-)
create mode 100644 frontend/src/pages/shoppingLists/shoppingListQueries.ts
diff --git a/frontend/src/pages/shoppingLists/ShoppingListDetailPage.css b/frontend/src/pages/shoppingLists/ShoppingListDetailPage.css
index d391968..0513a58 100644
--- a/frontend/src/pages/shoppingLists/ShoppingListDetailPage.css
+++ b/frontend/src/pages/shoppingLists/ShoppingListDetailPage.css
@@ -359,3 +359,21 @@
gap: 4px;
}
}
+
+/* ---------- Feil og tom liste ---------- */
+
+.notepad__error {
+ margin: 0 0 1rem;
+ padding: 0.75rem 1rem;
+ border: 1px solid var(--text-accent);
+ border-radius: 6px;
+ color: var(--text-accent);
+ font-size: 0.875rem;
+}
+
+.notepad__empty {
+ margin: 0 0 1rem;
+ font-family: var(--font-note);
+ font-size: 1.1rem;
+ color: var(--text-muted);
+}
diff --git a/frontend/src/pages/shoppingLists/ShoppingListDetailPage.tsx b/frontend/src/pages/shoppingLists/ShoppingListDetailPage.tsx
index 323ec23..b4f6688 100644
--- a/frontend/src/pages/shoppingLists/ShoppingListDetailPage.tsx
+++ b/frontend/src/pages/shoppingLists/ShoppingListDetailPage.tsx
@@ -1,13 +1,22 @@
import { useState } from "react";
-import type { SubmitEvent } from "react";
import { Link, useParams } from "react-router";
-import { mockLists } from "./shoppingListMockData";
+import { useMutation, useQuery } from "@apollo/client/react";
import type { ShoppingListItem } from "../../types/shoppingList";
+import {
+ REMOVE_ITEM_MUTATION,
+ SET_ITEM_CHECKED_MUTATION,
+ SHOPPING_LIST_QUERY,
+ UPDATE_ITEM_QUANTITY_MUTATION,
+} from "./shoppingListQueries";
import "./ShoppingListDetailPage.css";
import Button from "../../components/button/Button";
import ShoppingListRow from "../../components/shoppingListRow/ShoppingListRow";
import CompletedListView from "./CompletedListView";
+// Detaljsiden for én handleliste (/lists/:id).
+// Lista hentes fra backend, og alle endringer lagres med en gang.
+// Mutasjonene returnerer hele lista, så Apollo oppdaterer siden av seg selv.
+
type SortOrder = "added" | "alphabetical";
// Gir f.eks. "47,80"
@@ -26,65 +35,78 @@ function formatDate(isoDate: string) {
function ShoppingListDetailPage() {
// Henter id fra adressen, f.eks. /lists/2 gir id = "2"
- const { id } = useParams();
- const list = mockLists.find((candidate) => candidate.id === id);
+ const { id = "" } = useParams();
+
+ // ---------- Data ----------
+
+ const { data, loading, error } = useQuery(SHOPPING_LIST_QUERY, {
+ variables: { id },
+ skip: !id,
+ });
+ const list = data?.shoppingList ?? null;
+
+ // ---------- Mutasjoner ----------
+
+ // Feil fra en endring vises over lista (f.eks. hvis lista er slettet imens)
+ const [actionError, setActionError] = useState("");
+ const showError = (mutationError: Error) =>
+ setActionError(mutationError.message);
+
+ const [setItemChecked] = useMutation(SET_ITEM_CHECKED_MUTATION, {
+ onError: showError,
+ });
+ const [updateItemQuantity] = useMutation(UPDATE_ITEM_QUANTITY_MUTATION, {
+ onError: showError,
+ });
+ const [removeItemFromList] = useMutation(REMOVE_ITEM_MUTATION, {
+ onError: showError,
+ });
- const [items, setItems] = useState(list?.items ?? []);
const [sortOrder, setSortOrder] = useState("added");
- const [newItem, setNewItem] = useState("");
- const checkedCount = items.filter((item) => item.checked).length;
- const total = items.reduce(
- (sum, item) => sum + (item.unitPrice ?? 0) * item.quantity,
- 0,
- );
+ // ---------- Handlinger ----------
- const sortedItems =
- sortOrder === "alphabetical"
- ? [...items].sort((a, b) => a.name.localeCompare(b.name, "nb"))
- : items;
+ function toggleChecked(item: ShoppingListItem) {
+ setActionError("");
+ setItemChecked({ variables: { itemId: item.id, checked: !item.checked } });
+ }
- function toggleChecked(itemId: string) {
- setItems((current) =>
- current.map((item) =>
- item.id === itemId ? { ...item, checked: !item.checked } : item,
- ),
- );
+ function changeQuantity(item: ShoppingListItem, change: number) {
+ const quantity = item.quantity + change;
+ if (quantity < 1) return;
+
+ setActionError("");
+ updateItemQuantity({ variables: { itemId: item.id, quantity } });
}
- function changeQuantity(itemId: string, change: number) {
- setItems((current) =>
- current.map((item) =>
- item.id === itemId
- ? { ...item, quantity: Math.max(1, item.quantity + change) }
- : item,
- ),
- );
+ function removeItem(item: ShoppingListItem) {
+ setActionError("");
+ removeItemFromList({ variables: { itemId: item.id } });
}
- function removeItem(itemId: string) {
- setItems((current) => current.filter((item) => item.id !== itemId));
+ // ---------- Laster, feil og ukjent liste ----------
+
+ if (loading) {
+ return (
+
+ Henter handlelisten …
+
+ );
}
- function addItem(event: SubmitEvent) {
- event.preventDefault();
- const name = newItem.trim();
- if (!name) return;
- setItems((current) => [
- ...current,
- {
- id: crypto.randomUUID(),
- ean: "", // fritt skrevet vare, ikke koblet til et produkt
- name,
- quantity: 1,
- unitPrice: null,
- checked: false,
- },
- ]);
- setNewItem("");
+ if (error) {
+ return (
+
+
+ ← MINE HANDLELISTER
+
+ Kunne ikke hente handlelisten
+ Prøv å laste siden på nytt.
+
+ );
}
- // Ugyldig id i adressen, f.eks. /lists/999
+ // Ugyldig id, slettet liste, eller en liste brukeren ikke har tilgang til
if (!list) {
return (
@@ -101,6 +123,18 @@ function ShoppingListDetailPage() {
return ;
}
+ // ---------- Utregninger ----------
+
+ const items = list.items;
+ const checkedCount = items.filter((item) => item.checked).length;
+ // Billigste pris × antall fra backend. null når ingen varer har pris.
+ const total = list.estimatedTotal ?? 0;
+
+ const sortedItems =
+ sortOrder === "alphabetical"
+ ? [...items].sort((a, b) => a.name.localeCompare(b.name, "nb"))
+ : items;
+
return (
@@ -152,36 +186,35 @@ function ShoppingListDetailPage() {
+ {actionError && (
+
+ {actionError}
+
+ )}
+
+ {items.length === 0 && (
+
+ Lappen er tom. Finn varer i katalogen og legg dem på.
+
+ )}
+
{sortedItems.map((item) => (
toggleChecked(item.id)}
- onDecrease={() => changeQuantity(item.id, -1)}
- onIncrease={() => changeQuantity(item.id, 1)}
- onRemove={() => removeItem(item.id)}
+ onToggle={() => toggleChecked(item)}
+ onDecrease={() => changeQuantity(item, -1)}
+ onIncrease={() => changeQuantity(item, 1)}
+ onRemove={() => removeItem(item)}
/>
))}
-
-
diff --git a/frontend/src/pages/shoppingLists/shoppingListQueries.ts b/frontend/src/pages/shoppingLists/shoppingListQueries.ts
new file mode 100644
index 0000000..8e788e1
--- /dev/null
+++ b/frontend/src/pages/shoppingLists/shoppingListQueries.ts
@@ -0,0 +1,88 @@
+import { gql } from "@apollo/client";
+import type { TypedDocumentNode } from "@apollo/client";
+import type { ShoppingList } from "../../types/shoppingList";
+
+// GraphQL for handlelistene. Alle mutasjonene returnerer hele lista med de
+// samme feltene som spørringen, så Apollo oppdaterer siden av seg selv.
+
+// ---------- Felles felter ----------
+
+const SHOPPING_LIST_FIELDS = gql`
+ fragment ShoppingListFields on ShoppingList {
+ id
+ name
+ status
+ inUse
+ createdAt
+ completedAt
+ estimatedTotal
+ items {
+ id
+ ean
+ name
+ quantity
+ note
+ checked
+ unitPrice
+ }
+ }
+`;
+
+// ---------- Typer ----------
+
+type ShoppingListData = { shoppingList: ShoppingList | null };
+type IdVariables = { id: string };
+type ItemVariables = { itemId: string };
+
+// ---------- Spørringer ----------
+
+// Én liste. null hvis den ikke finnes, eller brukeren ikke har tilgang.
+export const SHOPPING_LIST_QUERY: TypedDocumentNode<
+ ShoppingListData,
+ IdVariables
+> = gql`
+ query ShoppingList($id: ID!) {
+ shoppingList(id: $id) {
+ ...ShoppingListFields
+ }
+ }
+ ${SHOPPING_LIST_FIELDS}
+`;
+
+// ---------- Mutasjoner: varer ----------
+
+export const SET_ITEM_CHECKED_MUTATION: TypedDocumentNode<
+ { setItemChecked: ShoppingList },
+ ItemVariables & { checked: boolean }
+> = gql`
+ mutation SetItemChecked($itemId: ID!, $checked: Boolean!) {
+ setItemChecked(itemId: $itemId, checked: $checked) {
+ ...ShoppingListFields
+ }
+ }
+ ${SHOPPING_LIST_FIELDS}
+`;
+
+export const UPDATE_ITEM_QUANTITY_MUTATION: TypedDocumentNode<
+ { updateItemQuantity: ShoppingList },
+ ItemVariables & { quantity: number }
+> = gql`
+ mutation UpdateItemQuantity($itemId: ID!, $quantity: Int!) {
+ updateItemQuantity(itemId: $itemId, quantity: $quantity) {
+ ...ShoppingListFields
+ }
+ }
+ ${SHOPPING_LIST_FIELDS}
+`;
+
+export const REMOVE_ITEM_MUTATION: TypedDocumentNode<
+ { removeItemFromList: ShoppingList },
+ ItemVariables
+> = gql`
+ mutation RemoveItemFromList($itemId: ID!) {
+ removeItemFromList(itemId: $itemId) {
+ ...ShoppingListFields
+ }
+ }
+ ${SHOPPING_LIST_FIELDS}
+`;
diff --git a/frontend/src/types/shoppingList.ts b/frontend/src/types/shoppingList.ts
index a004ee3..162d3a9 100644
--- a/frontend/src/types/shoppingList.ts
+++ b/frontend/src/types/shoppingList.ts
@@ -7,7 +7,7 @@ export interface ShoppingListItem {
name: string;
quantity: number;
checked: boolean;
- note?: string;
+ note?: string | null; // null fra backend når varen ikke har notat
unitPrice?: number | null;
}
@@ -19,6 +19,6 @@ export interface ShoppingList {
createdAt: string;
items: ShoppingListItem[];
estimatedTotal: number | null;
- completedAt?: string; // dato listen ble handlet, f.eks. "2026-09-18"
+ completedAt?: string | null; // når lista ble handlet, null til den er fullført
store?: string; // butikken det ble handlet i
}
From aefa3c4b887186592d8ee80f27a56892a7a5a984 Mon Sep 17 00:00:00 2001
From: Ingrihka
Date: Tue, 27 Oct 2026 20:45:26 +0100
Subject: [PATCH 2/3] feat: handlelistene hentes og lagres i backend (#156,
#63, #68)
---
.../pages/shoppingLists/CompletedListView.css | 35 ++
.../pages/shoppingLists/CompletedListView.tsx | 74 +++-
.../shoppingLists/NewShoppingListForm.css | 60 ++++
.../shoppingLists/NewShoppingListForm.tsx | 106 ++++++
.../pages/shoppingLists/PreviousListRow.tsx | 4 +-
.../pages/shoppingLists/PreviousListsPage.tsx | 60 +++-
.../shoppingLists/ShoppingListDetailPage.css | 6 +
.../shoppingLists/ShoppingListDetailPage.tsx | 102 +++++-
.../pages/shoppingLists/ShoppingListsPage.css | 23 +-
.../pages/shoppingLists/ShoppingListsPage.tsx | 51 +--
.../shoppingLists/shoppingListMockData.ts | 319 ------------------
.../shoppingLists/shoppingListQueries.ts | 83 ++++-
12 files changed, 531 insertions(+), 392 deletions(-)
create mode 100644 frontend/src/pages/shoppingLists/NewShoppingListForm.css
create mode 100644 frontend/src/pages/shoppingLists/NewShoppingListForm.tsx
delete mode 100644 frontend/src/pages/shoppingLists/shoppingListMockData.ts
diff --git a/frontend/src/pages/shoppingLists/CompletedListView.css b/frontend/src/pages/shoppingLists/CompletedListView.css
index d4fb0ab..c797635 100644
--- a/frontend/src/pages/shoppingLists/CompletedListView.css
+++ b/frontend/src/pages/shoppingLists/CompletedListView.css
@@ -104,3 +104,38 @@
font-size: 1.1875rem;
}
}
+
+/* ---------- Bekreft sletting ---------- */
+
+.completed-list__confirm {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 0.5rem 1rem;
+}
+
+.completed-list__confirm p {
+ width: 100%;
+ margin: 0;
+ font-size: 0.875rem;
+ color: var(--text-primary);
+}
+
+.completed-list__delete--cancel {
+ color: var(--text-muted);
+}
+
+.completed-list__delete:disabled {
+ opacity: 0.5;
+ cursor: default;
+}
+
+/* Feil fra en handling (f.eks. hvis lista allerede er slettet) */
+.list-summary__error {
+ margin: 0 0 1rem;
+ padding: 0.75rem 1rem;
+ border: 1px solid var(--text-accent);
+ border-radius: 6px;
+ color: var(--text-accent);
+ font-size: 0.875rem;
+}
diff --git a/frontend/src/pages/shoppingLists/CompletedListView.tsx b/frontend/src/pages/shoppingLists/CompletedListView.tsx
index 84271bd..4e9dafa 100644
--- a/frontend/src/pages/shoppingLists/CompletedListView.tsx
+++ b/frontend/src/pages/shoppingLists/CompletedListView.tsx
@@ -1,10 +1,17 @@
+import { useState } from "react";
import { Link } from "react-router";
import type { ShoppingList } from "../../types/shoppingList";
import "./CompletedListView.css";
import Button from "../../components/button/Button";
+// Visning av en handlet (fullført) liste. Kontrollert komponent:
+// knappene sender hendelser opp til detaljsiden, som kaller backend.
type CompletedListViewProps = {
list: ShoppingList;
+ onReuse: () => void; // «Bruk på nytt»: ny aktiv kopi
+ onDelete: () => void; // Sletter lista for brukeren
+ busy?: boolean; // En handling pågår, knappene er låst
+ error?: string; // Feil fra siste handling
};
// Gir f.eks. "47,80"
@@ -55,7 +62,16 @@ function CheckIcon() {
);
}
-function CompletedListView({ list }: CompletedListViewProps) {
+function CompletedListView({
+ list,
+ onReuse,
+ onDelete,
+ busy = false,
+ error = "",
+}: CompletedListViewProps) {
+ // Sletting krever to trykk, så man ikke sletter ved et uhell
+ const [confirmDelete, setConfirmDelete] = useState(false);
+
const completedAt = list.completedAt ?? list.createdAt;
const total =
list.estimatedTotal ??
@@ -166,8 +182,19 @@ function CompletedListView({ list }: CompletedListViewProps) {
- {/* Knappene kobles til backend senere (#63) */}
-