Skip to content

Commit

Permalink
Merge pull request #11 from include-davis/feat/products-page
Browse files Browse the repository at this point in the history
Feat/products page
  • Loading branch information
Austin2Shih authored May 13, 2024
2 parents 41fbd12 + 85d0866 commit df66e4b
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/(pages)/_data/navLinks.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"name": "shop",
"slug": "/shop"
"slug": "/products"
},
{
"name": "about",
Expand Down
10 changes: 10 additions & 0 deletions app/(pages)/_globals/globals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ p {
input, select {
font-size: 1rem;
}

button {
background-color: var(--mid-background);
color: var(--text-light);
padding: var(--small-spacer);
border-radius: var(--b-radius);
border: none;
font-size: 1.125rem;
cursor: pointer;
}
26 changes: 26 additions & 0 deletions app/(pages)/_hooks/useProduct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use client';
import { useEffect, useState } from 'react';
import products from '../products/_data/products';

interface Product {
id: string;
src: string;
alt: string;
name: string;
price: number;
}

export default function useProduct(id: string) {
const [product, setProduct] = useState<Product | null>(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
const filteredProducts = products.filter((p) => p.id === id);
if (filteredProducts.length !== 0) {
setProduct(filteredProducts[0]);
}
setLoading(false);
}, [id]);

return { product, loading };
}
Empty file.
23 changes: 23 additions & 0 deletions app/(pages)/product/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client';
import useProduct from '@hooks/useProduct';
import ProductPage from '../_components/ProductPage/ProductPage';

interface Product {
id: string;
src: string;
alt: string;
name: string;
price: number;
}

export default function Product({ params }: { params: { id: string } }) {
const { product, loading } = useProduct(params.id);
if (loading) {
return 'loading...';
}

if (product === null) {
return 'product does not exist';
}
return <ProductPage {...(product as Product)} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$mid-gap: var(--spacer);

.container {
display: flex;
flex-direction: row;
padding: var(--medium-spacer);
gap: $mid-gap;
background-color: var(--light-foreground);
}

.main_img_container {
width: calc(calc(100% - $mid-gap) / 2);
aspect-ratio: calc(5/4);
position: relative;

.main_image {
object-fit: cover;
}
}

.prod_information {
display: flex;
flex-direction: column;
gap: var(--spacer);
}
25 changes: 25 additions & 0 deletions app/(pages)/product/_components/ProductPage/ProductPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import styles from './ProductPage.module.scss';
import Image from 'next/image';

interface Product {
id: string;
src: string;
alt: string;
name: string;
price: number;
}

export default function ProductPage({ id: _, src, alt, name, price }: Product) {
return (
<div className={styles.container}>
<div className={styles.main_img_container}>
<Image src={src} alt={alt} fill className={styles.main_image} />
</div>
<div className={styles.prod_information}>
<h2>{name}</h2>
<p>${price}</p>
<button className={styles.add_to_cart_button}>add to cart</button>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
interface Product {
id: string;
src: string;
alt: string;
name: string;
price: number;
}

const products = [
{
id: '1',
Expand Down Expand Up @@ -43,4 +51,4 @@ const products = [
},
];

export default products;
export default products as Product[];
File renamed without changes.
File renamed without changes.

0 comments on commit df66e4b

Please sign in to comment.