import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { oneHourHeight } from '../../SACalendar/utils/timeUtils'; dayjs.extend(utc); const listToTree = (arr = []) => { let map = {}, node, res = [], i; for (i = 0; i < arr.length; i += 1) { map[arr[i].id] = i; arr[i].children = []; } for (i = 0; i < arr.length; i += 1) { node = arr[i]; if (node.parentSpaceId !== null) { arr[map[node.parentSpaceId]].children.push(node); } else { res.push(node); } } return res; }; export function toTree(data) { const arr = data.map((x) => { let newX = { ...x }; newX.children = []; return newX; }); return listToTree(arr); } export const calculateMarginFromMinutes = (minutes: number) => (minutes / 60) * oneHourHeight; export const generateSlots = (slots: any) => slots.map((slot) => ({ ...slot, startDate: dayjs(slot.startDate) .set('month', dayjs().get('month')) .set('day', dayjs().get('day')) .format(), endDate: dayjs(slot.endDate) .set('month', dayjs().get('month')) .set('day', dayjs().get('day')) .format(), })); export const getHours = (startTime: string, endTime: string) => dayjs.utc(startTime).format('hh:mm a') + ' - ' + dayjs.utc(endTime).format('hh:mm a'); export const getWeeklyEventHours = (startTime: string, endTime: string) => { let duration = dayjs.utc(endTime).diff(dayjs.utc(startTime), 'minute'); if (duration < 0) duration += 1440; // minutes in day (60 * 24) duration = Number(Number(duration / 60).toFixed(2)); // duration in hours return dayjs.utc(startTime).format('hh:mma') + ' ' + duration + 'h'; };