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
94 changes: 74 additions & 20 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -31,6 +44,10 @@ const typeDefs = `#graphql
categoryId: ID
category: Category
prices: [Price!]!

ingredients: String
nutrition: [Nutrition!]!
allergens: [Allergen!]!
}

type Query {
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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, });

Expand Down
17 changes: 17 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Route,
useLocation,
useNavigate,
useParams,
} from "react-router";

// Felles komponenter
Expand All @@ -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 {
Expand All @@ -28,6 +30,16 @@ import { createCategoryUrl } from "./pages/home/categoryNavigation";
// Styling
import "./App.css";

function ProductDetailsRoute() {
const { ean } = useParams<{ ean: string }>();

if (!ean) {
return <p>Fant ikke produktet.</p>;
}

return <ProductDetailsPage ean={ean} />;
}

function AppLayout() {
const location = useLocation();
const navigate = useNavigate();
Expand Down Expand Up @@ -126,6 +138,11 @@ function AppLayout() {
}
/>

<Route
path="/product/:ean"
element={<ProductDetailsRoute />}
/>

<Route
path="/search"
element={<SearchResultsPage key={location.search} />}
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/pages/productDetails/ProductDetailsPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
72 changes: 64 additions & 8 deletions frontend/src/pages/productDetails/ProductDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -98,17 +106,46 @@ export default function ProductDetailsPage({
</figcaption>
</figure>


<section className="product-ingredients">
<h2>A — INGREDIENSER</h2>
<p>Ingrediensinformasjon er ikke tilgjengelig.</p>
<h2>A — INGREDIENSER</h2>
<p>
{product.ingredients?.trim() ||
"Ingrediensinformasjon er ikke tilgjengelig."}
</p>
</section>

<section className="product-allergens">
<h2>B — ALLERGENER</h2>
<h2>B — ALLERGENER</h2>

{product.allergens.length === 0 ? (
<p>Allergeninformasjon er ikke tilgjengelig.</p>
) : containsAllergens.length === 0 &&
traceAllergens.length === 0 ? (
<p>Ingen oppgitte allergener.</p>
) : (
<>
{containsAllergens.length > 0 && (
<p>
<strong>Inneholder: </strong>
{containsAllergens
.map((allergen) => allergen.displayName)
.join(", ")}
</p>
)}

{traceAllergens.length > 0 && (
<p>
<strong>Kan inneholde spor av: </strong>
{traceAllergens
.map((allergen) => allergen.displayName)
.join(", ")}
</p>
)}
</>
)}
</section>


</section>

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

<section className="product-nutrition">
<h2>C — NÆRINGSINNHOLD</h2>
<p>Næringsinnhold er ikke tilgjengelig.</p>
</section>

<section className="product-nutrition">
<h2>C — NÆRINGSINNHOLD</h2>

{product.nutrition.length > 0 ? (
<table className="product-nutrition__table">
<tbody>
{product.nutrition.map((nutrient) => (
<tr key={nutrient.displayName}>
<th scope="row">{nutrient.displayName}</th>
<td>
{nutrient.amount.toLocaleString("nb-NO")}{" "}
{nutrient.unit}
</td>
</tr>
))}
</tbody>
</table>
) : (
<p>Næringsinnhold er ikke tilgjengelig.</p>
)}
</section>

</section>
</section>
</main>
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/pages/productDetails/productQueries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { gql } from "@apollo/client";
import type { TypedDocumentNode } from "@apollo/client";
import type { Product } from "../../types/product";
Expand Down Expand Up @@ -30,6 +31,16 @@ export const PRODUCT_QUERY: TypedDocumentNode<
unitPrice
url
}
ingredients
allergens {
displayName
contains
}
nutrition {
displayName
amount
unit
}
}
}
`;
15 changes: 15 additions & 0 deletions frontend/src/types/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@ export type SearchProduct = {
export type Product = Omit<SearchProduct, "categoryId" | "prices"> & {
category: Category | null;
prices: (ProductPrice & { url: string | null })[];

ingredients: string | null;

allergens: {
displayName: string;
contains: string;
}[];

nutrition: {
displayName: string;
amount: number;
unit: string;
}[];
};