Skip to content

Commit

Permalink
We can now display products from a single category
Browse files Browse the repository at this point in the history
  • Loading branch information
w3bdesign committed Jul 4, 2020
1 parent 4a1d7b0 commit 629878a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
19 changes: 8 additions & 11 deletions components/Category/Categories.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ const Categories = ({ categories }) => {
<section className="container mx-auto bg-white">
<div className="flex ">
{categories.map(({ id, name, slug }) => (
<div
key={uuidv4()}
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
>
<div className="flex items-center justify-center w-full h-16 text-center border border-gray-300 rounded-lg shadow hover:shadow-outline">
<Link
as={`/kategori/${slug}-${id}`}
href={`/kategori?slug=${slug}-${id}`}
>
<Link as={`/kategori/${slug}?id=${id}`} href="/kategori/[id]">
<div
key={uuidv4()}
className="flex flex-col justify-around p-6 cursor-pointer xs:w-1/2 md:w-1/3 xl:w-1/4"
>
<div className="flex items-center justify-center w-full h-16 text-center border border-gray-300 rounded-lg shadow hover:shadow-outline">
<p className="text-lg">{name}</p>
</Link>
</div>
</div>
</div>
</Link>
))}
</div>
</section>
Expand Down
27 changes: 17 additions & 10 deletions pages/kategori/[slug].js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { withRouter } from 'next/router';

import SingleProduct from 'components/Product/SingleProduct.component';
import IndexProducts from 'components/Product/IndexProducts.component';
import PageTitle from 'components/Header/PageTitle.component';

import client from 'utils/apollo/ApolloClient';

import { GET_PRODUCTS_FROM_CATEGORY } from 'utils/const/GQL_QUERIES';

/**
* Display a single product with dynamic pretty urls
*/
const Produkt = (props) => {
const { product } = props;
const Produkt = ({ categoryName, products }) => {


const error = false;

return (
<>
{product ? (
<SingleProduct product={product} />
{products ? (
<>
<PageTitle title={categoryName} marginleft="50" />

<IndexProducts products={products} />
</>
) : (
<div className="mt-8 text-2xl text-center">Laster produkt ...</div>
)}
Expand All @@ -32,19 +38,20 @@ const Produkt = (props) => {

export default withRouter(Produkt);

export async function getStaticProps(context) {
export async function getServerSideProps(context) {
let {
query: { slug, productId },
query: { id },
} = context;

const id = productId;

const res = await client.query({
query: GET_PRODUCTS_FROM_CATEGORY,
variables: { id },
});

return {
props: { product: res.data.product },
props: {
categoryName: res.data.productCategory.name,
products: res.data.productCategory.products.nodes,
},
};
}

0 comments on commit 629878a

Please sign in to comment.