Skip to content

Commit 8800015

Browse files
committed
basket total
1 parent 6029dfd commit 8800015

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

components/ProductFeed.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from "react";
22
import Product from "./Product";
33

4-
function ProductFeed({ products }) {
4+
function ProductFeed({ products }) {
55
return (
66
<div className="grid grid-flow-row-dense md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 lg:-mt-28 mx-auto sm:mt-0 mt-12">
77
{/* {products.slice(0,4)
@@ -15,6 +15,7 @@ function ProductFeed({ products }) {
1515
description={product.description}
1616
category={product.category}
1717
image={product.image}
18+
priority={true}
1819
/>
1920
))}
2021
<img
@@ -32,6 +33,7 @@ function ProductFeed({ products }) {
3233
description={product.description}
3334
category={product.category}
3435
image={product.image}
36+
priority={true}
3537
/>
3638
))}
3739
</div>
@@ -44,6 +46,7 @@ function ProductFeed({ products }) {
4446
description={product.description}
4547
category={product.category}
4648
image={product.image}
49+
priority={true}
4750
/>
4851
))}
4952
</div>

pages/checkout.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import Image from "next/image";
22
import Header from "../components/Header";
33
import { useSelector } from "react-redux";
4-
import { selectItems } from "../slices/basketSlice";
4+
import { selectItems,selectTotal } from "../slices/basketSlice";
55
import CheckoutProduct from "../components/CheckoutProduct";
66
import Head from "next/head";
7+
import Currency from "react-currency-formatter";
8+
import { useSession } from "next-auth/client";
79

810
function checkout() {
911
const items = useSelector(selectItems);
10-
// console.log(items);
12+
const total = useSelector(selectTotal);
13+
const [session] = useSession();
14+
1115
return (
1216
<div className="bg-gray-100">
1317
<Head>
1418
<title>Checkout Page</title>
1519
</Head>
1620
<Header />
17-
<main className="lg:flex max-w-screen-2xl mx-auto">
21+
<main className="lg:flex max-w-screen-2xl mx-auto mt-28" >
1822
{/* Left */}
19-
<div className="flex-grow m-5 shadow-sm">
23+
<div className="flex-grow m-5 shadow-sm ">
2024
<Image
2125
src="https:links.papareact.com/ikj"
22-
width={1020}
26+
width={1050}
2327
height={250}
24-
alt="shopping banner"
28+
alt="shopping banner"
2529
priority={true}
26-
className="w-auto h-auto object-contain"
30+
className="w-auto h-auto object-contain "
2731
/>
2832

2933
<div className="flex flex-col p-5 space-y-10 bg-white">
@@ -48,6 +52,26 @@ function checkout() {
4852
</div>
4953
</div>
5054
{/* Right */}
55+
<div className="flex flex-col bg-white p-10 shadow-md min-w-fit">
56+
{items.length > 0 && (
57+
<>
58+
<h2>
59+
Subtotal ({items.length} items):
60+
<span className="font-bold">
61+
<Currency quantity={total} />
62+
</span>
63+
</h2>
64+
<button
65+
className={`button mt-2 ${
66+
!session &&
67+
"from-gray-300 to-gray-500 border-gray-200 text-gray-300 cursor-not-allowed"
68+
}`}
69+
>
70+
{!session ? "Sign in to checkout" : "Proceed to checkout"}
71+
</button>
72+
</>
73+
)}
74+
</div>
5175
</main>
5276
</div>
5377
);

slices/basketSlice.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ export const { addToBasket, removeFromBasket } = basketSlice.actions;
3333

3434
// Selectors - This is how we pull information from the Global store slice
3535
export const selectItems = (state) => state.basket.items;
36+
export const selectTotal = (state) =>
37+
state.basket.items.reduce((total, item) => total + item.price, 0);
3638

3739
export default basketSlice.reducer;

0 commit comments

Comments
 (0)