Skip to content

Commit

Permalink
committing before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
celinechoiii committed Nov 6, 2023
2 parents 37bcfee + 23cf026 commit 5e09445
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 198 deletions.
Binary file added public/images/Arrow_Left_MD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions src/api/supabase/queries/order_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function fetchOrders(): Promise<
> {
try {
const { data: orders, error } = await supabase
.from('Order') // Update to the "Order" table
.from('order') // Update to the "Order" table
.select('*');

if (error) {
Expand All @@ -40,7 +40,7 @@ export async function fetchOrderByUUID(
): Promise<PostgrestSingleResponse<Order>> {
try {
const { data: order, error } = await supabase
.from('Order') // Update to the "Order" table
.from('order') // Update to the "Order" table
.select('*')
.eq('id', uuid)
.single();
Expand All @@ -63,7 +63,7 @@ export async function getOrdersByUserId(
> {
try {
const { data: orders, error } = await supabase
.from('Order')
.from('order')
.select('*')
.eq('user_id', userId)
.single();
Expand All @@ -86,7 +86,7 @@ export async function getOrderById(
): Promise<PostgrestSingleResponse<Order>> {
try {
const { data: order, error } = await supabase
.from('Order')
.from('order')
.select('*')
.eq('id', orderId)
.single();
Expand All @@ -108,7 +108,7 @@ export async function toggleOrderProgress(
try {
// Fetch the order by ID to get its current "approved" value
const { data: currentOrder, error: fetchError } = await supabase
.from('Order')
.from('order')
.select('approved')
.eq('id', orderId)
.single();
Expand All @@ -123,7 +123,7 @@ export async function toggleOrderProgress(

// Update the order with the new "approved" value
const { data: updatedOrder, error: updateError } = await supabase
.from('Order')
.from('order')
.update({ approved: updatedApprovedValue })
.eq('id', orderId)
.single();
Expand All @@ -146,7 +146,7 @@ export async function updateAllOrdersProgressToTrue(): Promise<
try {
// Update all orders to set "approved" to true
const { error: updateError } = await supabase
.from('Order')
.from('order')
.update({ approved: true });

if (updateError) {
Expand Down
8 changes: 3 additions & 5 deletions src/api/supabase/queries/product_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function fetchProducts(): Promise<
> {
try {
const { data: products, error } = await supabase
.from('Product')
.from('product')
.select('*');

if (error) {
Expand All @@ -35,12 +35,10 @@ export async function fetchProducts(): Promise<
}
}

export async function fetchProductByID(
productId: string,
): Promise<PostgrestSingleResponse<Product>> {
export async function fetchProductByID(productId: number) {
try {
const { data: product, error } = await supabase
.from('Product')
.from('product')
.select('*')
.eq('product_id', productId)
.single();
Expand Down
2 changes: 1 addition & 1 deletion src/api/supabase/queries/tests/product_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function testFetchProducts() {

// Test fetching a product by name
export async function testFetchProductByName() {
const productId = '1'; // Replace with a valid product name
const productId = 1; // Replace with a valid product name
try {
const result = await fetchProductByID(productId);
console.log('Fetch Product by Name Result:', result);
Expand Down
41 changes: 23 additions & 18 deletions src/api/supabase/queries/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@
import {
fetchUserData,
fetchUserByUUID,
addUserAddress,
fetchFavoriteItems,
addToFavorites,
removeFromFavorites,
} from '../user_queries';

export async function testFetchUserData() {
export async function runFetchUserData() {
try {
const result = await fetchUserData();
console.log('Fetch Data Result:', result);
console.log('fetchUserData Result:', result);
} catch (error) {
console.error('Test Fetch Data Error:', error);
console.error('Error in fetchUserData:', error);
}
}

export async function testFetchUserByUUID() {
const uuid = '3b4a1317-b9ea-4cbd-95d7-e959aa80d1ea'; // Replace with a valid user ID
export async function runFetchUserByUUID() {
const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea';
try {
const result = await fetchUserByUUID(uuid);
console.log('Fetch User by UUID Result:', result);
const result = await fetchUserByUUID(testUUID);
console.log('fetchUserByUUID Result:', result);
} catch (error) {
console.error('Test Fetch User by UUID Error:', error);
console.error('Error in fetchUserByUUID:', error);
}
}

export async function testAddUserAddress() {
const uuid = '3b4a1317-b9ea-4cbd-95d7-e959aa80d1ea'; // Replace with a valid user ID
const newStreet = '123 New Street';
const newCity = 'New City';
const newZipcode = '12345';

export async function fullFavItemTest() {
const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1';
const testItemId = '10';
try {
const result = await addUserAddress(uuid, newStreet, newCity, newZipcode);
console.log('Add User Address Result:', result);
const result = await fetchUserByUUID(testUserId);
console.log('fetchUserData Result:', result);
addToFavorites(testUserId, testItemId);
let result1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result1);
removeFromFavorites(testUserId, testItemId);
result1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result1);
} catch (error) {
console.error('Test Add User Address Error:', error);
console.error('Error in incrementCartItemByOne:', error);
}
}
120 changes: 79 additions & 41 deletions src/api/supabase/queries/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ export async function fetchUserData(): Promise<
PostgrestSingleResponse<User[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: users, error } = await supabase
.from('profiles')
.select('*')
.single();
const { data: users, error } = await supabase.from('profiles').select('*');

if (error) {
console.error('Error fetching data:', error);
Expand Down Expand Up @@ -56,47 +53,88 @@ export async function fetchUserByUUID(uuid: string) {
}
}

export async function addUserAddress(
uuid: string,
newStreet: string,
newCity: string,
newZipcode: string,
): Promise<PostgrestSingleResponse<unknown>> {
try {
const { data: existingUser, error: selectError } = await supabase
.from('profiles')
.select('street, city, zipcode')
.eq('user_id', uuid)
.single();
export async function fetchFavoriteItems(
userId: string,
): Promise<Record<string, number>> {
// Fetch fav_items for the specified user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (selectError) {
console.error('Error selecting user data:', selectError);
throw selectError;
}
if (error) {
throw new Error(
`An error occurred when trying to fetch favorite items: ${error.message}`,
);
} else if (!data) {
throw new Error('No user found with the specified user_id.');
}

// Append new values to the arrays
const updatedStreet = [...(existingUser?.street || []), newStreet];
const updatedCity = [...(existingUser?.city || []), newCity];
const updatedZipcode = [...(existingUser?.zipcode || []), newZipcode];
return data.fav_items;
}

const { data, error } = await supabase
.from('profiles')
.update({
street: updatedStreet,
city: updatedCity,
zipcode: updatedZipcode,
})
.eq('user_id', uuid)
.single();
export async function addToFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
console.error('Error updating user data:', error);
throw error;
}
if (error) {
throw new Error(
`An error occurred when trying to fetch the user's favorite items: ${error.message}`,
);
}

return { data, error: null, status: 200, statusText: 'OK', count: 1 };
} catch (error) {
console.error('Error:', error);
throw error;
const currentFavItems = data?.fav_items || {};

// Add the product to fav_items or update its quantity
currentFavItems[productId] = currentFavItems[productId] || 0;

// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(
`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`,
);
}
}

// Function to remove a product from fav_items
export async function removeFromFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
throw new Error(
`An error occurred when trying to fetch the user's favorite items: ${error.message}`,
);
}

const currentFavItems = data?.fav_items || {};

// Remove the product from fav_items
delete currentFavItems[productId];
console.log(currentFavItems);
// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(
`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`,
);
}
}
37 changes: 37 additions & 0 deletions src/app/[productId]/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { useState } from 'react';
import {
ButtonsWrapper,
AddToCartButton,
QuantityButton,
PlusMinusButton,
} from './styles';

export default function Buttons() {
const [quantity, setQuantity] = useState<number>(1);

const increaseQuantity = () => {
setQuantity(quantity + 1);
};

const decreaseQuantity = () => {
if (quantity > 1) {
setQuantity(quantity - 1);
}
};

// used hyphen instead of dash for display
return (
<ButtonsWrapper>
<QuantityButton>
<PlusMinusButton type="button" onClick={decreaseQuantity}>
</PlusMinusButton>
<span>{quantity}</span>
<PlusMinusButton type="button" onClick={increaseQuantity}>
+
</PlusMinusButton>
</QuantityButton>
<AddToCartButton>Add to cart</AddToCartButton>
</ButtonsWrapper>
);
}
Loading

0 comments on commit 5e09445

Please sign in to comment.