import React, { useEffect, useState } from "react"; import TimezoneSelect from "react-timezone-select"; import { TrashIcon } from "@heroicons/react/outline"; import { WeekdaySelect } from "./WeekdaySelect"; import SetTimesModal from "./modal/SetTimesModal"; import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; import { Availability } from "@prisma/client"; dayjs.extend(utc); dayjs.extend(timezone); type Props = { timeZone: string; availability: Availability[]; setTimeZone: unknown; }; export const Scheduler = ({ availability, setAvailability, timeZone: selectedTimeZone, setTimeZone, }: Props) => { const [editSchedule, setEditSchedule] = useState(-1); const [dateOverrides, setDateOverrides] = useState([]); const [openingHours, setOpeningHours] = useState([]); useEffect(() => { setOpeningHours( availability .filter((item: Availability) => item.days.length !== 0) .map((item) => { item.startDate = dayjs().utc().startOf("day").add(item.startTime, "minutes"); item.endDate = dayjs().utc().startOf("day").add(item.endTime, "minutes"); return item; }) ); setDateOverrides(availability.filter((item: Availability) => item.date)); }, []); // updates availability to how it should be formatted outside this component. useEffect(() => { setAvailability({ dateOverrides: dateOverrides, openingHours: openingHours, }); }, [dateOverrides, openingHours]); const addNewSchedule = () => setEditSchedule(openingHours.length); const applyEditSchedule = (changed) => { // new entry if (!changed.days) { changed.days = [1, 2, 3, 4, 5]; // Mon - Fri setOpeningHours(openingHours.concat(changed)); } else { // update const replaceWith = { ...openingHours[editSchedule], ...changed }; openingHours.splice(editSchedule, 1, replaceWith); setOpeningHours([].concat(openingHours)); } }; const removeScheduleAt = (toRemove: number) => { openingHours.splice(toRemove, 1); setOpeningHours([].concat(openingHours)); }; const OpeningHours = ({ idx, item }) => (
  • (item.days = selected)} />
  • ); return (
    setTimeZone(tz.value)} className="shadow-sm focus:ring-black focus:border-black mt-1 block w-full sm:text-sm border-gray-300 rounded-md" />
      {openingHours.map((item, idx) => ( ))}
    {editSchedule >= 0 && ( applyEditSchedule({ ...(openingHours[editSchedule] || {}), ...times })} onExit={() => setEditSchedule(-1)} /> )}
    ); };