diff --git a/backend/src/index.ts b/backend/src/index.ts index f5aca64..8d93e27 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -23,6 +23,19 @@ const typeDefs = `#graphql url: String } + type Nutrition { + code: String! + displayName: String! + amount: Float! + unit: String! + } + + type Allergen { + code: String! + displayName: String! + contains: String! + } + type Product { ean: String! name: String! @@ -31,6 +44,10 @@ const typeDefs = `#graphql categoryId: ID category: Category prices: [Price!]! + + ingredients: String + nutrition: [Nutrition!]! + allergens: [Allergen!]! } type Query { @@ -225,23 +242,26 @@ const resolvers = { return result.rows; }, - product: async (_parent: unknown, args: { ean: string }) => { - const result = await pool.query( - ` - SELECT - ean, - name, - brand, - image_url AS "imageUrl", - category_id AS "categoryId" - FROM products - WHERE ean = $1 - `, - [args.ean], - ); - - return result.rows[0] ?? null; - }, + product: async (_parent: unknown, args: { ean: string }) => { + const result = await pool.query( + ` + SELECT + ean, + name, + brand, + image_url AS "imageUrl", + category_id AS "categoryId", + ingredients, + nutrition, + allergens + FROM products + WHERE ean = $1 + `, + [args.ean], + ); + + return result.rows[0] ?? null; + }, products: async ( @@ -602,10 +622,44 @@ const resolvers = { [product.ean], ); - return result.rows; + return result.rows; + }, + + nutrition: ( + product: { + nutrition?: { + code: string; + display_name: string; + amount: number; + unit: string; + }[] | null; + }, + ) => { + return (product.nutrition ?? []).map((item) => ({ + code: item.code, + displayName: item.display_name, + amount: item.amount, + unit: item.unit, + })); + }, + + allergens: ( + product: { + allergens?: { + code: string; + display_name: string; + contains: string; + }[] | null; + }, + ) => { + return (product.allergens ?? []).map((item) => ({ + code: item.code, + displayName: item.display_name, + contains: item.contains, + })); + }, }, - }, -}; + }; const server = new ApolloServer({ typeDefs, resolvers, introspection: true, }); diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 92d8830..55aeaf9 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -5,6 +5,7 @@ import { Route, useLocation, useNavigate, + useParams, } from "react-router"; // Felles komponenter @@ -17,6 +18,7 @@ import SearchResultsPage from "./pages/SearchResults/SearchResultsPage"; import ShoppingListsPage from "./pages/shoppingLists/ShoppingListsPage"; import ShoppingListDetailPage from "./pages/shoppingLists/ShoppingListDetailPage"; import PreviousListsPage from "./pages/shoppingLists/PreviousListsPage"; +import ProductDetailsPage from "./pages/productDetails/ProductDetailsPage"; // Hjelpefunksjoner import { @@ -28,6 +30,16 @@ import { createCategoryUrl } from "./pages/home/categoryNavigation"; // Styling import "./App.css"; +function ProductDetailsRoute() { + const { ean } = useParams<{ ean: string }>(); + + if (!ean) { + return

Fant ikke produktet.

; + } + + return ; +} + function AppLayout() { const location = useLocation(); const navigate = useNavigate(); @@ -126,6 +138,11 @@ function AppLayout() { } /> + } + /> + } diff --git a/frontend/src/pages/productDetails/ProductDetailsPage.css b/frontend/src/pages/productDetails/ProductDetailsPage.css index 58950b0..14e465d 100644 --- a/frontend/src/pages/productDetails/ProductDetailsPage.css +++ b/frontend/src/pages/productDetails/ProductDetailsPage.css @@ -381,6 +381,27 @@ margin-top: 38px; } + +.product-details-page .product-nutrition__table { + width: 100%; + border-collapse: collapse; +} + +.product-details-page .product-nutrition__table th { + text-align: left; + font-weight: 600; + color: var(--text-primary); + padding: 5px 0; +} + +.product-details-page .product-nutrition__table td { + text-align: right; + color: var(--text-primary); + padding: 5px 0; + white-space: nowrap; +} + + .product-details-page .product-nutrition dl { display: grid; grid-template-columns: minmax(0, 1fr) auto; diff --git a/frontend/src/pages/productDetails/ProductDetailsPage.tsx b/frontend/src/pages/productDetails/ProductDetailsPage.tsx index dc9ca26..d1046cc 100644 --- a/frontend/src/pages/productDetails/ProductDetailsPage.tsx +++ b/frontend/src/pages/productDetails/ProductDetailsPage.tsx @@ -57,6 +57,14 @@ export default function ProductDetailsPage({ ); } + const containsAllergens = product.allergens.filter( + (allergen) => allergen.contains === "YES" + ); + + const traceAllergens = product.allergens.filter( + (allergen) => allergen.contains === "CAN_CONTAIN_TRACES" + ); + const storePrices = [...product.prices].sort( (a, b) => a.price - b.price ); @@ -98,17 +106,46 @@ export default function ProductDetailsPage({ +
-

A — INGREDIENSER

-

Ingrediensinformasjon er ikke tilgjengelig.

+

A — INGREDIENSER

+

+ {product.ingredients?.trim() || + "Ingrediensinformasjon er ikke tilgjengelig."} +

-

B — ALLERGENER

+

B — ALLERGENER

+ + {product.allergens.length === 0 ? (

Allergeninformasjon er ikke tilgjengelig.

+ ) : containsAllergens.length === 0 && + traceAllergens.length === 0 ? ( +

Ingen oppgitte allergener.

+ ) : ( + <> + {containsAllergens.length > 0 && ( +

+ Inneholder: + {containsAllergens + .map((allergen) => allergen.displayName) + .join(", ")} +

+ )} + + {traceAllergens.length > 0 && ( +

+ Kan inneholde spor av: + {traceAllergens + .map((allergen) => allergen.displayName) + .join(", ")} +

+ )} + + )}
- {/* Høyre side: Produktinformasjon */} @@ -209,10 +246,29 @@ export default function ProductDetailsPage({ /> -
-

C — NÆRINGSINNHOLD

-

Næringsinnhold er ikke tilgjengelig.

-
+ +
+

C — NÆRINGSINNHOLD

+ + {product.nutrition.length > 0 ? ( + + + {product.nutrition.map((nutrient) => ( + + + + + ))} + +
{nutrient.displayName} + {nutrient.amount.toLocaleString("nb-NO")}{" "} + {nutrient.unit} +
+ ) : ( +

Næringsinnhold er ikke tilgjengelig.

+ )} +
+ diff --git a/frontend/src/pages/productDetails/productQueries.ts b/frontend/src/pages/productDetails/productQueries.ts index 7e20417..c127f3d 100644 --- a/frontend/src/pages/productDetails/productQueries.ts +++ b/frontend/src/pages/productDetails/productQueries.ts @@ -1,3 +1,4 @@ + import { gql } from "@apollo/client"; import type { TypedDocumentNode } from "@apollo/client"; import type { Product } from "../../types/product"; @@ -30,6 +31,16 @@ export const PRODUCT_QUERY: TypedDocumentNode< unitPrice url } + ingredients + allergens { + displayName + contains + } + nutrition { + displayName + amount + unit + } } } `; \ No newline at end of file diff --git a/frontend/src/types/product.ts b/frontend/src/types/product.ts index 8f470b1..2385f4e 100644 --- a/frontend/src/types/product.ts +++ b/frontend/src/types/product.ts @@ -24,4 +24,19 @@ export type SearchProduct = { export type Product = Omit & { category: Category | null; prices: (ProductPrice & { url: string | null })[]; + + ingredients: string | null; + + allergens: { + displayName: string; + contains: string; + }[]; + + nutrition: { + displayName: string; + amount: number; + unit: string; + }[]; }; + +