From c04a4c8f00965a86bf54e023f5bc9504a6e2eb3d Mon Sep 17 00:00:00 2001 From: Georgi 7DIGIT Date: Fri, 6 Sep 2024 15:07:02 +0300 Subject: [PATCH] Refactor: display the organization slots in the SelectConsultation backdrop and filter out any duplicates --- .../SelectConsultation/SelectConsultation.jsx | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/src/backdrops/SelectConsultation/SelectConsultation.jsx b/src/backdrops/SelectConsultation/SelectConsultation.jsx index 009169ff..78db582d 100644 --- a/src/backdrops/SelectConsultation/SelectConsultation.jsx +++ b/src/backdrops/SelectConsultation/SelectConsultation.jsx @@ -69,7 +69,6 @@ export const SelectConsultation = ({ providerId, campaignId ); - if (campaignId) { return data.map((x) => ({ time: parseUTCDate(x.time), @@ -77,7 +76,34 @@ export const SelectConsultation = ({ })); } - return data; + const slots = data.map((x) => { + if (x.time) { + return { + time: parseUTCDate(x.time), + organization_id: x.organization_id, + }; + } + return x; + }); + + const organizationSlotTimes = slots.reduce((acc, slot) => { + if (slot.organization_id) { + acc.push(slot.time.getTime()); + } + return acc; + }, []); + + // Ensure that there is no overlap between organization slots and regular slots + // If there are duplicates, remove the regular slot + if (organizationSlotTimes.length > 0) { + return slots.filter((slot) => { + if (slot.time) return slot; + const slotTime = new Date(slot).getTime(); + return !organizationSlotTimes.includes(slotTime); + }); + } + + return slots; }; const availableSlotsQuery = useQuery( ["available-slots", startDate, currentDay, providerId], @@ -97,7 +123,7 @@ export const SelectConsultation = ({ const renderFreeSlots = () => { const todaySlots = availableSlots?.filter((slot) => { - const slotDate = new Date(campaignId ? slot.time : slot).getDate(); + const slotDate = new Date(slot.time || slot).getDate(); const currentDayDate = new Date(currentDay).getDate(); // Check if the slot is for the current campaign @@ -111,9 +137,9 @@ export const SelectConsultation = ({ const options = todaySlots?.map( (slot) => { - const slotLocal = new Date(campaignId ? slot.time : slot); + const slotLocal = new Date(slot.time || slot); - const value = new Date(campaignId ? slot.time : slot).getTime(); + const value = new Date(slot.time || slot).getTime(); const getDoubleDigitHour = (hour) => hour === 24 ? "00" : hour < 10 ? `0${hour}` : hour; @@ -150,8 +176,23 @@ export const SelectConsultation = ({ } return isTimeMatching && slot.campaign_id === campaignId; }); + } else { + const allMatchingSlots = availableSlots.filter((slot) => { + const isTimeMatching = new Date(slot.time).getTime() === selectedSlot; + return isTimeMatching; + }); + + if (allMatchingSlots.length >= 1) { + const hasOrganizationSlot = allMatchingSlots.find( + (slot) => !!slot.organization_id + ); + if (hasOrganizationSlot) { + slotObject = hasOrganizationSlot; + } + } } - const time = campaignId ? slotObject : selectedSlot; + const time = slotObject || selectedSlot; + handleBlockSlot(time, providerData.consultationPrice); };