Skip to content

Commit

Permalink
Merge pull request #31 from calblueprint/buyankhuu/orders
Browse files Browse the repository at this point in the history
Buyankhuu/orders
  • Loading branch information
EthanAuyeung authored Nov 16, 2023
2 parents 79637ee + 9525e6e commit 030b1f2
Show file tree
Hide file tree
Showing 27 changed files with 1,134 additions and 163 deletions.
78 changes: 75 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"next": "^13.5.4",
"react": "^18.2.0",
"react-dom": "18.2.0",
"react-feather": "^2.0.10",
"react-router-dom": "^6.17.0",
"react-toastify": "^9.1.3",
"styled-components": "^6.0.8"
},
"devDependencies": {
Expand Down
20 changes: 1 addition & 19 deletions src/api/supabase/queries/tests/user_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* eslint-disable no-console */
//

import {
fetchUserData,
fetchUserByUUID,
fetchFavoriteItems,
} from '../user_queries';
import { fetchUserData, fetchUserByUUID } from '../user_queries';

export async function runFetchUserData() {
try {
Expand All @@ -25,17 +21,3 @@ export async function runFetchUserByUUID() {
console.error('Error in fetchUserByUUID:', error);
}
}

export async function fullFavItemTest() {
const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1';
try {
const result = await fetchUserByUUID(testUserId);
console.log('fetchUserData Result:', result);
let result1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result1);
result1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result1);
} catch (error) {
console.error('Error in incrementCartItemByOne:', error);
}
}
118 changes: 100 additions & 18 deletions src/api/supabase/queries/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PostgrestError,
createClient,
} from '@supabase/supabase-js';
import { User } from '../../../schema/schema';
import { User, Product } from '../../../schema/schema';

// Replace these with your Supabase project URL and API key
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
Expand Down Expand Up @@ -53,25 +53,107 @@ export async function fetchUserByUUID(uuid: string) {
}
}

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 (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.');
export async function getUserInfo(product: Product, isFav: boolean) {
const {
data: { user },
} = await supabase.auth.getUser();

if (user !== null) {
const { data } = await supabase
.from('profiles')
.select()
.eq('user_id', user.id);

if (data !== null) {
const CurrUserFavoriteItems = data[0].fav_items;

if (isFav) {
CurrUserFavoriteItems[product.product_id] = 1;
} else {
delete CurrUserFavoriteItems[product.product_id];
}

const { error } = await supabase
.from('profiles')
.update({ fav_items: CurrUserFavoriteItems })
.eq('user_id', user.id);
}
}
}

export async function arrayOfFavorites() {
const {
data: { user },
} = await supabase.auth.getUser();

if (user !== null) {
const { data: userProfileData, error: userProfileError } = await supabase
.from('profiles')
.select()
.eq('user_id', user.id)
.single();

const CurrUserFavoriteItems = userProfileData.fav_items;

const Favkey = Object.keys(CurrUserFavoriteItems);

const { data: productData, error: productError } = await supabase
.from('product')
.select('*');

if (productData !== null && productData !== undefined) {
const FavArray = productData.filter(product =>
Favkey.includes(String(product.product_id)),
);

return FavArray;
}
}
return [];
}

export async function getProduct() {
const { data } = await supabase.from('product').select('*');
return data;
}

return data.fav_items;
export async function filterProduct(productType: string) {
const { data } = await supabase
.from('product')
.select('*')
.eq('category', productType);
return data;
}

export async function totalNumberOfItemsInCart() {
const {
data: { user },
} = await supabase.auth.getUser();

if (user !== null) {
const { data, error } = await supabase
.from('profiles')
.select()
.eq('user_id', user.id)
.single();

if (data !== null) {
const CurrUserCart = data.cart;

if (CurrUserCart === null || CurrUserCart === undefined) {
return 0;
}

const itemNumb = Object.values(CurrUserCart) as number[];

let sum = 0;

for (let i = 0; i < itemNumb.length; i += 1) {
sum += itemNumb[i];
}

return sum;
}
}
return 0;
}
6 changes: 5 additions & 1 deletion src/app/[productId]/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import { toast } from 'react-toastify';
import {
ButtonsWrapper,
AddToCartButton,
Expand All @@ -20,6 +21,8 @@ export default function Buttons() {
};

// used hyphen instead of dash for display
const notify = () => toast(`you have added ${quantity} items to the cart!`);

return (
<ButtonsWrapper>
<QuantityButton>
Expand All @@ -31,7 +34,8 @@ export default function Buttons() {
+
</PlusMinusButton>
</QuantityButton>
<AddToCartButton>Add to cart</AddToCartButton>

<AddToCartButton onClick={notify}>Add to cart</AddToCartButton>
</ButtonsWrapper>
);
}
10 changes: 10 additions & 0 deletions src/app/[productId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import Link from 'next/link';
import Image from 'next/image';
import { useEffect, useState } from 'react';
import { fetchProductByID } from '../../api/supabase/queries/product_queries';
import 'react-toastify/dist/ReactToastify.css';

import {
BackButton,
ImageContainer,
TextContainer,
DescriptionContainer,
ToastPopUP,
} from './styles';
import { GlobalStyle } from '../../styles/components';
import NavBar from '../../components/NavBar';
Expand Down Expand Up @@ -40,7 +43,14 @@ export default function ItemDisplay({
return (
<main>
<GlobalStyle />

<NavBar />
<ToastPopUP
position="top-right"
autoClose={500}
limit={1}
hideProgressBar
/>
<BackButton>
<Link href="/storefront">
<Image
Expand Down
8 changes: 8 additions & 0 deletions src/app/[productId]/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from 'styled-components';
import { ToastContainer } from 'react-toastify';
import COLORS from '../../styles/colors';

export const BackButton = styled.button`
Expand Down Expand Up @@ -76,3 +77,10 @@ export const AddToCartButton = styled.button`
color: ${COLORS.white};
border-color: transparent;
`;

export const ToastPopUP = styled(ToastContainer)`
position: fixed;
z-index: 100;
transform: translatey(130px);
`;
33 changes: 33 additions & 0 deletions src/app/cart/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useState } from 'react';

import { ButtonsWrapper, 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>
</ButtonsWrapper>
);
}
Loading

1 comment on commit 030b1f2

@vercel
Copy link

@vercel vercel bot commented on 030b1f2 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.