Skip to content

Commit

Permalink
Merge pull request #31 from fac25/update-homepage
Browse files Browse the repository at this point in the history
Created add basket functionality on homepage and link to individual p…
  • Loading branch information
yassienAbdillahi authored Oct 27, 2022
2 parents ab68f51 + e902d3a commit 8414891
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Layout from "../components/layout";
import Image from "next/image";
import Link from "next/link";
import styles from "../styles/Home.module.css";
import { getProducts } from "../database/model.js";
import { useState } from "react";


export async function getStaticProps() {
// Fetch necessary data for the blog post using params.id



let products = getProducts();
return {
props: {
Expand All @@ -21,6 +19,20 @@ export async function getStaticProps() {
export default function Home({ products }) {
const [category, setCategory] = useState("all");

function handleBasket(id) {
let localBasket = JSON.parse(localStorage.getItem("basket") || "[]");
products.filter((product) => {
if (product.id === id) {
localBasket.push({
name: product.name,
price: product.price,
image: product.src,
});
}
});
localStorage.setItem("basket", JSON.stringify(localBasket));
}

function filterByCategory() {
let filtered;
category === "all"
Expand All @@ -34,15 +46,21 @@ export default function Home({ products }) {
{filtered.map((product, index) => {
return (
<li key={index}>
<p>{product.name}</p>
<Image
src={"/images/" + product.src} // Route of the image file
height={144} // Desired size with correct aspect ratio
width={144} // Desired size with correct aspect ratio
alt={product.name}
/>
<Link href={"/products/" + product.id}>
<div>
<p>{product.name}</p>
<Image
src={"/images/" + product.src} // Route of the image file
height={144} // Desired size with correct aspect ratio
width={144} // Desired size with correct aspect ratio
alt={product.name}
/>
</div>
</Link>
<p>{product.price}</p>
<button>Add to basket</button>
<button onClick={() => handleBasket(product.id)}>
Add to basket
</button>
</li>
);
})}
Expand Down

0 comments on commit 8414891

Please sign in to comment.