Skip to content

Commit 4a1d7b0

Browse files
committed
Add GQL query for fetching products from category
1 parent 3e05b2f commit 4a1d7b0

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

components/Category/Categories.component.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Link from 'next/link';
12
import { v4 as uuidv4 } from 'uuid';
23

34
/**
@@ -10,13 +11,18 @@ const Categories = ({ categories }) => {
1011
<>
1112
<section className="container mx-auto bg-white">
1213
<div className="flex ">
13-
{categories.map(({ name }) => (
14+
{categories.map(({ id, name, slug }) => (
1415
<div
1516
key={uuidv4()}
1617
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
1718
>
1819
<div className="flex items-center justify-center w-full h-16 text-center border border-gray-300 rounded-lg shadow hover:shadow-outline">
19-
<p className="text-lg">{name}</p>
20+
<Link
21+
as={`/kategori/${slug}-${id}`}
22+
href={`/kategori?slug=${slug}-${id}`}
23+
>
24+
<p className="text-lg">{name}</p>
25+
</Link>
2026
</div>
2127
</div>
2228
))}

pages/kategori/[slug].js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { withRouter } from 'next/router';
2+
3+
import SingleProduct from 'components/Product/SingleProduct.component';
4+
import client from 'utils/apollo/ApolloClient';
5+
6+
import { GET_PRODUCTS_FROM_CATEGORY } from 'utils/const/GQL_QUERIES';
7+
8+
/**
9+
* Display a single product with dynamic pretty urls
10+
*/
11+
const Produkt = (props) => {
12+
const { product } = props;
13+
14+
const error = false;
15+
16+
return (
17+
<>
18+
{product ? (
19+
<SingleProduct product={product} />
20+
) : (
21+
<div className="mt-8 text-2xl text-center">Laster produkt ...</div>
22+
)}
23+
{/* Display error message if error occured */}
24+
{error && (
25+
<div className="mt-8 text-2xl text-center">
26+
Feil under lasting av produkt ...
27+
</div>
28+
)}
29+
</>
30+
);
31+
};
32+
33+
export default withRouter(Produkt);
34+
35+
export async function getStaticProps(context) {
36+
let {
37+
query: { slug, productId },
38+
} = context;
39+
40+
const id = productId;
41+
42+
const res = await client.query({
43+
query: GET_PRODUCTS_FROM_CATEGORY,
44+
variables: { id },
45+
});
46+
47+
return {
48+
props: { product: res.data.product },
49+
};
50+
}

utils/const/GQL_QUERIES.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,56 @@ export const FETCH_ALL_CATEGORIES_QUERY = gql`
118118
}
119119
`;
120120

121+
export const GET_PRODUCTS_FROM_CATEGORY = gql`
122+
query ProductsFromCategory($id: ID!) {
123+
productCategory(id: $id) {
124+
id
125+
name
126+
products(first: 50) {
127+
nodes {
128+
id
129+
productId
130+
averageRating
131+
slug
132+
description
133+
image {
134+
id
135+
uri
136+
title
137+
srcSet
138+
sourceUrl
139+
}
140+
name
141+
... on SimpleProduct {
142+
price
143+
id
144+
}
145+
... on VariableProduct {
146+
price
147+
id
148+
}
149+
... on ExternalProduct {
150+
price
151+
id
152+
externalUrl
153+
}
154+
... on GroupProduct {
155+
products {
156+
nodes {
157+
... on SimpleProduct {
158+
id
159+
price
160+
}
161+
}
162+
}
163+
id
164+
}
165+
}
166+
}
167+
}
168+
}
169+
`;
170+
121171
export const GET_CART = gql`
122172
query GET_CART {
123173
cart {

0 commit comments

Comments
 (0)