ADD: Clear cart and deselect after purchase
This commit is contained in:
parent
52c2bde094
commit
042b48c576
@ -11,6 +11,7 @@ const Details = () => {
|
|||||||
const [orderID, setOrderID] = useState(null);
|
const [orderID, setOrderID] = useState(null);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
const [invoice, setInvoice] = useState(null);
|
const [invoice, setInvoice] = useState(null);
|
||||||
|
const [clearCart, setClearCart] = useState(false);
|
||||||
|
|
||||||
// refresh the cart logo and amount of the cart
|
// refresh the cart logo and amount of the cart
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -27,10 +28,10 @@ const Details = () => {
|
|||||||
<ShowCase />
|
<ShowCase />
|
||||||
{/* you can try 600011 in orderID */}
|
{/* you can try 600011 in orderID */}
|
||||||
<Orders orderID={orderID} setOrderID={setOrderID} invoice={invoice} setInvoice={setInvoice} />
|
<Orders orderID={orderID} setOrderID={setOrderID} invoice={invoice} setInvoice={setInvoice} />
|
||||||
<AllProducts setCart={setCart} />
|
<AllProducts setCart={setCart} clearCart={clearCart} setClearCart={setClearCart} />
|
||||||
<CartIcon count={cart?.length} total={total} />
|
<CartIcon count={cart?.length} total={total} />
|
||||||
<Purchase />
|
<Purchase />
|
||||||
<PayPal cart={cart} setInvoice={setInvoice} />
|
<PayPal cart={cart} setInvoice={setInvoice} setClearCart={setClearCart} />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,7 @@ const initialOptions = {
|
|||||||
// buyer account: dev01@oemautomation.com, pwd: 3Deerhead
|
// buyer account: dev01@oemautomation.com, pwd: 3Deerhead
|
||||||
// merchant account: biz01@oemautomation.com, pwd 3Deerhead
|
// merchant account: biz01@oemautomation.com, pwd 3Deerhead
|
||||||
|
|
||||||
const PayPal = ({cart, setInvoice}) => {
|
const PayPal = ({cart, setClearCart, setInvoice}) => {
|
||||||
|
|
||||||
const createOrder = (data, actions) => {
|
const createOrder = (data, actions) => {
|
||||||
return actions.order.create({
|
return actions.order.create({
|
||||||
@ -29,6 +29,7 @@ const PayPal = ({cart, setInvoice}) => {
|
|||||||
const onApprove = (data, actions) => {
|
const onApprove = (data, actions) => {
|
||||||
return actions.order.capture().then(function(details) {
|
return actions.order.capture().then(function(details) {
|
||||||
setInvoice(details);
|
setInvoice(details);
|
||||||
|
setClearCart(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ const sampleOrder = {
|
|||||||
|
|
||||||
// constructs paypal order details into correct shape for db
|
// constructs paypal order details into correct shape for db
|
||||||
const invoiceShape = (order) => {
|
const invoiceShape = (order) => {
|
||||||
// let addressInfo = order.purchase_units[0].shipping.address;
|
let addressInfo = order.purchase_units[0].shipping.address;
|
||||||
// let address = `${addressInfo.address_line_1} ${addressInfo.admin_area_2}, ${addressInfo.admin_area_1} ${addressInfo.postal_code}`;
|
let address = `${addressInfo.address_line_1} ${addressInfo.admin_area_2}, ${addressInfo.admin_area_1} ${addressInfo.postal_code}`;
|
||||||
return {
|
return {
|
||||||
items: order.purchase_units.map((item) => {
|
items: order.purchase_units.map((item) => {
|
||||||
return {
|
return {
|
||||||
@ -29,10 +29,13 @@ const invoiceShape = (order) => {
|
|||||||
return a + +b.amount.value;
|
return a + +b.amount.value;
|
||||||
}, 0),
|
}, 0),
|
||||||
orderDate: order.create_time,
|
orderDate: order.create_time,
|
||||||
// email: order.payer.email_address,
|
email: order.payer.email_address,
|
||||||
orderByFirstName: order.payer.name.given_name,
|
orderByFirstName: order.payer.name.given_name,
|
||||||
orderByLastName: order.payer.name.surname,
|
orderByLastName: order.payer.name.surname,
|
||||||
// address: address
|
shipping: {
|
||||||
|
shipTo: `${order.payer.name.given_name} ${order.payer.name.surname}`,
|
||||||
|
address: address
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,13 +53,11 @@ const Orders = ({ orderID, setOrderID, invoice, setInvoice }) => {
|
|||||||
}, [results, setOrderID]); //didUpdate
|
}, [results, setOrderID]); //didUpdate
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("Invoice: ", invoice && invoiceShape(invoice));
|
|
||||||
if (invoice) {
|
if (invoice) {
|
||||||
// console.log("Invoice: ", invoiceShape(invoice));
|
|
||||||
setOrder(invoiceShape(invoice));
|
setOrder(invoiceShape(invoice));
|
||||||
setInvoice(null);
|
setInvoice(null);
|
||||||
}
|
}
|
||||||
}, [invoice, setOrder, setInvoice]); //missing dep warning
|
}, [invoice, setOrder, setInvoice]);
|
||||||
|
|
||||||
const onClick = (event) => {
|
const onClick = (event) => {
|
||||||
setOrder(sampleOrder);
|
setOrder(sampleOrder);
|
||||||
|
@ -2,19 +2,23 @@ import React, { useState, useEffect } from 'react';
|
|||||||
import { useGetProd } from '../api';
|
import { useGetProd } from '../api';
|
||||||
import './allProducts.css';
|
import './allProducts.css';
|
||||||
|
|
||||||
const AllProducts = ({ setCart }) => {
|
const AllProducts = ({ setCart, clearCart, setClearCart }) => {
|
||||||
const allProducts = useGetProd();
|
const allProducts = useGetProd();
|
||||||
const [productState, setProductState] = useState([]);
|
const [productState, setProductState] = useState([]);
|
||||||
|
|
||||||
// adds "selected" property to each product for checkbox functionality
|
// adds "selected" property to each product for checkbox functionality
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (allProducts) {
|
if (allProducts || clearCart) {
|
||||||
setProductState(allProducts.map(p => ({
|
setProductState(allProducts.map(p => ({
|
||||||
selected: false,
|
selected: false,
|
||||||
...p
|
...p
|
||||||
})));
|
})));
|
||||||
|
if (clearCart) {
|
||||||
|
setClearCart(false); //reset flag after clearing cart / unchecking items
|
||||||
|
setCart([]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [allProducts])
|
}, [allProducts, clearCart, setClearCart, setCart])
|
||||||
|
|
||||||
// when checkbox is changed, the corresponding product "selected" value is inversed
|
// when checkbox is changed, the corresponding product "selected" value is inversed
|
||||||
// cart state is filled with products that have TRUE "selected" value
|
// cart state is filled with products that have TRUE "selected" value
|
||||||
|
Loading…
Reference in New Issue
Block a user