@kadconsulting/dry
Version:
KAD Reusable Component Library
139 lines • 6.92 kB
JavaScript
import { removeTimezoneFromISO, convertTime } from '@/utils/dateUtils';
const isoUtcMain = (isoString) => {
const formattedTime = removeTimezoneFromISO(isoString); // => 2024-04-03T20:00:00
return formattedTime;
};
const isoUtcDisplay = (isoString) => {
const formattedTime = isoUtcMain(isoString); // => 2024-04-03T20:00:00
const formattedTimeMain = convertTime(formattedTime); // =>'4/28/2024, 10:00:00 PM'
return formattedTimeMain;
};
const transformBookingsToCalendarEvents = (classGroupData, selectedFilters) => {
const calendarEvents = [];
const classGroups = selectedFilters?.classGroups || [];
const locations = selectedFilters?.locations;
const coaches = selectedFilters?.coaches;
if (selectedFilters?.eventGroups?.length)
return [];
classGroupData.forEach((classGroupInfo) => {
classGroupInfo?.attributes?.classes?.data?.forEach((classInfo) => {
const classLocation = classInfo?.attributes?.location?.data?.id;
const classCoach = classInfo?.attributes?.coaches?.data?.[0]?.id;
const membershipClass = classInfo?.attributes?.membershipClass;
const groupColor = classGroupInfo?.attributes?.calendarColor || 'black';
let isVisible = false;
if (selectedFilters) {
if ((!classGroups ||
classGroups.length === 0 ||
classGroups.includes(classGroupInfo?.id)) &&
(!locations ||
locations?.length === 0 ||
locations.includes(classLocation)) &&
(!coaches || coaches.length === 0 || coaches.includes(classCoach))) {
isVisible = true;
}
}
if (isVisible && membershipClass) {
classInfo.attributes.sessions.forEach((session) => {
session.bookings.data.forEach((booking) => {
const bookingAttr = booking.attributes;
if (bookingAttr.startDateTime && bookingAttr.endDateTime) {
const calendarEvent = {
id: String(booking.id),
title: bookingAttr.title || 'No Title',
start: isoUtcMain(bookingAttr.startDateTime),
end: isoUtcMain(bookingAttr.endDateTime),
color: groupColor,
allDay: false,
extraData: {
color: groupColor,
price: classInfo?.attributes?.price,
ageRange: membershipClass
? membershipClass?.ageRange
: 'Unknown',
coachFirstName: classInfo?.attributes?.coaches?.data?.[0]?.attributes?.user
?.data?.attributes?.firstName,
coachLastName: classInfo?.attributes?.coaches?.data?.[0]?.attributes?.user
?.data?.attributes?.lastName,
location: classInfo?.attributes?.location?.data?.attributes?.address,
description: classInfo?.attributes?.membershipClass?.description,
bookingId: booking.id,
classId: classInfo.id,
title: classInfo?.attributes?.title,
date: isoUtcDisplay(bookingAttr.startDateTime),
type: 'membershipClass',
},
};
calendarEvents.push(calendarEvent);
}
});
});
}
});
});
return calendarEvents;
};
const transformEventsToCalendarEvents = (eventGroups, selectedFilters) => {
const calendarEvents = [];
if (selectedFilters?.classes?.length)
return [];
eventGroups.forEach((eventGroup) => {
const eventGroups = selectedFilters?.eventGroups;
const locations = selectedFilters?.locations;
const coaches = selectedFilters?.coaches;
let isVisible = false;
const events = eventGroup?.events;
if (!events)
return;
events.forEach((event) => {
// TODO-p1: Fix the ts-ignore and fix the types
// @ts-ignore
const eventLocation = event?.location?.id;
// @ts-ignore
const eventCoach = event?.coach?.id;
const booking = event?.booking;
if (selectedFilters) {
if ((!eventGroups ||
eventGroups.length === 0 ||
eventGroups.includes(eventGroup?.id)) &&
(!locations ||
locations.length === 0 ||
locations.includes(eventLocation)) &&
(!coaches || coaches.length === 0 || coaches.includes(eventCoach))) {
isVisible = true;
}
}
if (isVisible) {
let color = eventGroup?.calendarColor || 'black';
if (booking?.startDateTime && booking?.endDateTime) {
const calendarEvent = {
id: String(booking.id),
title: booking?.title || 'No Title',
start: isoUtcMain(booking.startDateTime),
end: isoUtcMain(booking.endDateTime),
allDay: false,
color: color,
extraData: {
color,
price: event?.price,
coachFirstName: event?.coach?.data?.attributes?.user?.data?.attributes
?.firstName,
coachLastName: event.coach?.data?.attributes?.user?.data?.attributes?.lastName,
location: event?.location?.address,
description: event?.description,
bookingId: booking.id,
eventId: event.id,
title: event.title,
date: isoUtcDisplay(booking?.startDateTime),
type: 'event',
},
};
calendarEvents.push(calendarEvent);
}
}
});
});
return calendarEvents;
};
export { transformBookingsToCalendarEvents, transformEventsToCalendarEvents };
//# sourceMappingURL=transformFunctions.js.map