Skip to content

Commit

Permalink
Refactor: display the organization slots in the SelectConsultation ba…
Browse files Browse the repository at this point in the history
…ckdrop and filter out any duplicates
  • Loading branch information
georgipavlov-7DIGIT committed Sep 6, 2024
1 parent 6284a95 commit c04a4c8
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/backdrops/SelectConsultation/SelectConsultation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,41 @@ export const SelectConsultation = ({
providerId,
campaignId
);

if (campaignId) {
return data.map((x) => ({
time: parseUTCDate(x.time),
campaign_id: x.campaign_id,
}));
}

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],
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down

0 comments on commit c04a4c8

Please sign in to comment.