From a92bb3fad943206deca814a9c60ad8ba239b2959 Mon Sep 17 00:00:00 2001 From: Andrew Smithwick Date: Sat, 13 Apr 2024 12:12:14 -0700 Subject: [PATCH] Andy/complete cancel pickup events (#220) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added complete and cancel buttons to PickupEvent Edit Page Also fixed text wrap issue Also updated Button component to take in class names * Added buttons to admin pickup home page * Moved complete and edit buttons to single pickup event page Updated button styling for pickup event page Updated common Button component styling to auto expand to content Hide the complete/cancel buttons if the event isn't active * Removed unnecessary CSS * Fixed bug with linkedEventUuid not found edge case If you clicked the select a pickup event toggle but didn't select an event, the API call wouldn't work b/c the linkedEventUuid would be '', not null. Fixed the issue by changing value to be null when linkedEventUuid is '' * Made admin pickup events sort by start date * Removed console.log (For last commit) https://stackoverflow.com/questions/54634515/typescript-sorting-object-by-date * Refactored code to eliminate creation of new variable You have to specify the key name like linkedEventUuid: linkedEventUuid || null the shorthand only works if you pass in a single variable that’s used as the key and value like u did for title * Added checks to make weird dates not break code added a popup instead * Use instanceof instead of === for error check * Added function to parse props for complete and cancel functions --- .../AdminPickupEvent/AdminPickupEventForm.tsx | 42 +++++++++++++------ src/pages/admin/store/pickup/index.tsx | 10 +++-- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/components/admin/event/AdminPickupEvent/AdminPickupEventForm.tsx b/src/components/admin/event/AdminPickupEvent/AdminPickupEventForm.tsx index d53ba189..5fb61cf0 100644 --- a/src/components/admin/event/AdminPickupEvent/AdminPickupEventForm.tsx +++ b/src/components/admin/event/AdminPickupEvent/AdminPickupEventForm.tsx @@ -25,6 +25,13 @@ interface IProps { upcomingEvents: PublicEvent[]; } +const parseProps = (isoStart: string, isoEnd: string, rawOrderLimit: string) => { + const start = new Date(isoStart).toISOString(); + const end = new Date(isoEnd).toISOString(); + const orderLimit = parseInt(`${rawOrderLimit}`, 10); + return { start, end, orderLimit }; +}; + export const completePickupEvent = async (uuid: UUID, token: string) => { try { await AdminEventManager.completePickupEvent({ @@ -89,18 +96,16 @@ const AdminPickupEventForm = ({ mode, defaultData = {}, token, upcomingEvents }: orderLimit: rawOrderLimit, } = formData; - const start = new Date(isoStart).toISOString(); - const end = new Date(isoEnd).toISOString(); - const orderLimit = parseInt(`${rawOrderLimit}`, 10); - try { + const { start, end, orderLimit } = parseProps(isoStart, isoEnd, `${rawOrderLimit}`); + const uuid = await AdminEventManager.createPickupEvent(token, { title, start, end, description, orderLimit, - linkedEventUuid, + linkedEventUuid: linkedEventUuid || null, }); showToast('Pickup Event created successfully!', '', [ { @@ -110,7 +115,11 @@ const AdminPickupEventForm = ({ mode, defaultData = {}, token, upcomingEvents }: ]); router.push(`${config.admin.store.pickup}/${uuid}`); } catch (error) { - reportError('Could not create pickup event', error); + if (error instanceof RangeError) { + reportError('Invalid date, could not create pickup event', error); + } else { + reportError('Could not create pickup event', error); + } } finally { setLoading(false); } @@ -128,13 +137,18 @@ const AdminPickupEventForm = ({ mode, defaultData = {}, token, upcomingEvents }: orderLimit: rawOrderLimit, } = formData; - const start = new Date(isoStart).toISOString(); - const end = new Date(isoEnd).toISOString(); - const orderLimit = parseInt(`${rawOrderLimit}`, 10); - try { + const { start, end, orderLimit } = parseProps(isoStart, isoEnd, `${rawOrderLimit}`); + await AdminEventManager.editPickupEvent({ - pickupEvent: { title, start, end, description, orderLimit, linkedEventUuid }, + pickupEvent: { + title, + start, + end, + description, + orderLimit, + linkedEventUuid: linkedEventUuid || null, + }, uuid: defaultData.uuid ?? '', token: token, @@ -153,7 +167,11 @@ const AdminPickupEventForm = ({ mode, defaultData = {}, token, upcomingEvents }: }, }); } catch (error) { - reportError('Could not save changes', error); + if (error instanceof RangeError) { + reportError("Invalid date, can't save changes", error); + } else { + reportError('Could not save changes', error); + } } finally { setLoading(false); } diff --git a/src/pages/admin/store/pickup/index.tsx b/src/pages/admin/store/pickup/index.tsx index 5e4b898c..881891e4 100644 --- a/src/pages/admin/store/pickup/index.tsx +++ b/src/pages/admin/store/pickup/index.tsx @@ -52,9 +52,13 @@ const AdminPickupPage = ({ futurePickupEvents, pastPickupEvents }: AdminPickupPa
- {displayPickupEvents.map(pickupEvent => ( - - ))} + {displayPickupEvents + .sort((x, y) => { + return Date.parse(x.start) - Date.parse(y.start); + }) + .map(pickupEvent => ( + + ))}
);