From 004ddf0a4c0ab0960ea351110baa54a1c2b63bbe Mon Sep 17 00:00:00 2001 From: Nikhil Dange Date: Mon, 13 May 2024 19:47:59 -0700 Subject: [PATCH] restructured api call --- src/lib/api/StoreAPI.ts | 20 ++++++++++++++++++++ src/pages/store/cart.tsx | 18 ++---------------- src/pages/store/orders.tsx | 2 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/lib/api/StoreAPI.ts b/src/lib/api/StoreAPI.ts index d1a0eda9..ac0b2539 100644 --- a/src/lib/api/StoreAPI.ts +++ b/src/lib/api/StoreAPI.ts @@ -400,6 +400,26 @@ export const getFutureOrderPickupEvents = async ( return response.data.pickupEvents; }; +export const getValidFutureOrderPickupEvents = async ( + token: string +): Promise => { + return getFutureOrderPickupEvents(token).then(events => + events + .filter(event => event.status !== 'CANCELLED') + .filter( + event => !(event.orders && event.orderLimit && event.orders.length > event.orderLimit) + ) + // filter out events that have a start time less than 2 days from now + .filter(event => { + const startTime = new Date(event.start); + const now = Date.now(); + const twoDaysInMs = 2 * 24 * 60 * 60 * 1000; + const twoDaysFromNow = new Date(now + twoDaysInMs); + return startTime >= twoDaysFromNow; + }) + ); +}; + export const placeMerchOrder = async ( token: string, data: PlaceMerchOrderRequest diff --git a/src/pages/store/cart.tsx b/src/pages/store/cart.tsx index 2df7b6e4..765ab9af 100644 --- a/src/pages/store/cart.tsx +++ b/src/pages/store/cart.tsx @@ -8,7 +8,7 @@ import { StoreConfirmModal, } from '@/components/store'; import { config, showToast } from '@/lib'; -import { getFutureOrderPickupEvents } from '@/lib/api/StoreAPI'; +import { getValidFutureOrderPickupEvents } from '@/lib/api/StoreAPI'; import { getCurrentUserAndRefreshCookie } from '@/lib/api/UserAPI'; import withAccessType from '@/lib/hoc/withAccessType'; import { StoreManager } from '@/lib/managers'; @@ -248,21 +248,7 @@ const getServerSidePropsFunc: GetServerSideProps = async ({ req, res }) => { const AUTH_TOKEN = getServerCookie(CookieType.ACCESS_TOKEN, { req, res }); const savedCartPromise = CartService.getCart({ req, res }); - const pickupEventsPromise = getFutureOrderPickupEvents(AUTH_TOKEN).then(events => - events - .filter(event => event.status !== 'CANCELLED') - .filter( - event => !(event.orders && event.orderLimit && event.orders.length > event.orderLimit) - ) - // filter out events that have a start time less than 2 days from now - .filter(event => { - const startTime = new Date(event.start); - const now = Date.now(); - const twoDaysInMs = 2 * 24 * 60 * 60 * 1000; - const twoDaysFromNow = new Date(now + twoDaysInMs); - return startTime >= twoDaysFromNow; - }) - ); + const pickupEventsPromise = getValidFutureOrderPickupEvents(AUTH_TOKEN); const userPromise = getCurrentUserAndRefreshCookie(AUTH_TOKEN, { req, res }); // gather the API request promises and await them concurrently diff --git a/src/pages/store/orders.tsx b/src/pages/store/orders.tsx index 681efeaf..6ae2f2ce 100644 --- a/src/pages/store/orders.tsx +++ b/src/pages/store/orders.tsx @@ -68,7 +68,7 @@ export default StoreOrderPage; const getServerSidePropsFunc: GetServerSidePropsWithAuth = async ({ authToken }) => { const ordersPromise = StoreAPI.getAllOrders(authToken); - const futurePickupEventsPromise = StoreAPI.getFutureOrderPickupEvents(authToken); + const futurePickupEventsPromise = StoreAPI.getValidFutureOrderPickupEvents(authToken); const [orders, futurePickupEvents] = await Promise.all([ ordersPromise,