diff --git a/src/app/profileScreen/page.tsx b/src/app/profileScreen/page.tsx index 48e2c696..523aee20 100644 --- a/src/app/profileScreen/page.tsx +++ b/src/app/profileScreen/page.tsx @@ -1,7 +1,7 @@ 'use client'; import { useRouter } from 'next/navigation'; -import { toast, ToastContainer } from 'react-toastify'; +import { toast } from 'react-toastify'; import { useEffect, useState } from 'react'; import { Heading2, @@ -18,8 +18,20 @@ import { fetchUser, fetchUserAddress, } from '@/api/supabase/queries/user_queries'; -import { Address, Product, User } from '@/schema/schema'; +import { + Address, + Order, + ProductWithQuantity, + Product, + User, +} from '@/schema/schema'; import ViewAllButton from '@/components/ViewAllButton/ViewAllButton'; +import { + fetchOrdersByUserIdSorted, + fetchOrderProductById, + fetchProductWithQuantityById, +} from '@/api/supabase/queries/order_queries'; +import { Check, CheckCircle, X } from 'react-feather'; import BackButton from '../../components/BackButton/BackButton'; import { LogOutButton, @@ -36,7 +48,7 @@ import { BackButtonDiv, BlankSpace, HeaderDiv, - Fullscreen, + MostRecentOrder, } from './styles'; import { signOut } from '../../api/supabase/auth/auth'; import 'react-toastify/dist/ReactToastify.css'; @@ -67,8 +79,11 @@ function FavoriteSection(props: { style={{ width: '75px', height: '75px' }} /> - {favorite.name} - Category: {favorite.category} +

+ {favorite.name} +
+ Product ID: {favorite.id} +

clickFunctions({ fav: favorite })} @@ -82,17 +97,148 @@ function FavoriteSection(props: { ); } -function OrderHistorySection() { - return ( -
- - - Order History - - - -
- ); +function OrderHistorySection(props: { + Orders: Order[]; + setOrder: (category: Order[]) => void; +}) { + const {Orders} = props; + const [firstOrderProducts, setFirstOrderProducts] = useState< + ProductWithQuantity[] + >([]); + const [fD, setFormattedDate] = useState(''); + + + + + useEffect(() => { + async function fetchFirstOrderProducts() { + if (Orders.length > 0) { + const firstOrder = Orders[0]; + const firstOrderProductIds = firstOrder.order_product_id_array.slice( + 0, + 3, + ); + + const productIds = await Promise.all( + firstOrderProductIds.map(productId => + fetchOrderProductById(productId).then( + orderproduct => orderproduct.product_id, + ), + ), + ); + + const productArray = await Promise.all( + productIds.map(async productId => + fetchProductWithQuantityById(productId).then(product => product), + ), + ); + + setFirstOrderProducts(productArray); + + const timestamp = firstOrder.created_at; + const date = new Date(timestamp); + const options: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric', year: 'numeric' }; + const formattedDate = date.toLocaleDateString('en-US', options); + setFormattedDate(formattedDate); + } + } + fetchFirstOrderProducts(); + }, [Orders]); + + if (firstOrderProducts.length > 0) { + let backgroundColor = 'transparent'; + if (Orders[0].status === 'Submitted') { + backgroundColor = '#CEE8BE'; + } else if (Orders[0].status === 'Rejected') { + backgroundColor = '#FFDDDD'; + } else if (Orders[0].status === 'Completed') { + backgroundColor = '#C7DDFF'; + } + let icon; + if (Orders[0].status === 'Submitted') { + icon = + } else if (Orders[0].status === 'Rejected') { + icon = + } else if (Orders[0].status === 'Completed'){ + icon = + } else { + icon = null; + } + return ( +
+ + + Order History + + +
+ + Order No. {Orders[0].id} + + + {fD} + +
+ {icon} + + {Orders[0].status} + +
+ + {firstOrderProducts.map(product => ( +
+ {product.name} +
+ ))} +
+
+
+
+ ); + } + return null; + } function AccountDetailSectionDelivery(props: { user: User }) { const { user } = props; @@ -125,7 +271,7 @@ function AccountDetailSectionDelivery(props: { user: User }) { Email - {user?.email} + {user?.email} Name @@ -197,23 +343,32 @@ export default function Profile() { const [Favorites, setFavorites] = useState([]); const [user, setUser] = useState(); + const [Orders, setOrder] = useState([]); + async function fetchProducts() { const data = (await arrayOfFavorites()) as Product[]; setFavorites(data); } + + async function fetchOrders() { + const data = (await fetchOrdersByUserIdSorted()) as Order[]; + setOrder(data); + } + async function getUser() { const data = await fetchUser(); setUser(data); } useEffect(() => { fetchProducts(); + fetchOrders(); getUser(); }, []); if (user === undefined) { return

Error Loading User

; } return ( - +
@@ -221,25 +376,19 @@ export default function Profile() { My Profile - {user.delivery_allowed ? ( ) : ( )} - + {/* router.push('/favorites')}> Favorites */} - +
); } diff --git a/src/app/profileScreen/styles.ts b/src/app/profileScreen/styles.ts index 7afc9ff6..79d2e6b5 100644 --- a/src/app/profileScreen/styles.ts +++ b/src/app/profileScreen/styles.ts @@ -143,3 +143,8 @@ export const Fullscreen = styled.div` width: 100%; height: 100%; `; + +export const MostRecentOrder = styled.div` + display: flex; + flex-direction: row; +`; diff --git a/src/schema/schema.ts b/src/schema/schema.ts index fd434377..bac687ac 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -16,12 +16,9 @@ export type User = { }; export enum OrderStatus { - ApprovalInProgress = 'Approval in Progress', - OrderApproved = 'Order Approved', - OrderProcessing = 'Order Processing', - ReadyForPickup = 'Ready for Pickup', - OutForDelivery = 'Out for Delivery', - Completed = 'Completed', + Submitted = 'Submitted', + Complete = 'Completed', + Rejected = 'Rejected', } export type Order = {