diff --git a/components/ShoppingCartItem.tsx b/components/ShoppingCartItem.tsx deleted file mode 100644 index a7b8a57..0000000 --- a/components/ShoppingCartItem.tsx +++ /dev/null @@ -1,103 +0,0 @@ -import { FC, useState } from 'react'; - -import { DisplayProduct } from './ProductCard'; -import { PlusIcon, MinusIcon, CloseIcon } from './Icons'; - -interface Props { - item: DisplayProduct & { _id: string; }; - setModified: (modified: boolean) => void; -} - -const ShoppingCartItem: FC = ({ item, setModified }) => { - const [deleted, setDeleted] = useState(false); - const [cart, setCart] = useState([]); - const [amount, setAmount] = useState(item.amount); - const [price, setPrice] = useState(item.price); - - const removeFromCart = (): void => { - if (window === undefined) return; - if (!window.localStorage.getItem('cart')) return; - if (!cart.length) setCart([...cart, ...JSON.parse(window.localStorage.getItem('cart')!)]); - - setCart(cart.filter((e: DisplayProduct) => e._id !== item._id)); - setDeleted(true); - - window.localStorage.setItem('cart', JSON.stringify(cart)); - setModified(true); - }; - - const increaseAmount = (): void => { - if (window === undefined) return; - if (!window.localStorage.getItem('cart')) return; - if (!cart.length) setCart([...cart, ...JSON.parse(window.localStorage.getItem('cart')!)]); - if (item.stock < amount + 1) return; - - const newCart = cart; - const newItem = newCart.find(cartItem => cartItem._id === item._id); - if (!newItem) return; - newItem.amount++; - setCart(newCart); - setPrice(newItem.amount * (price / amount)); - setAmount(newItem.amount); - - window.localStorage.setItem('cart', JSON.stringify(cart)); - setModified(true); - }; - - const decreaseAmount = (): void => { - if (window === undefined) return; - if (!window.localStorage.getItem('cart')) return; - if (!cart.length) setCart([...cart, ...JSON.parse(window.localStorage.getItem('cart')!)]); - if (amount - 1 < 0) return; - if (amount - 1 === 0) return removeFromCart(); - - const newCart = cart; - const newItem = newCart.find(cartItem => cartItem._id === item._id); - - if (!newItem) return; - - newItem.amount--; - - setCart(newCart); - setAmount(newItem.amount); - setPrice(newItem.amount * (price / amount)); - - window.localStorage.setItem('cart', JSON.stringify(cart)); - setModified(true); - }; - - return <> - {!deleted && ( -
- {item.name} -
-
-

{item.name}

-

€{(price / amount / 100).toFixed(2)} elk

-
-
-
- -

{amount}

- -
-

€{(price / 100).toFixed(2)}

- -
-
- {/*
-

€{(price).toFixed(2)}

-
*/} - -
- )} - ; -}; - -export default ShoppingCartItem; \ No newline at end of file diff --git a/lib/hooks/useLocalStorage.tsx b/lib/hooks/useLocalStorage.tsx index 6b080f3..d1d6b43 100644 --- a/lib/hooks/useLocalStorage.tsx +++ b/lib/hooks/useLocalStorage.tsx @@ -35,8 +35,8 @@ export const useLocalStorage = (key: string, initialValue: T) const index = (storedValue as []).findIndex((o: DisplayProduct) => o.name === (value as DisplayProduct).name); if (index !== -1) { - if ((value as DisplayProduct).stock === 0) return undefined; - if ((value as DisplayProduct).amount + (storedValue as DisplayProduct[])[index].amount > (value as DisplayProduct).stock) return undefined; + // if ((value as DisplayProduct).stock === 0) return undefined; + // if ((value as DisplayProduct).amount + (storedValue as DisplayProduct[])[index].amount > (value as DisplayProduct).stock) return undefined; if ((value as DisplayProduct).amount + (storedValue as DisplayProduct[])[index].amount < 0) return undefined; setStoredValue((storedValue as DisplayProduct[]).filter(p => p.name !== (value as DisplayProduct).name) as SetStateAction); const amount = (storedValue as DisplayProduct[])[index].amount + (value as DisplayProduct).amount; diff --git a/pages/api/products/[product_id].ts b/pages/api/products/[product_id].ts index bf391cf..31b0319 100644 --- a/pages/api/products/[product_id].ts +++ b/pages/api/products/[product_id].ts @@ -9,7 +9,7 @@ import dbConnect from '../../../src/util/dbConnect'; import { ironOptions } from '../../../src/util/ironConfig'; import { Stripe } from 'stripe'; -const stripe = new Stripe(process.env.NEXT_STRIPE_SECRET_KEY, { +const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', }); diff --git a/pages/api/products/index.ts b/pages/api/products/index.ts index 541bd42..435297b 100644 --- a/pages/api/products/index.ts +++ b/pages/api/products/index.ts @@ -8,7 +8,7 @@ import dbConnect from '../../../src/util/dbConnect'; import { ironOptions } from '../../../src/util/ironConfig'; import { Stripe } from 'stripe'; -const stripe = new Stripe(process.env.NEXT_STRIPE_SECRET_KEY, { +const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', }); diff --git a/pages/api/stripe/customer.ts b/pages/api/stripe/customer.ts index 10fd096..86fecf9 100644 --- a/pages/api/stripe/customer.ts +++ b/pages/api/stripe/customer.ts @@ -3,7 +3,7 @@ import { NextApiRequest, NextApiResponse } from 'next'; import dbConnect from '../../../src/util/dbConnect'; import { Stripe } from 'stripe'; -const stripe = new Stripe(process.env.NEXT_STRIPE_SECRET_KEY, { +const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', }); @@ -16,8 +16,8 @@ interface Body { name: string; email: string; payment_intent: string; - } - } + }; + }; } export default async function customerHandler( diff --git a/pages/api/stripe/orders.ts b/pages/api/stripe/orders.ts index c0e827b..bb68c66 100644 --- a/pages/api/stripe/orders.ts +++ b/pages/api/stripe/orders.ts @@ -6,14 +6,14 @@ import { Stripe } from 'stripe'; import { withIronSessionApiRoute } from 'iron-session/next'; import { ironOptions } from '../../../src/util/ironConfig'; -const stripe = new Stripe(process.env.NEXT_STRIPE_SECRET_KEY, { +const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', }); dbConnect(); export default withIronSessionApiRoute(async function paymentHandler( - req: Omit & { body: Body }, + req: Omit & { body: Body; }, res: NextApiResponse>, ): Promise { switch (req.method) {