Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 152 additions & 50 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-icons": "^4.2.0",
"react-qr-reader": "^2.2.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"recoil": "^0.1.2",
Expand Down Expand Up @@ -52,6 +53,7 @@
]
},
"devDependencies": {
"@types/react-qr-reader": "^2.1.3",
"@types/react-router-dom": "^5.1.7",
"@types/styled-components": "^5.1.7",
"eslint": "^7.19.0",
Expand Down
12 changes: 0 additions & 12 deletions src/pages/Cart.tsx

This file was deleted.

34 changes: 34 additions & 0 deletions src/pages/Cart/QrScanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useState } from "react";
import QrReader from "react-qr-reader";
import { useEnablePurchase } from "state/cart";

const QrScanner = () => {
const enablePurchase = useEnablePurchase();
const [isScanning, setIsScanning] = useState(false);

// TODO Handle error
const handleError = (error: any) => {
console.log(error);
};

const handleScan = (data: string | null) => {
if (data) {
setIsScanning(false);
enablePurchase();
}
};

if (isScanning)
return (
<QrReader
delay={300}
onError={handleError}
onScan={handleScan}
style={{ width: "100%" }}
/>
);

return <button onClick={() => setIsScanning(true)}>Scan QR</button>;
};

export default QrScanner;
28 changes: 28 additions & 0 deletions src/pages/Cart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BottomNavigationBar from "components/BottomNavigationBar";
import { useRecoilState } from "recoil";
import cartState from "state/cart";
import QRScanner from "./QrScanner";

type PurchaseButtonProps = {
isConfirmed: boolean;
};

const PurchaseButton = (props: PurchaseButtonProps) => {
if (props.isConfirmed) {
return <button>Purchase</button>;
}
return <QRScanner />;
};

const Cart = () => {
const [cart] = useRecoilState(cartState);
return (
<div>
<p>Cart</p>
<PurchaseButton isConfirmed={cart.cartConfirmed} />
<BottomNavigationBar />
</div>
);
};

export default Cart;
10 changes: 5 additions & 5 deletions src/pages/Products/Product.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { IMAGE_URI } from "api";
import React from "react";
import { useRecoilState } from "recoil";
import cartState, { addCartItem } from "state/cart";
import { useAddCartItem } from "state/cart";
import styled from "styled-components";
import { Product as ProductType } from "types/inventory";

Expand Down Expand Up @@ -35,18 +33,20 @@ const Description = styled.p`
text-align: left;
font-weight: 200;
`;

type Props = {
product: ProductType;
};

export const Product = (props: Props) => {
const [cart, setCart] = useRecoilState(cartState);
const addCartItem = useAddCartItem();
const { product } = props;
const imageSrc = product.image
? IMAGE_URI(product.image.sm)
: `${process.env.PUBLIC_URL}/images/noImage.png`;
const cartItem = { name: product.name, price: product.price, quantity: 1 };
return (
<Wrapper onClick={() => setCart(addCartItem(cart, cartItem))}>
<Wrapper onClick={() => addCartItem(cartItem)}>
<Image src={imageSrc}></Image>
<TextWrapper>
<Name>{product.name}</Name>
Expand Down
1 change: 1 addition & 0 deletions src/pages/Products/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Wrapper = styled.div`

const Products = () => {
const [inventory] = useRecoilState(inventoryState);
if (!inventory) return null;
return (
<div>
<Wrapper>
Expand Down
43 changes: 35 additions & 8 deletions src/state/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@ import { atom, useRecoilState } from "recoil";

type CartItem = { name: string; price: number; quantity: number };

const cartState = atom<CartItem[]>({
interface CartState {
items: CartItem[];
cartConfirmed: boolean;
}

const initialState: CartState = {
items: [],
cartConfirmed: false,
};

const cartState = atom<CartState>({
key: "cart",
default: [],
default: initialState,
});

export const useGetCartTotal = () => {
const [cart] = useRecoilState(cartState);
return cart.reduce(
return cart.items.reduce(
(previousValue, currentValue) =>
previousValue + currentValue.price * currentValue.quantity,
0
);
};

export const addCartItem = (cart: CartItem[], cartItem: CartItem) => {
const cartItemIndex = cart.findIndex((item) => item.name === cartItem.name);
export const useAddCartItem = () => {
const [cart, setCart] = useRecoilState(cartState);
const addItemAndResetConfirmation = (cartItem: CartItem) => {
const newCartItems = addCartItem(cart.items, cartItem);
setCart({ ...cart, items: newCartItems, cartConfirmed: false });
};
return addItemAndResetConfirmation;
};

export const useEnablePurchase = () => {
const [cart, setCart] = useRecoilState(cartState);
const enablePurchase = () => setCart({ ...cart, cartConfirmed: true });
return enablePurchase;
};

const addCartItem = (cartItems: CartItem[], cartItem: CartItem) => {
const cartItemIndex = cartItems.findIndex(
(item) => item.name === cartItem.name
);
if (cartItemIndex === -1) {
return [...cart, cartItem];
return [...cartItems, cartItem];
}
const mutatedCartItem = {
...cartItem,
quantity: cart[cartItemIndex].quantity + 1,
quantity: cartItems[cartItemIndex].quantity + 1,
};
const filteredCart = cart.filter((item) => item.name !== cartItem.name);
const filteredCart = cartItems.filter((item) => item.name !== cartItem.name);
return [...filteredCart, mutatedCartItem];
};

Expand Down
1 change: 1 addition & 0 deletions src/types/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Product = {
name: string;
};
};

export type CartItem = {
object_id: number;
quantity: number;
Expand Down