Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buyankhuu/fixed queries+changed store front #34

Merged
merged 8 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/api/supabase/queries/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function fetchUserByUUID(uuid: string) {
const { data: user, error } = await supabase
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi Kevin, thanks for the comments! Super helpful. I don't think I wrote this function so I might pass it to you but I have resolved the rest of the comments

.from('profiles')
.select('*')
.eq('user_id', uuid)
.eq('id', uuid)
.single();

if (error) {
Expand All @@ -62,21 +62,22 @@ export async function getUserInfo(product: Product, isFav: boolean) {
const { data } = await supabase
BuyankhuuTsCAl marked this conversation as resolved.
Show resolved Hide resolved
.from('profiles')
.select()
.eq('user_id', user.id);
.eq('id', user.id)
.single();

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

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

const { error } = await supabase
.from('profiles')
.update({ fav_items: CurrUserFavoriteItems })
.eq('user_id', user.id);
.eq('id', user.id);
}
}
}
Expand All @@ -90,23 +91,25 @@ export async function arrayOfFavorites() {
const { data: userProfileData, error: userProfileError } = await supabase
BuyankhuuTsCAl marked this conversation as resolved.
Show resolved Hide resolved
.from('profiles')
.select()
.eq('user_id', user.id)
.eq('id', user.id)
.single();

const CurrUserFavoriteItems = userProfileData.fav_items;
if (userProfileData !== null) {
const CurrUserFavoriteItems = userProfileData.fav_items;

const Favkey = Object.keys(CurrUserFavoriteItems);
const Favkey = Object.keys(CurrUserFavoriteItems);

const { data: productData, error: productError } = await supabase
.from('product')
.select('*');
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)),
);
if (productData !== null && productData !== undefined) {
const FavArray = productData.filter(product =>
Favkey.includes(String(product.id)),
);

return FavArray;
return FavArray;
}
}
}
return [];
Expand Down Expand Up @@ -134,7 +137,7 @@ export async function totalNumberOfItemsInCart() {
const { data, error } = await supabase
BuyankhuuTsCAl marked this conversation as resolved.
Show resolved Hide resolved
.from('profiles')
.select()
.eq('user_id', user.id)
.eq('id', user.id)
.single();

if (data !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/[productId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function ItemDisplay({
{Item?.category}
</h4>
<Buttons />
<p style={{ paddingTop: '50px' }}>Product ID: {Item?.product_id}</p>
<p style={{ paddingTop: '50px' }}>Product ID: {Item?.id}</p>
<p style={{ paddingTop: '20px' }}>Product Details:</p>
<p>{Item?.description}</p>
</TextContainer>
Expand Down
16 changes: 4 additions & 12 deletions src/app/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ import {

import Buttons from './Buttons';

interface Product {
description: string;
category: string;
quantity: number;
photo: string;
product_id: number;
name: string;
updated_at: Date;
}
import { Product } from '../../schema/schema';

export default function OrderPage() {
const [Cart, setCart] = useState<Product[]>([]);
Expand All @@ -61,7 +53,7 @@ export default function OrderPage() {
async function clickFunctions(props: { fav: Product }) {
const { fav } = props;
getUserInfo(fav, false);
setCart(Cart.filter(Prod => Prod.product_id !== fav.product_id));
setCart(Cart.filter(Prod => Prod.id !== fav.id));
}

return (
Expand All @@ -77,7 +69,7 @@ export default function OrderPage() {
<h1>Cart</h1>
<OutterFavoriteDiv>
{Cart.map(cart => (
<FavoriteDiv key={cart.product_id}>
<FavoriteDiv key={cart.id}>
<img
src={cart.photo}
alt={cart.name}
Expand All @@ -103,7 +95,7 @@ export default function OrderPage() {
<Qty>Qty.</Qty>
<OrderSummaryDiv>
{Cart.map(cart => (
<ItemSummaryDiv key={cart.product_id}>
<ItemSummaryDiv key={cart.id}>
<PShiftLeft>{cart.name}</PShiftLeft>
<PShiftRight>{cart.quantity}</PShiftRight>
</ItemSummaryDiv>
Expand Down
14 changes: 3 additions & 11 deletions src/app/favorites/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ import {
NavBarMovedUP,
} from '../profileScreen/styles';

interface Product {
description: string;
category: string;
quantity: number;
photo: string;
product_id: number;
name: string;
updated_at: Date;
}
import { Product } from '../../schema/schema';

export default function FavoritesPage() {
const [Favorites, setFavorites] = useState<Product[]>([]);
Expand All @@ -45,7 +37,7 @@ export default function FavoritesPage() {
async function clickFunctions(props: { fav: Product }) {
const { fav } = props;
getUserInfo(fav, false);
setFavorites(Favorites.filter(Prod => Prod.product_id !== fav.product_id));
setFavorites(Favorites.filter(Prod => Prod.id !== fav.id));
}

return (
Expand All @@ -60,7 +52,7 @@ export default function FavoritesPage() {
<h1>Favorites</h1>
<OutterFavoriteDiv>
{Favorites.map(favorite => (
<FavoriteDiv key={favorite.product_id}>
<FavoriteDiv key={favorite.id}>
<img
src={favorite.photo}
alt={favorite.name}
Expand Down
4 changes: 2 additions & 2 deletions src/app/profileScreen/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
LogOutButton,
GlobalStyle,
PopUp,
NavBarZeroIndex,
NavBarMovedUP,
FooterMoved,
} from './styles';

Expand All @@ -31,7 +31,7 @@ export default function Profile() {

return (
<main>
<NavBarZeroIndex />
<NavBarMovedUP />
<GlobalStyle />
<LogOutButton onClick={() => showToastMessage()}>Log Out!</LogOutButton>
<PopUp closeButton={false} autoClose={3000} hideProgressBar limit={1} />
Expand Down
5 changes: 2 additions & 3 deletions src/app/profileScreen/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const LogOutButton = styled.button`
width: 300px;
height: 50px;
z-index: 1000;
transform: translateY(200px);
`;
/* transform: translateY(200px); */

Expand All @@ -58,7 +57,7 @@ export const OutterFavoriteDiv = styled.div`
background: var(--White, #fff);
box-shadow: 0px 1px 4px 1px rgba(0, 0, 0, 0.2);
width: 800px;
height: 350px;
height: 500px;
overflow: scroll;
margin-top: 10px;
`;
Expand Down Expand Up @@ -97,7 +96,7 @@ export const NavBarZeroIndex = styled(NavBar)`
`;

export const FooterMoved = styled(Footer)`
transform: translateY(300px);
transform: translateY(50px);
`;

export const TransparentButton = styled.button`
Expand Down
6 changes: 3 additions & 3 deletions src/app/storefront/IndividualItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function IndividualItem(props: {

useEffect(() => {
async function fetchProducts() {
if (products.find(item => item.product_id === product.product_id)) {
if (products.find(item => item.id === product.id)) {
setIsFavorite(false);
}
}
Expand All @@ -36,8 +36,8 @@ export default function IndividualItem(props: {
}
return (
<div>
<StorefrontItem key={product.product_id}>
<ItemButtons onClick={() => router.push(`/${product.product_id}`)}>
<StorefrontItem key={product.id}>
<ItemButtons onClick={() => router.push(`/${product.id}`)}>
<img
src={product.photo}
alt={product.name}
Expand Down
17 changes: 15 additions & 2 deletions src/app/storefront/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export default function App() {
];
const [FilteredProducts, setFilteredProducts] = useState<Product[]>([]);

const [CategoryWord, setCategoryWord] = useState('All');

const [IsClickedButton, setIsClickedButton] = useState<boolean[]>([
true,
false,
false,
false,
]);

useEffect(() => {
async function fetchProducts() {
try {
Expand All @@ -57,16 +66,20 @@ export default function App() {
<GlobalStyle />
<NavBarZeroIndex />
<ButtonsContainer>
{buttons.map(type => (
{buttons.map((type, index) => (
<ProductButtons
key={type.count}
value={type.value}
setFiltredProducts={setFilteredProducts}
content={type.name}
setIsClickedButton={setIsClickedButton}
IsClickedButton={IsClickedButton}
setCategoryWord={setCategoryWord}
index={index}
/>
))}
</ButtonsContainer>
<ShopAllText>Shop All</ShopAllText>
<ShopAllText>Shop {CategoryWord}</ShopAllText>
<Storefront products={FilteredProducts} />
<Footer />
</main>
Expand Down
70 changes: 65 additions & 5 deletions src/app/storefront/productButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,75 @@ export default function ProductButtons(props: {
value: string;
setFiltredProducts: (category: Product[]) => void;
content: string;
setIsClickedButton: (clicked: boolean[]) => void;
IsClickedButton: boolean[];
setCategoryWord: (word: string) => void;
index: number;
}) {
const { key, value, content, setFiltredProducts } = props;
const [IsClicked, setIsClicked] = useState(false);
const {
key,
value,
content,
setFiltredProducts,
setIsClickedButton,
IsClickedButton,
setCategoryWord,
index,
} = props;
const buttons = [
{
name: 'All',
value: 'All',
count: 0,
},
{
name: 'Dog',
value: 'Dog',
count: 1,
},
{
name: 'Cat',
value: 'Cat',
count: 2,
},
{
name: 'Other',
value: 'Other',
count: 3,
},
];

async function applyFilter(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>,
) {
setIsClicked(!IsClicked);
// which button is clicked

const category = e.currentTarget.value;

for (let i = 0; i < buttons.length; i += 1) {
if (buttons[i].value === category) {
const ind = buttons[i].count;
if (IsClickedButton[ind] === true) {
const tempArray = [...IsClickedButton];
tempArray[ind] = !tempArray[ind];
tempArray[0] = true;
setCategoryWord('All');
setIsClickedButton(tempArray);
} else {
const arrayOfFalse = [false, false, false, false];
arrayOfFalse[ind] = true;
setCategoryWord(buttons[i].value);
setIsClickedButton(arrayOfFalse);
}

break;
}
}

// Changing the Color of the Button

// Applying the filter to the categories of the product

if (category !== 'All') {
const products = await filterProduct(category);
if (products !== null) {
Expand All @@ -41,14 +101,14 @@ export default function ProductButtons(props: {
return (
<IndividualContainer>
<Button
isClicked={IsClicked}
$pickColor={IsClickedButton[index]}
key={key}
value={value}
onClick={(e: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>
applyFilter(e)
}
/>
<Label isClicked={IsClicked}>{content}</Label>
<Label $pickColor={IsClickedButton[index]}>{content}</Label>
</IndividualContainer>
);
}
2 changes: 1 addition & 1 deletion src/app/storefront/storefrontItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Storefront({ products }: { products: Product[] }) {
<IndividualItem
products={Favorites}
product={productVal}
key={productVal.product_id}
key={productVal.id}
/>
))}
</StorefrontWrapper>
Expand Down
8 changes: 4 additions & 4 deletions src/app/storefront/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export const StickyHeader = styled.div`
z-index: 2;
`;

export const Button = styled.button<props>`
background-color: ${props => (props.isClicked ? '#00507f' : '#C7E1FF')};
export const Button = styled.button<{ $pickColor?: boolean }>`
background-color: ${props => (props.$pickColor ? '#00507f' : '#C7E1FF')};
border-radius: 50%;
width: 50px;
height: 50px;
border: transparent;
`;

export const Label = styled.p<props>`
color: ${props => (props.isClicked ? '#00507f' : '#000')};
export const Label = styled.p<{ $pickColor?: boolean }>`
color: ${props => (props.$pickColor ? '#00507f' : '#000')};
text-align: center;
font-family: 'Public Sans', sans-serif;
padding-top: 5px;
Expand Down
Loading