Skip to content

Commit

Permalink
restructured api call
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-dange committed May 14, 2024
1 parent 1547272 commit 004ddf0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/lib/api/StoreAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,26 @@ export const getFutureOrderPickupEvents = async (
return response.data.pickupEvents;
};

export const getValidFutureOrderPickupEvents = async (
token: string
): Promise<PublicOrderPickupEvent[]> => {
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
Expand Down
18 changes: 2 additions & 16 deletions src/pages/store/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pages/store/orders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 004ddf0

Please sign in to comment.