Skip to content

Instantly share code, notes, and snippets.

@hadnazzar
Created June 6, 2022 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadnazzar/16db3e51fd99d0c6b4e0ab559685ddc5 to your computer and use it in GitHub Desktop.
Save hadnazzar/16db3e51fd99d0c6b4e0ab559685ddc5 to your computer and use it in GitHub Desktop.
import styles from './products.module.css';
import {Grid} from "@mui/material";
import Link from 'next/link'
import Image from 'next/image';
export type ProductType = {
id: number,
title: string,
description: string,
price: number,
discountPercentage: number,
rating: number,
stock: number,
brand: string,
category: string,
thumbnail: string,
images: string[]
}
type ProductsPagePropsType = {
products: ProductType[]
}
export const getStaticProps = async () => {
const response = await fetch('https://dummyjson.com/products')
const data = await response.json();
const products = data.products;
return {
props: {
products
}
}
}
const Products = ({products}: ProductsPagePropsType) => {
return (
<div className={styles.productPage}>
Products
<div>
{products.map((product)=> {
return (
<Link key={product.id} href={`/products/${product.id}`}>
<div className={styles.productItem} key={product.id}>
<Grid container spacing={2}>
<Grid className={styles.productTitle} item xs={8}>
{product.title}
</Grid>
<Grid item xs={4}>
<Image className={styles.productImage} src={product.thumbnail} alt={'product image'}/>
</Grid>
</Grid>
</div>
</Link>
)
})}
</div>
</div>
)
}
export default Products;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment