Skip to content

Commit 27fff6b

Browse files
committed
implement buy item logic
1 parent 9771828 commit 27fff6b

File tree

6 files changed

+173
-2
lines changed

6 files changed

+173
-2
lines changed

examples/kendo-react-e-commerce-astro-app/src/components/CardsList.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { Badge, BadgeContainer } from "@progress/kendo-react-indicators";
22
import { Button } from "@progress/kendo-react-buttons";
33
import { cartIcon } from "@progress/kendo-svg-icons";
44
import { CardListProps } from "../data/types";
5-
import { useNavigate } from "react-router-dom";
65

76
export const CardsList = (props: CardListProps) => {
87

98
const onButtonClick = (index: number) => {
10-
// navigate(`/product/${index + 1}`);
9+
window.location.href=`/product/${index + 1}`;
1110
};
1211

1312
return (
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from "react";
2+
import { addToCart } from "../helpers/useCart";
3+
import { ProductCard } from "../components/ProductCard";
4+
import { listData } from "../data/listData";
5+
import { Layout } from "../components/Layout";
6+
import { CategoryList } from "../components/CategoryList";
7+
import { CardDescriptor } from "../data/types";
8+
import { CustomSection } from "../components/CustomizedSection";
9+
10+
interface ProductDetailsProps {
11+
id: string;
12+
}
13+
14+
export const ProductDetails: React.FC<ProductDetailsProps> = ({ id }) => {
15+
const productId = parseInt(id, 10);
16+
17+
console.log("Raw ID from props:", id);
18+
console.log("Parsed Product ID:", productId);
19+
console.log("List Data:", listData);
20+
21+
const productToAdd = listData.find((item) => item.id === productId);
22+
23+
console.log("Matched Product:", productToAdd);
24+
25+
const descriptionText =
26+
"Elegant wedding bands featuring lustrous pearls, beautifully set in sleek, timeless bands.";
27+
28+
const handleAddToCart = () => {
29+
if (productToAdd) {
30+
addToCart(productToAdd);
31+
}
32+
window.location.href = "/shoppingcart";
33+
};
34+
35+
const data: CardDescriptor[] = [
36+
{
37+
img: "/homemadePinkDiamondRing.jpg",
38+
collectionText: "Handmade Pink Diamond Ring",
39+
},
40+
{
41+
img: "/diamondRingPinkRuby.jpg",
42+
collectionText: "Diamond Ring with Pink Ruby",
43+
},
44+
{
45+
img: "/whiteSandDiamondRing.jpg",
46+
collectionText: "White Sand Diamond Ring",
47+
},
48+
];
49+
50+
return (
51+
<>
52+
<Layout>
53+
{productToAdd ? (
54+
<ProductCard
55+
title={productToAdd.title}
56+
image={productToAdd.img}
57+
subtitle="In Platinum with Natural Diamond"
58+
breadCrumbItem={[
59+
{ text: "Home" },
60+
{ text: "Jewelry" },
61+
{ text: productToAdd.category },
62+
]}
63+
rating={productToAdd.rating}
64+
reviews="208 reviews"
65+
price={productToAdd.newPrice}
66+
description={descriptionText}
67+
addToCart={handleAddToCart}
68+
/>
69+
) : (
70+
<div>Product not found</div>
71+
)}
72+
</Layout>
73+
<Layout>
74+
<CustomSection>
75+
<CategoryList
76+
title="You May Also Like"
77+
subtitle="Enjoy an excellent selection of fine jewelry"
78+
data={data}
79+
/>
80+
</CustomSection>
81+
</Layout>
82+
</>
83+
);
84+
};
85+
86+
export default ProductDetails;

examples/kendo-react-e-commerce-astro-app/src/data/listData.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,5 @@ export const listData: ListDataDescriptor[] = [
186186
id: index + 1,
187187
rating: Math.floor(Math.random() * 5) + 3,
188188
}));
189+
190+
export default listData
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { atom } from "nanostores";
2+
import { ListDataDescriptor } from "../data/types";
3+
4+
export const cartStore = atom<
5+
{ product: ListDataDescriptor; quantity: number }[]
6+
>([]);
7+
8+
export const addToCart = (product: ListDataDescriptor) => {
9+
const currentCart = cartStore.get();
10+
11+
const existingProduct = currentCart.find(
12+
(item) => item.product.id === product.id
13+
);
14+
15+
if (existingProduct) {
16+
existingProduct.quantity += 1;
17+
} else {
18+
currentCart.push({ product, quantity: 1 });
19+
}
20+
21+
cartStore.set([...currentCart]);
22+
};
23+
24+
export const removeFromCart = (productId: number) => {
25+
const currentCart = cartStore.get();
26+
const updatedCart = currentCart.filter((item) => item.product.id !== productId);
27+
cartStore.set(updatedCart);
28+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { atom } from "nanostores";
2+
import { DataModel } from "../data/types";
3+
4+
export const cartStore = atom<DataModel[]>([]);
5+
6+
export const addToCart = (item: DataModel) => {
7+
const currentCart = cartStore.get();
8+
const existingItem = currentCart.find((cartItem) => cartItem.id === item.id);
9+
10+
if (existingItem) {
11+
const updatedCart = currentCart.map((cartItem) =>
12+
cartItem.id === item.id
13+
? { ...cartItem, quantity: (cartItem.quantity || 0) + 1 }
14+
: cartItem
15+
);
16+
cartStore.set(updatedCart);
17+
} else {
18+
cartStore.set([...currentCart, { ...item, quantity: 1 }]);
19+
}
20+
};
21+
22+
export const removeFromCart = (id: number) => {
23+
const currentCart = cartStore.get();
24+
cartStore.set(currentCart.filter((item) => item.id !== id));
25+
};
26+
27+
export const clearCart = () => {
28+
cartStore.set([]);
29+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
import Layout from '/src/layouts/Layout.astro';
3+
import ProductDetails from '/src/components/ProductDetails';
4+
import listData from '/src/data/listData'; // Import product data globally
5+
6+
export async function getStaticPaths() {
7+
const paths = listData.map((product) => ({
8+
params: { id: String(product.id) }, // Dynamically generate paths
9+
}));
10+
11+
console.log('Generated paths:', paths); // Debug generated paths
12+
return paths;
13+
}
14+
15+
const { id } = Astro.params; // Access the dynamic route parameter
16+
const product = listData.find((item) => String(item.id) === id); // Find the matching product
17+
---
18+
19+
<Layout title={`Product ${id}`}>
20+
<main>
21+
{product ? (
22+
<ProductDetails id={id} product={product} client:only="react" />
23+
) : (
24+
<div>Product not found</div>
25+
)}
26+
</main>
27+
</Layout>

0 commit comments

Comments
 (0)