-
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
5 changed files
with
124 additions
and
149 deletions.
There are no files selected for viewing
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,114 @@ | ||
import supabase from '../createClient'; | ||
|
||
import { fetchUser } from './user_queries'; | ||
|
||
// define cart item type | ||
export type CartItem = { | ||
id: number; | ||
product_id: number; | ||
quantity: number; | ||
}; | ||
|
||
export async function fetchCartItem(cartItemID: number): Promise<CartItem> { | ||
const { data, error } = await supabase | ||
.from('cart_items') | ||
.select('*') | ||
.match({ id: cartItemID }) | ||
.single(); | ||
if (error) { | ||
throw new Error(`Error fetching cart item: ${error.message}`); | ||
} | ||
return data; | ||
} | ||
|
||
export async function fetchCart(): Promise<CartItem[]> { | ||
const user = await fetchUser(); | ||
const cartID = user.cart_id; | ||
const { data, error } = await supabase | ||
.from('order') | ||
.select('*') | ||
.match({ id: cartID }) | ||
.single(); | ||
if (error) { | ||
throw new Error(`Error fetching cart: ${error.message}`); | ||
} | ||
const products = data.product_id_array; | ||
const productPromises = products.map(async (productID: number) => { | ||
const product = await fetchCartItem(productID); | ||
return product; | ||
}); | ||
const fetchedProducts = await Promise.all(productPromises); | ||
|
||
return fetchedProducts; | ||
} | ||
|
||
export async function updateCart(cartID: number, productIDArray: number[]) { | ||
await supabase | ||
.from('order') | ||
.update({ product_id_array: productIDArray }) | ||
.match({ id: cartID }); | ||
} | ||
|
||
export async function addToCart(productID: number, quantity: number) { | ||
const items = await fetchCart(); | ||
|
||
// check if product is already in cart | ||
const existingItem = items.find(item => item.product_id === productID); | ||
if (existingItem) { | ||
const newQuantity = existingItem.quantity + quantity; | ||
await supabase | ||
.from('cart_items') | ||
.update({ quantity: newQuantity }) | ||
.match({ id: existingItem.id }); | ||
} else { | ||
const { data, error } = await supabase | ||
.from('cart_items') | ||
.insert([{ product_id: productID, quantity }]) | ||
.select('*') | ||
.single(); | ||
if (error) { | ||
throw new Error(`Error adding to cart: ${error.message}`); | ||
} | ||
// append to existing cart | ||
const user = await fetchUser(); | ||
const cartID = user.cart_id; | ||
const productIdArray = items.map(item => item.id); | ||
productIdArray.push(data.id); | ||
updateCart(cartID, productIdArray); | ||
} | ||
} | ||
|
||
export async function removeFromCart(productID: number, quantity: number) { | ||
const items = await fetchCart(); | ||
const existingItem = items.find(item => item.product_id === productID); | ||
if (existingItem) { | ||
const newQuantity = existingItem.quantity - quantity; | ||
if (newQuantity <= 0) { | ||
await supabase | ||
.from('cart_items') | ||
.delete() | ||
.match({ id: existingItem.id }) | ||
.select('*') | ||
.single(); | ||
|
||
// remove from existing cart | ||
const user = await fetchUser(); | ||
const cartID = user.cart_id; | ||
const productIdArray = items.map(item => item.id); | ||
const index = productIdArray.indexOf(existingItem.id); | ||
productIdArray.splice(index, 1); | ||
updateCart(cartID, productIdArray); | ||
} else { | ||
await supabase | ||
.from('cart_items') | ||
.update({ quantity: newQuantity }) | ||
.match({ id: existingItem.id }); | ||
} | ||
} | ||
} | ||
|
||
export async function clearCart() { | ||
const user = await fetchUser(); | ||
const cartID = user.cart_id; | ||
updateCart(cartID, []); | ||
} |
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