From a0841a2903fd748fa127c7676248419e277676d3 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 17:48:00 -0700 Subject: [PATCH 1/4] Done NEed to run checkers --- src/api/supabase/queries/tests/user_test.ts | 44 ++++---- src/api/supabase/queries/user_queries.ts | 105 +++++++++++++------- src/app/page.tsx | 17 +++- 3 files changed, 106 insertions(+), 60 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 37a19898..3c9e558d 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -4,38 +4,46 @@ 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 result_1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result_1); + removeFromFavorites(testUserId, testItemId); + result_1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result_1); + } catch (error) { - console.error('Test Add User Address Error:', error); + console.error('Error in incrementCartItemByOne:', error); } -} +} \ No newline at end of file diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 22807404..30ae0278 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -20,7 +20,7 @@ export async function fetchUserData(): Promise< PostgrestSingleResponse | { data: never[]; error: PostgrestError } > { try { - const { data: users, error } = await supabase.from('users').select('*'); + const { data: users, error } = await supabase.from('profiles').select('*'); if (error) { console.error('Error fetching data:', error); @@ -39,7 +39,7 @@ export async function fetchUserByUUID( ): Promise> { try { const { data: user, error } = await supabase - .from('Users') + .from('profiles') .select('*') .eq('user_id', uuid) .single(); @@ -55,47 +55,76 @@ export async function fetchUserByUUID( } } -export async function addUserAddress( - uuid: string, - newStreet: string, - newCity: string, - newZipcode: string, -): Promise> { - try { - const { data: existingUser, error: selectError } = await supabase - .from('Users') - .select('street, city, zipcode') - .eq('user_id', uuid) +export async function fetchFavoriteItems(userId: string): Promise> { + // 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]; - - const { data, error } = await supabase - .from('Users') - .update({ - street: updatedStreet, - city: updatedCity, - zipcode: updatedZipcode, - }) - .eq('user_id', uuid) + return data.fav_items; +} + +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}`); + } +} \ No newline at end of file diff --git a/src/app/page.tsx b/src/app/page.tsx index a2b349a5..f3ca9fa6 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,10 +1,12 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ +'use client'; + +import React, { useEffect } from 'react'; + import Link from 'next/link'; import { - testFetchUserData, - testFetchUserByUUID, - testAddUserAddress, + fullFavItemTest } from '../api/supabase/queries/tests/user_test'; import { testFetchOrderByUUID, @@ -24,7 +26,7 @@ import { } from '../api/supabase/queries/tests/pickup_test'; export default function Checkout() { - testFetchUserData(); + // testFetchUserData(); // testFetchUserByUUID(); // testAddUserAddress(); // testFetchOrderByUUID(); @@ -36,8 +38,15 @@ export default function Checkout() { // testFetchPickupData(); // testFetchPickupTimesByUUID(); // testUpdateAllOrdersProgressToTrue(); + useEffect(() => { + async function testEverything() { + await fullFavItemTest(); + } + testEverything(); + }); return ( +
Login
From 2ef3323543ebd80bcf8a6b573b083fbc1ce40e40 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 20:02:22 -0700 Subject: [PATCH 2/4] Done: --- src/api/supabase/queries/tests/user_test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 3c9e558d..83377f0a 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -37,11 +37,11 @@ export async function fullFavItemTest() { const result = await fetchUserByUUID(testUserId); console.log('fetchUserData Result:', result); addToFavorites(testUserId, testItemId); - let result_1 = await fetchFavoriteItems(testUserId); - console.log('fetchFavoriteItems Result:', result_1); + let result1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result1); removeFromFavorites(testUserId, testItemId); - result_1 = await fetchFavoriteItems(testUserId); - console.log('fetchFavoriteItems Result:', result_1); + result1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result1); } catch (error) { console.error('Error in incrementCartItemByOne:', error); From 33e4307b10bb07fa387accc0a3ce508bee2093c5 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 20:03:37 -0700 Subject: [PATCH 3/4] Done: --- src/api/supabase/queries/tests/user_test.ts | 5 +- src/api/supabase/queries/user_queries.ts | 66 ++++++++++++--------- src/app/page.tsx | 5 +- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 83377f0a..07532473 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -28,8 +28,6 @@ export async function runFetchUserByUUID() { } } - - export async function fullFavItemTest() { const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1'; const testItemId = '10'; @@ -42,8 +40,7 @@ export async function fullFavItemTest() { removeFromFavorites(testUserId, testItemId); result1 = await fetchFavoriteItems(testUserId); console.log('fetchFavoriteItems Result:', result1); - } catch (error) { console.error('Error in incrementCartItemByOne:', error); } -} \ No newline at end of file +} diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 30ae0278..3bd6815b 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -55,18 +55,22 @@ export async function fetchUserByUUID( } } -export async function fetchFavoriteItems(userId: string): Promise> { +export async function fetchFavoriteItems( + userId: string, +): Promise> { // Fetch fav_items for the specified user const { data, error } = await supabase - .from('profiles') - .select('fav_items') - .eq('user_id', userId) - .single(); + .from('profiles') + .select('fav_items') + .eq('user_id', userId) + .single(); if (error) { - throw new Error(`An error occurred when trying to fetch favorite items: ${error.message}`); + 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."); + throw new Error('No user found with the specified user_id.'); } return data.fav_items; @@ -75,28 +79,32 @@ export async function fetchFavoriteItems(userId: string): Promise Login From 76ec1e7bc8efbbffb5bec2bc6ca421e98398f61d Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Fri, 3 Nov 2023 14:49:39 -0700 Subject: [PATCH 4/4] Prettier --- src/styles/fonts.tsx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/styles/fonts.tsx b/src/styles/fonts.tsx index 417ebd83..956d3377 100644 --- a/src/styles/fonts.tsx +++ b/src/styles/fonts.tsx @@ -1,17 +1,17 @@ import styled from 'styled-components/native'; export const Heading1 = styled.Text` - font-family: public-sans; - font-size: 40px; - font-style: normal; - font-weight: 700; - line-height: normal; + font-family: public-sans; + font-size: 40px; + font-style: normal; + font-weight: 700; + line-height: normal; `; export const Heading4 = styled.Text` - font-family: public-sans; - font-size: 15px; - font-style: normal; - font-weight: 400; - line-height: normal; -`; \ No newline at end of file + font-family: public-sans; + font-size: 15px; + font-style: normal; + font-weight: 400; + line-height: normal; +`;