Skip to content

Commit c7d333f

Browse files
authored
Merge pull request #168 from w3bdesign/development
Finish category page
2 parents f3f8a3a + 629878a commit c7d333f

File tree

3 files changed

+118
-8
lines changed

3 files changed

+118
-8
lines changed

components/Category/Categories.component.jsx

Lines changed: 11 additions & 8 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,15 +11,17 @@ const Categories = ({ categories }) => {
1011
<>
1112
<section className="container mx-auto bg-white">
1213
<div className="flex ">
13-
{categories.map(({ name }) => (
14-
<div
15-
key={uuidv4()}
16-
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
17-
>
18-
<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>
14+
{categories.map(({ id, name, slug }) => (
15+
<Link as={`/kategori/${slug}?id=${id}`} href="/kategori/[id]">
16+
<div
17+
key={uuidv4()}
18+
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
19+
>
20+
<div className="flex items-center justify-center w-full h-16 text-center border border-gray-300 rounded-lg shadow hover:shadow-outline">
21+
<p className="text-lg">{name}</p>
22+
</div>
2023
</div>
21-
</div>
24+
</Link>
2225
))}
2326
</div>
2427
</section>

pages/kategori/[slug].js

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

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)