Skip to content

Commit df8782b

Browse files
committedMay 26, 2021
Added grouping basket item by categpry
1 parent d3617d7 commit df8782b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed
 

‎firebase.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import firebase from 'firebase'
22

33
const firebaseConfig = {
4-
// you firebase configs
4+
apiKey: "AIzaSyAbhucudE4NX2f0DOFdl3FABGKBa2urhjU",
5+
authDomain: "amaz-clone-ks.firebaseapp.com",
6+
projectId: "amaz-clone-ks",
7+
storageBucket: "amaz-clone-ks.appspot.com",
8+
messagingSenderId: "379541826028",
9+
appId: "1:379541826028:web:4848bdf4e86000949a38c9"
510
}
611

712
const app = !firebase.apps.length ? firebase.initializeApp(firebaseConfig) : firebase.app();

‎src/components/Product.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@heroicons/react/outline'
1313
import QuickView from "./QuickView";
1414

15-
function Product({id, title, price, description, image, shipping, colors, company, setShowCart, products}) {
15+
function Product({id, title, price, category, description, image, shipping, colors, company, setShowCart, products}) {
1616
const dispatch = useDispatch()
1717
const MAX_RATING = 5
1818
const MIN_RATING = 1
@@ -23,7 +23,7 @@ function Product({id, title, price, description, image, shipping, colors, compan
2323
const [added, setAdded] = useState(false)
2424

2525
const addItemToBasket = () => {
26-
const product = {id, title, price, description, image, shipping, colors, quantity: 1}
26+
const product = {id, title, price, category, description, image, shipping, colors, quantity: 1}
2727

2828
dispatch(addToBasket(product))
2929
setShowCart(true)
@@ -72,7 +72,7 @@ function Product({id, title, price, description, image, shipping, colors, compan
7272
<p className="text-xs text-gray-500">Free Next-day delivery</p>
7373
</div>
7474
)}
75-
<button title="Please Click MEEEE" onClick={addItemToBasket} className="mt-auto button">{added ? 'Added' : 'Add to Busket'}</button>
75+
<button title="Add to cart" onClick={addItemToBasket} className="mt-auto button">{added ? 'Added' : 'Add to Busket'}</button>
7676
</div>
7777
</>
7878
</Fade>

‎src/pages/checkout.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useSession } from "next-auth/client"
88
import { loadStripe } from '@stripe/stripe-js';
99
import axios from 'axios'
1010
import Head from "next/head"
11+
import { useEffect, useState } from "react"
1112

1213
const stripePromise = loadStripe(process.env.stripe_public_key);
1314

@@ -16,7 +17,7 @@ function Checkout({products}) {
1617
const items = useSelector(selectItems)
1718
const totalPrice = useSelector(selectTotal)
1819
const selectTotalItem = useSelector(selectTotalItems)
19-
20+
const [categorys, setCategorys] = useState([])
2021

2122
const createCheckoutSession = async () => {
2223
const stripe = await stripePromise
@@ -36,6 +37,12 @@ function Checkout({products}) {
3637
if (result.error) alert(result.error.message)
3738
}
3839

40+
useEffect(() => {
41+
const allCategories = items.map(item => item.category)
42+
const unique = [...new Set(allCategories)]
43+
setCategorys(unique)
44+
}, [items])
45+
3946
return (
4047
<>
4148
<Head>
@@ -54,9 +61,20 @@ function Checkout({products}) {
5461
{!!items.length ? "Your Shopping Basket" : "Your Busket is emty"}
5562
</h1>
5663

57-
{!!items.length && items.map(item => (
58-
<CheckoutProduct key={item.id} {...item} />
59-
))}
64+
<div className="mb-5">
65+
{!!categorys.length && categorys.map(category => (
66+
<>
67+
<h1 className="text-xl pb-4 font-medium">
68+
{category}
69+
</h1>
70+
<div className="mb-14">
71+
{!!items.length && items.filter(item => item.category === category).map(item =>
72+
<CheckoutProduct key={item.id} {...item} />
73+
)}
74+
</div>
75+
</>
76+
))}
77+
</div>
6078
</div>
6179

6280
</div>

0 commit comments

Comments
 (0)