Skip to content

Commit

Permalink
Refactor: functionality to display slots for ogranizations and to not…
Browse files Browse the repository at this point in the history
… show duplicate slots
  • Loading branch information
georgipavlov-7DIGIT committed Sep 19, 2024
1 parent de911d0 commit 6959fd1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
60 changes: 51 additions & 9 deletions src/backdrops/SelectConsultation/SelectConsultation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,33 @@ export const SelectConsultation = ({
providerId,
campaignId
);
return data;
const slots = data.map((x) => {
if (x.time) {
return {
time: parseUTCDate(x.time),
organization_id: x.organization_id,
campaign_id: x.campaign_id,
};
}
return x;
});
const organizationSlotTimes = slots.reduce((acc, slot) => {
if (slot.organization_id) {
acc.push(new Date(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, campaignId],
Expand All @@ -91,9 +117,8 @@ export const SelectConsultation = ({
const renderFreeSlots = () => {
const todaySlots = availableSlots?.filter((slot) => {
if (!slot) return false;
const slotDate = campaignId
? parseUTCDate(slot.time).getDate()
: new Date(slot).getDate();
console.log(slot, "slot");
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 @@ -108,9 +133,9 @@ export const SelectConsultation = ({
);
const options = todaySlots?.map(
(slot) => {
const slotLocal = campaignId ? parseUTCDate(slot.time) : new Date(slot);
const value = campaignId
? parseUTCDate(slot.time).getTime()
const slotLocal = new Date(slot.time || slot);
const value = slot.time
? slot.time.getTime()
: new Date(slot).getTime();
const getDoubleDigitHour = (hour) =>
hour === 24 ? "00" : hour < 10 ? `0${hour}` : hour;
Expand Down Expand Up @@ -140,10 +165,24 @@ export const SelectConsultation = ({
let slotObject;
if (campaignId) {
slotObject = availableSlots.find((slot) => {
return parseUTCDate(slot.time).getTime() === selectedSlot;
return slot.time.getTime() === selectedSlot;
});
} 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 All @@ -154,6 +193,9 @@ export const SelectConsultation = ({

if (data?.campaign_id) {
setCampaignId(data.campaign_id);
if (couponError) {
setCouponError("");
}
}
} catch (err) {
const { message: errorMessage } = useError(err);
Expand Down
2 changes: 1 addition & 1 deletion src/screens/ProviderOverview/ProviderOverview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const ProviderOverview = ({ navigation, route }) => {

const time = useMemo(() => {
return isWithCampaign
? parseUTCDate(selectedSlot.current.time)
? new Date(selectedSlot.current.time)
: new Date(selectedSlot.current);
}, [isWithCampaign, selectedSlot.current]);

Expand Down
4 changes: 1 addition & 3 deletions src/services/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ async function blockSlot(clientId, providerId, slotTimestamp) {
typeof slotTimestamp === "object"
? {
campaign_id: slotTimestamp.campaign_id,
time: JSON.stringify(
parseUTCDate(slotTimestamp.time).getTime() / 1000
),
time: JSON.stringify(slotTimestamp.time.getTime() / 1000),
}
: JSON.stringify(slotTimestamp / 1000),
});
Expand Down

0 comments on commit 6959fd1

Please sign in to comment.