-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
299 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
"use client"; | ||
|
||
|
||
import { createContext, ReactNode, useContext, useState } from 'react'; | ||
import ShoppingCart from '../../components/cart/ShoppingCart'; | ||
import useLocalStorage from '../hooks/useLocalStorage'; | ||
import { updateCartForUser } from '../../api/supabase/queries/user_queries'; | ||
|
||
type ShoppingCartProviderProps = { | ||
children: ReactNode | ||
} | ||
|
||
type CartItem = { | ||
id: number | ||
quantity: number | ||
} | ||
|
||
type ShoppingCartContext = { | ||
openCart: () => void | ||
closeCart: () => void | ||
getItemQuantity: (id: number) => number | ||
increaseCartQuantity: (id: number) => void | ||
decreaseCartQuantity: (id: number) => void | ||
removeFromCart: (id: number) => void | ||
cartQuantity: number | ||
cartItems: CartItem[] | ||
} | ||
|
||
const ShoppingCartContext = createContext({} as ShoppingCartContext) | ||
|
||
export function useShoppingCart() { | ||
return useContext(ShoppingCartContext) | ||
} | ||
export function ShoppingCartProvider({ children }: ShoppingCartProviderProps) { | ||
const [isOpen, setIsOpen] = useState(false) | ||
const [cartItems, setCartItems] = useLocalStorage<CartItem[]>( | ||
"shopping-cart", | ||
[] | ||
) | ||
|
||
const cartQuantity = cartItems.reduce( | ||
(quantity, item) => item.quantity + quantity, | ||
0 | ||
) | ||
|
||
const openCart = () => setIsOpen(true) | ||
const closeCart = () => setIsOpen(false) | ||
function getItemQuantity(id: number) { | ||
return cartItems.find(item => item.id === id)?.quantity || 0 | ||
} | ||
function increaseCartQuantity(id: number) { | ||
setCartItems(currItems => { | ||
if (currItems.find(item => item.id === id) == null) { | ||
return [...currItems, { id, quantity: 1 }] | ||
} else { | ||
return currItems.map(item => { | ||
if (item.id === id) { | ||
return { ...item, quantity: item.quantity + 1 } | ||
} else { | ||
return item | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
function decreaseCartQuantity(id: number) { | ||
setCartItems(currItems => { | ||
if (currItems.find(item => item.id === id)?.quantity === 1) { | ||
return currItems.filter(item => item.id !== id) | ||
} else { | ||
return currItems.map(item => { | ||
if (item.id === id) { | ||
return { ...item, quantity: item.quantity - 1 } | ||
} else { | ||
return item | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
function removeFromCart(id: number) { | ||
setCartItems(currItems => { | ||
return currItems.filter(item => item.id !== id) | ||
}) | ||
} | ||
|
||
return ( | ||
<ShoppingCartContext.Provider | ||
value={{ | ||
getItemQuantity, | ||
increaseCartQuantity, | ||
decreaseCartQuantity, | ||
removeFromCart, | ||
openCart, | ||
closeCart, | ||
cartItems, | ||
cartQuantity, | ||
}} | ||
> | ||
{children} | ||
<ShoppingCart isOpen={isOpen} /> | ||
</ShoppingCartContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// import { createContext, ReactNode, useContext, useState, useMemo, useReducer} from 'react'; | ||
// import ShoppingCart from '../../components/cart/ShoppingCart'; | ||
// import useLocalStorage from '../hooks/useLocalStorage'; | ||
// import { updateCartForUser } from '../../api/supabase/queries/user_queries'; | ||
|
||
// type ShoppingCartProviderProps = { | ||
// children: ReactNode; | ||
// }; | ||
|
||
// type CartItem = { | ||
// id: number; | ||
// quantity: number; | ||
// }; | ||
|
||
// export interface cartState { | ||
// items: CartItem[]; | ||
// } | ||
|
||
// export type AuthContextAction = | ||
// | { type: 'ADD_ITEM'; userObject: RegularUser | null } | ||
// | { type: 'REMOVE_ITEM'; userObject: RegularUser | null } | ||
// | { type: 'REMOVE_ITEM' } | ||
// | { type: 'CHANGE_BOOKMARK'; bookmarkedArray: string[] | undefined } | ||
// | { type: 'UPDATE_LANGUAGE'; language: string | undefined }; | ||
|
||
// // TODO: get this from the user | ||
// const userID = '1'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,37 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { useRouter } from 'next/router' | ||
import { Container } from "react-bootstrap" | ||
import { Home } from "./pages/Home" | ||
import Store from "./pages/Store" | ||
import { About } from "./pages/About" | ||
import Navbar from "../components/cart/Navbar" | ||
import { ShoppingCartProvider } from "./context/ShoppingCartContext copy" | ||
|
||
import Link from 'next/link'; | ||
import { | ||
testFetchUserData, | ||
testFetchUserByUUID, | ||
testAddUserAddress, | ||
} from '../api/supabase/queries/tests/user_test'; | ||
import { | ||
testFetchOrderByUUID, | ||
testFetchOrders, | ||
testGetOrderById, | ||
testToggleOrderProgress, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
testUpdateAllOrdersProgressToTrue, | ||
} from '../api/supabase/queries/tests/order_test'; | ||
import { | ||
testFetchProducts, | ||
testFetchProductByName, | ||
} from '../api/supabase/queries/tests/product_test'; | ||
import { | ||
testFetchPickupData, | ||
testFetchPickupTimesByUUID, | ||
} from '../api/supabase/queries/tests/pickup_test'; | ||
function App() { | ||
const router = useRouter(); | ||
|
||
export default function Checkout() { | ||
// testFetchUserData(); | ||
// testFetchUserByUUID(); | ||
// testAddUserAddress(); | ||
// testFetchOrderByUUID(); | ||
// testFetchOrders(); | ||
// testGetOrderById(); | ||
// testToggleOrderProgress(); | ||
testFetchProducts(); | ||
// testFetchProductByName(); | ||
// testFetchPickupData(); | ||
// testFetchPickupTimesByUUID(); | ||
// testUpdateAllOrdersProgressToTrue(); | ||
let Component; | ||
switch (router.pathname) { | ||
case '/': | ||
Component = Home; | ||
break; | ||
case '/store': | ||
Component = Store; | ||
break; | ||
case '/about': | ||
Component = About; | ||
break; | ||
default: | ||
Component = Home; | ||
} | ||
|
||
return ( | ||
<main> | ||
<Link href="/store">Store</Link> | ||
</main> | ||
); | ||
<ShoppingCartProvider> | ||
<Navbar /> | ||
<Container className="mb-4"> | ||
<Component /> | ||
</Container> | ||
</ShoppingCartProvider> | ||
) | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function About() { | ||
return <h1>About</h1> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function Home() { | ||
return <h1>Home</h1> | ||
} |
Oops, something went wrong.