Skip to content

Commit

Permalink
WIP Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjcai committed Oct 21, 2023
1 parent eebad1e commit f12286d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/app/context/ShoppingCartContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createContext, ReactNode, useContext, useState } from "react"
import ShoppingCart from "../../components/cart/ShoppingCart"
import useLocalStorage from "../hooks/useLocalStorage"

import {updateCartForUser} from "../../supabase/user_queries"
type ShoppingCartProviderProps = {
children: ReactNode
}

const userID = 1

type CartItem = {
id: number
quantity: number
Expand Down Expand Up @@ -58,6 +60,8 @@ export function ShoppingCartProvider({ children }: ShoppingCartProviderProps) {
})

})

updateCartForUser(userID, cartItems)
}
function decreaseCartQuantity(id: number) {
setCartItems(currItems => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../supabase/tests/pickup_test';

export default function Checkout() {
testFetchUserData();
// testFetchUserData();
// testFetchUserByUUID();
// testAddUserAddress();
// testFetchOrderByUUID();
Expand All @@ -39,7 +39,7 @@ export default function Checkout() {

return (
<main>
<Link href="/login">Login</Link>
<Link href="/pages">Store</Link>
</main>
);
}
3 changes: 3 additions & 0 deletions src/app/pages/About.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function About() {
return <h1>About</h1>
}
3 changes: 3 additions & 0 deletions src/app/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Home() {
return <h1>Home</h1>
}
7 changes: 4 additions & 3 deletions src/app/Store.tsx → src/app/pages/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Col, Row } from "react-bootstrap"
import { useEffect, useState } from "react"
import { StoreItem } from "../components/cart/StoreItem"
import {fetchProducts} from "../supabase/product_queries"
import {Product} from "../schema/schema"
import { StoreItem } from "../../components/cart/StoreItem"
import {fetchProducts} from "../../supabase/product_queries"
import {Product} from "../../schema/schema"

// eslint-disable-next-line import/prefer-default-export
export function Store() {

Expand Down
48 changes: 48 additions & 0 deletions src/supabase/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,51 @@ export async function addUserAddress(
throw error;
}
}


export async function updateCartForUser(userId: string, newCartData: Record<string, number>): Promise<PostgrestSingleResponse<User[]>> {
try {
const { data: users, error } = await supabase
.from<User>('users') // Specify the User type for type safety
.upsert([
{
userId,
cart_items: newCartData, // Update the 'cart_items' field with new cart data
}
]);

if (error) {
console.error('Error updating cart for user:', error);
throw error;
}

return { data: users } as PostgrestSingleResponse<User[]>;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function getCartForUser(userId: string): Promise<PostgrestSingleResponse<Record<string, number>>> {
try {
const { data: user, error } = await supabase
.from('users')
.select('cart_items') // Select only the 'cart_items' field
.eq('user_id', userId)
.single();

if (error) {
console.error('Error fetching cart for user:', error);
throw error;
}

if (user) {
return { data: user.cart_items } as PostgrestSingleResponse<Record<string, number>>;
}
throw new Error('User not found');

} catch (error) {
console.error('Error:', error);
throw error;
}
}

0 comments on commit f12286d

Please sign in to comment.