@kadconsulting/dry
Version:
KAD Reusable Component Library
268 lines • 11.4 kB
JavaScript
// @ts-nocheck
// TODO-p1: Remove ts-nocheck
// import { BookingAttributes, CalendarAttributes } from '../types/main';
// import { CalendarEvent } from '../@@NewComponents/Client/dry-calendar/calTemp/components/CalendarComponent/CalendarComponent';
// import { ResourceCalendarEvent } from '../@@NewComponents/Client/dry-calendar/calTemp/components/ResourceCalendar/ResourceCalendar';
const isoUtc = (isoString) => {
let datePart, timePart;
// Check if the string contains the 'T' character, typical for ISO strings
if (isoString.includes('T')) {
[datePart, timePart] = isoString.split('T');
}
else {
// Handle cases where the input might not be in standard ISO format
datePart = isoString.substring(0, 10); // Assuming 'YYYY-MM-DD' format
timePart = isoString.substring(11, 19); // Assuming 'hh:mm:ss' format
}
// Remove any extraneous parts from time (like milliseconds or timezone info)
timePart = timePart.split('.')[0].split('Z')[0];
// Extract year, month, and day from the date part
const [year, month, day] = datePart.split('-').map(Number);
// Extract hours and minutes from the time part
const [hours, minutes] = timePart.split(':').map(Number);
// Create a new Date object using local time values
const formattedTime = new Date(year, month - 1, day, hours, minutes);
return formattedTime;
};
const transformResourceBookingsToCalendarEvents = ({ bookings, resourceId, selectedFilters, }) => {
return bookings.reduce((calendarEvents, booking) => {
const bookingAttr = booking;
const eventGroups = selectedFilters?.eventGroups;
const locations = selectedFilters?.locations;
const coaches = selectedFilters?.coaches;
const services = selectedFilters?.services;
const classGroups = selectedFilters?.classGroups;
const membershipClasses = selectedFilters?.membershipClasses;
const categories = selectedFilters?.categories;
const simulatorBays = selectedFilters?.simulatorBays;
const bookingLocation = booking?.location?.id;
const bookingCoaches = booking?.coaches;
const bookingEvent = booking?.event?.eventGroup?.id;
const bookingService = booking?.service?.id;
const bookingClassGroup =
// @ts-ignore
booking?.class?.classGroup?.id;
const bookingMembershipClass = booking?.class?.id;
const bookingSimulatorBay = booking?.simulator_bays;
let isVisible = true;
const hasACoach = () => {
let flag = false;
bookingCoaches?.forEach((coach) => {
if (coaches?.includes(coach.id)) {
flag = true;
}
});
return flag;
};
const hasASimulatorBay = () => {
let flag = false;
bookingSimulatorBay?.forEach((simulatorBay) => {
if (simulatorBays?.includes(simulatorBay.id)) {
flag = true;
}
});
return flag;
};
console.log({
booking,
hasASimulatorBay: hasASimulatorBay(),
hasACoach: hasACoach(),
});
if (selectedFilters) {
isVisible =
(!eventGroups ||
eventGroups?.length === 0 ||
eventGroups?.includes(bookingEvent)) &&
(!locations ||
locations?.length === 0 ||
locations?.includes(bookingLocation)) &&
(!coaches || coaches?.length === 0 || hasACoach()) &&
(!services ||
services?.length === 0 ||
services?.includes(bookingService)) &&
(!classGroups ||
classGroups?.length === 0 ||
classGroups?.includes(bookingClassGroup)) &&
(!membershipClasses ||
membershipClasses?.length === 0 ||
membershipClasses?.includes(bookingMembershipClass)) &&
(!categories ||
categories?.length === 0 ||
categories?.includes(bookingCategory)) &&
(!simulatorBays || simulatorBays?.length === 0 || hasASimulatorBay());
}
let color = 'black';
if (booking?.service) {
color = booking?.service?.calendarColor;
}
if (booking?.class) {
color = booking?.class?.classGroup?.calendarColor || 'black';
}
if (booking?.event) {
color = booking?.event?.eventGroup?.calendarColor;
}
if (isVisible && bookingAttr?.startDateTime && bookingAttr?.endDateTime) {
const calendarEvent = {
id: String(booking?.id),
title: bookingAttr?.title || 'No Title',
resourceId,
// bounds: ?{
// // For "select" action
// x: number,
// y: number,
// top: number,
// right: number,
// left: number,
// bottom: number,
// },
// box: ?{
// // For "click" or "doubleClick" actions
// clientX: number,
// clientY: number,
// x: number,
// y: number,
// },
start: isoUtc(bookingAttr?.startDateTime),
end: isoUtc(bookingAttr?.endDateTime),
allDay: false,
color,
extraData: {
bookingId: booking?.id,
title: bookingAttr?.title,
date: isoUtc(bookingAttr?.startDateTime),
type: 'membershipClass',
},
};
return [...calendarEvents, calendarEvent];
}
return calendarEvents;
}, []);
};
const transformResourceCalendarToCalendarEvents = ({ calendar, resourceId, blockedTimeColor, }) => {
const test = calendar[0]?.blocked_times?.reduce((calendarEvents, data) => {
const id = data?.id;
const { startTime, endTime, reason, type, booking } = data;
if (startTime && endTime && !booking) {
const calendarEvent = {
id: String(id),
title: `${reason} - ${type}`,
resourceId,
// bounds: ?{
// // For "select" action
// x: number,
// y: number,
// top: number,
// right: number,
// left: number,
// bottom: number,
// },
// box: ?{
// // For "click" or "doubleClick" actions
// clientX: number,
// clientY: number,
// x: number,
// y: number,
// },
start: isoUtc(startTime),
end: isoUtc(endTime),
allDay: false,
color: blockedTimeColor,
extraData: {
blockedTimeId: id,
title: `${reason} - ${type}`,
date: isoUtc(startTime),
},
};
return [...calendarEvents, calendarEvent];
}
return calendarEvents;
}, []);
return test;
};
const transformAllBookingsToCalendarEvents = (bookings, selectedFilters) => {
return bookings.reduce((calendarEvents, booking) => {
const bookingAttr = booking;
const eventGroups = selectedFilters?.eventGroups;
const locations = selectedFilters?.locations;
const coaches = selectedFilters?.coaches;
const services = selectedFilters?.services;
const classGroups = selectedFilters?.classGroups;
const membershipClasses = selectedFilters?.membershipClasses;
const simulatorBays = selectedFilters?.simulatorBays;
const bookingLocation = booking?.location?.id;
const bookingCoaches = booking?.coaches;
const bookingEvent = booking?.event?.eventGroup?.id;
const bookingService = booking?.service?.id;
const bookingClassGroup = booking?.class?.classGroup?.id;
const bookingMembershipClass = booking?.class?.id;
const bookingSimulatorBay = booking?.simulator_bays;
const hasACoach = () => {
let flag = false;
bookingCoaches?.forEach((coach) => {
if (coaches?.includes(coach.id)) {
flag = true;
}
});
return flag;
};
const hasASimulatorBay = () => {
let flag = false;
bookingSimulatorBay?.forEach((simulatorBay) => {
if (simulatorBays?.includes(simulatorBay.id)) {
flag = true;
}
});
return flag;
};
const isVisible = !selectedFilters ||
((!eventGroups ||
eventGroups?.length === 0 ||
eventGroups?.includes(bookingEvent)) &&
(!locations ||
locations?.length === 0 ||
locations?.includes(bookingLocation)) &&
(!coaches || coaches?.length === 0 || hasACoach()) &&
(!services ||
services?.length === 0 ||
services?.includes(bookingService)) &&
(!classGroups ||
classGroups?.length === 0 ||
classGroups?.includes(bookingClassGroup)) &&
(!membershipClasses ||
membershipClasses?.length === 0 ||
membershipClasses?.includes(bookingMembershipClass)) &&
(!simulatorBays ||
simulatorBays?.length === 0 ||
hasASimulatorBay()));
let color = 'black';
if (booking?.service) {
color = booking?.service?.calendarColor;
}
if (booking?.class) {
color = booking?.class?.classGroup?.calendarColor || 'black';
}
if (booking?.event) {
color = booking?.event?.eventGroup?.calendarColor;
}
if (isVisible && bookingAttr?.startDateTime && bookingAttr?.endDateTime) {
const calendarEvent = {
id: String(booking.id),
title: bookingAttr?.title || 'No Title',
start: isoUtc(bookingAttr?.startDateTime),
end: isoUtc(bookingAttr?.endDateTime),
allDay: false,
color,
extraData: {
bookingId: booking.id,
title: bookingAttr?.title,
date: isoUtc(bookingAttr?.startDateTime),
type: 'membershipClass',
},
};
return [...calendarEvents, calendarEvent];
}
return calendarEvents;
}, []);
};
export { transformResourceBookingsToCalendarEvents, transformAllBookingsToCalendarEvents, transformResourceCalendarToCalendarEvents, };
//# sourceMappingURL=BETransformFunctions.js.map