Scheduling infrastructure for absolutely everyone.
Merged with main, fixed issue that caused multiple AvailableTimes refreshes.
Alex van Andel7030851efb18
diff --cc components/booking/DatePicker.tsxindex b58b6c93eac,00000000000..ac1a0420cfcmode 100644,000000..100644--- a/components/booking/DatePicker.tsx+++ b/components/booking/DatePicker.tsx@@@ -1,109 -1,0 +1,109 @@@+import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/solid";+import { useEffect, useState } from "react";+import dayjs, { Dayjs } from "dayjs";+import isToday from "dayjs/plugin/isToday";+dayjs.extend(isToday);++const DatePicker = ({ weekStart, onDatePicked, workingHours, disableToday }) => {+ const workingDays = workingHours.reduce((workingDays: number[], wh) => [...workingDays, ...wh.days], []);+ const [selectedMonth, setSelectedMonth] = useState(dayjs().month());+ const [selectedDate, setSelectedDate] = useState();++ useEffect(() => {+ if (selectedDate) onDatePicked(selectedDate);- }, [selectedDate, onDatePicked]);++ }, [selectedDate]);++ // Handle month changes+ const incrementMonth = () => {+ setSelectedMonth(selectedMonth + 1);+ };++ const decrementMonth = () => {+ setSelectedMonth(selectedMonth - 1);+ };++ // Set up calendar+ const daysInMonth = dayjs().month(selectedMonth).daysInMonth();+ const days = [];+ for (let i = 1; i <= daysInMonth; i++) {+ days.push(i);+ }++ // Create placeholder elements for empty days in first week+ let weekdayOfFirst = dayjs().month(selectedMonth).date(1).day();+ if (weekStart === "Monday") {+ weekdayOfFirst -= 1;+ if (weekdayOfFirst < 0) weekdayOfFirst = 6;+ }+ const emptyDays = Array(weekdayOfFirst)+ .fill(null)+ .map((day, i) => (+ <div key={`e-${i}`} className={"text-center w-10 h-10 rounded-full mx-auto"}>+ {null}+ </div>+ ));++ const isDisabled = (day: number) => {+ const date: Dayjs = dayjs().month(selectedMonth).date(day);+ return (+ date.isBefore(dayjs()) || !workingDays.includes(+date.format("d")) || (date.isToday() && disableToday)+ );+ };++ // Combine placeholder days with actual days+ const calendar = [+ ...emptyDays,+ ...days.map((day) => (+ <button+ key={day}+ onClick={() => setSelectedDate(dayjs().month(selectedMonth).date(day))}+ disabled={+ (selectedMonth < parseInt(dayjs().format("MM")) &&+ dayjs().month(selectedMonth).format("D") > day) ||+ isDisabled(day)+ }+ className={+ "text-center w-10 h-10 rounded-full mx-auto" ++ (isDisabled(day) ? " text-gray-400 font-light" : " text-blue-600 font-medium") ++ (selectedDate && selectedDate.isSame(dayjs().month(selectedMonth).date(day), "day")+ ? " bg-blue-600 text-white-important"+ : !isDisabled(day)+ ? " bg-blue-50"+ : "")+ }>+ {day}+ </button>+ )),+ ];++ return (+ <div className={"mt-8 sm:mt-0 " + (selectedDate ? "sm:w-1/3 border-r sm:px-4" : "sm:w-1/2 sm:pl-4")}>+ <div className="flex text-gray-600 font-light text-xl mb-4 ml-2">+ <span className="w-1/2">{dayjs().month(selectedMonth).format("MMMM YYYY")}</span>+ <div className="w-1/2 text-right">+ <button+ onClick={decrementMonth}+ className={"mr-4 " + (selectedMonth < parseInt(dayjs().format("MM")) && "text-gray-400")}+ disabled={selectedMonth < parseInt(dayjs().format("MM"))}>+ <ChevronLeftIcon className="w-5 h-5" />+ </button>+ <button onClick={incrementMonth}>+ <ChevronRightIcon className="w-5 h-5" />+ </button>+ </div>+ </div>+ <div className="grid grid-cols-7 gap-y-4 text-center">+ {["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]+ .sort((a, b) => (weekStart.startsWith(a) ? -1 : weekStart.startsWith(b) ? 1 : 0))+ .map((weekDay) => (+ <div key={weekDay} className="uppercase text-gray-400 text-xs tracking-widest">+ {weekDay}+ </div>+ ))}+ {calendar}+ </div>+ </div>+ );+};++export default DatePicker;diff --cc package.jsonindex a81a930bffe,c59a83a0e4b..b0ac1af372e--- a/package.json+++ b/package.json@@@ -37,8 -38,8 +39,9 @@@"uuid": "^8.3.2"},"devDependencies": {+ "@types/jest": "^26.0.23","@types/node": "^14.14.33",+ "@types/nodemailer": "^6.4.2","@types/react": "^17.0.3","@typescript-eslint/eslint-plugin": "^4.27.0","@typescript-eslint/parser": "^4.27.0",diff --cc pages/[user]/[type].tsxindex 66f3640e757,04a13d3b05b..973c8d7826a--- a/pages/[user]/[type].tsx+++ b/pages/[user]/[type].tsx@@@ -1,26 -1,32 +1,22 @@@-import { useEffect, useState } from "react";+import { useEffect, useState, useMemo } from "react";+ import { GetServerSideProps } from "next";import Head from "next/head";-import Link from "next/link";++import { ClockIcon, GlobeIcon, ChevronDownIcon } from "@heroicons/react/solid";import prisma from "../../lib/prisma";+ import { useRouter } from "next/router";import dayjs, { Dayjs } from "dayjs";- import { ClockIcon, GlobeIcon, ChevronDownIcon } from "@heroicons/react/solid";-import {- ClockIcon,- GlobeIcon,- ChevronDownIcon,- ChevronLeftIcon,- ChevronRightIcon,-} from "@heroicons/react/solid";--import isSameOrBefore from "dayjs/plugin/isSameOrBefore";--import utc from "dayjs/plugin/utc";--import timezone from "dayjs/plugin/timezone";--dayjs.extend(isSameOrBefore);--dayjs.extend(utc);--dayjs.extend(timezone);import { collectPageParameters, telemetryEventTypes, useTelemetry } from "../../lib/telemetry";import AvailableTimes from "../../components/booking/AvailableTimes";import TimeOptions from "../../components/booking/TimeOptions";import Avatar from "../../components/Avatar";import { timeZone } from "../../lib/clock";+import DatePicker from "../../components/booking/DatePicker";+import PoweredByCalendso from "../../components/ui/PoweredByCalendso";- import { useRouter } from "next/router";+import getSlots from "@lib/slots";- export default function Type(props) {+ export default function Type(props): Type {+ // Get router variablesconst router = useRouter();const { rescheduleUid } = router.query;@@@ -45,12 -41,66 +41,12 @@@telemetry.withJitsu((jitsu) => jitsu.track(telemetryEventTypes.pageView, collectPageParameters()));}, [telemetry]);- // Handle month changes- const incrementMonth = () => {- setSelectedMonth(selectedMonth + 1);- };-- const decrementMonth = () => {- setSelectedMonth(selectedMonth - 1);- };-- // Set up calendar- const daysInMonth = dayjs().month(selectedMonth).daysInMonth();- const days = [];- for (let i = 1; i <= daysInMonth; i++) {- days.push(i);- }-- // Create placeholder elements for empty days in first week- let weekdayOfFirst = dayjs().month(selectedMonth).date(1).day();- if (props.user.weekStart === "Monday") {- weekdayOfFirst -= 1;- if (weekdayOfFirst < 0) weekdayOfFirst = 6;- }- const emptyDays = Array(weekdayOfFirst)- .fill(null)- .map((day, i) => (- <div key={`e-${i}`} className={"text-center w-10 h-10 rounded-full mx-auto"}>- {null}- </div>- ));-- const changeDate = (day): void => {+ const changeDate = (date: Dayjs) => {telemetry.withJitsu((jitsu) => jitsu.track(telemetryEventTypes.dateSelected, collectPageParameters()));- setSelectedDate(date);- setSelectedDate(dayjs().month(selectedMonth).date(day));++ setSelectedDate(date.tz(timeZone()));};- const handleSelectTimeZone = (selectedTimeZone: string) => {- // Combine placeholder days with actual days- const calendar = [- ...emptyDays,- ...days.map((day) => (- <button- key={day}- onClick={() => changeDate(day)}- disabled={- selectedMonth < parseInt(dayjs().format("MM")) && dayjs().month(selectedMonth).format("D") > day- }- className={- "text-center w-10 h-10 rounded-full mx-auto " +- (dayjs().isSameOrBefore(dayjs().date(day).month(selectedMonth))- ? "bg-blue-50 text-blue-600 font-medium"- : "text-gray-400 font-light") +- (dayjs(selectedDate).month(selectedMonth).format("D") == day- ? " bg-blue-600 text-white-important"- : "")- }>- {day}- </button>- )),- ];-+ const handleSelectTimeZone = (selectedTimeZone: string): void => {if (selectedDate) {setSelectedDate(selectedDate.tz(selectedTimeZone));}@@@ -67,7 -119,21 +63,21 @@@{rescheduleUid && "Reschedule"} {props.eventType.title} | {props.user.name || props.user.username} |Calendso</title>- <link rel="icon" href="/favicon.ico" />+ <meta name="title" content={"Meet " + (props.user.name || props.user.username) + " via Calendso"} />+ <meta name="description" content={props.eventType.description} />++ <meta property="og:type" content="website" />+ <meta property="og:url" content="https://calendso/" />+ <meta property="og:title" content={"Meet " + (props.user.name || props.user.username) + " via Calendso"}/>+ <meta property="og:description" content={props.eventType.description}/>+ <meta property="og:image" content={"https://og-image-one-pi.vercel.app/" + encodeURIComponent("Meet **" + (props.user.name || props.user.username) + "** <br>" + props.eventType.description).replace(/'/g, "%27") + ".png?md=1&images=https%3A%2F%2Fcalendso.com%2Fcalendso-logo-white.svg&images=" + encodeURIComponent(props.user.avatar)} />++ <meta property="twitter:card" content="summary_large_image" />- <meta property="twitter:url" content="https://calendso/" />++ <meta property="twitter:url" content="https://calendso/" />+ <meta property="twitter:title" content={"Meet " + (props.user.name || props.user.username) + " via Calendso"} />+ <meta property="twitter:description" content={props.eventType.description} />- <meta property="twitter:image" content={"https://og-image-one-pi.vercel.app/" + encodeURIComponent("Meet **" + (props.user.name || props.user.username) + "** <br>" + props.eventType.description).replace(/'/g, "%27") + ".png?md=1&images=https%3A%2F%2Fcalendso.com%2Fcalendso-logo-white.svg&images=" + encodeURIComponent(props.user.avatar)} />-++ <meta property="twitter:image" content={"https://og-image-one-pi.vercel.app/" + encodeURIComponent("Meet **" + (props.user.name || props.user.username) + "** <br>" + props.eventType.description).replace(/'/g, "%27") + ".png?md=1&images=https%3A%2F%2Fcalendso.com%2Fcalendso-logo-white.svg&images=" + encodeURIComponent(props.user.avatar)} />++</Head><mainclassName={@@@ -125,18 -228,10 +135,18 @@@);}+interface WorkingHours {+ days: number[];+ startTime: number;+ length: number;+}++type Availability = WorkingHours;+- export async function getServerSideProps(context) {+ export const getServerSideProps: GetServerSideProps = async (context) => {const user = await prisma.user.findFirst({where: {- username: context.query.user,+ username: context.query.user.toLowerCase(),},select: {id: true,@@@ -182,27 -275,10 +192,27 @@@};}+ const getWorkingHours = (providesAvailability) =>+ providesAvailability.availability && providesAvailability.availability.length+ ? providesAvailability.availability+ : null;++ const workingHours: WorkingHours[] =+ getWorkingHours(eventType) ||+ getWorkingHours(user) ||+ [+ {- days: [1, 2, 3, 4, 5, 6, 7],++ days: [0, 1, 2, 3, 4, 5, 6],+ startTime: user.startTime,+ length: user.endTime,+ },+ ].filter((availability: Availability): boolean => typeof availability["days"] !== "undefined");+return {props: {user,eventType,+ workingHours,},};- }+ };diff --cc pages/availability/event/[type].tsxindex 129be9fceac,ff41c7614cb..4a48b7b10f4--- a/pages/availability/event/[type].tsx+++ b/pages/availability/event/[type].tsx@@@ -13,13 -11,7 +13,14 @@@ import { LocationMarkerIcon, PlusCircleimport { EventTypeCustomInput, EventTypeCustomInputType } from "../../../lib/eventTypeInput";import { PlusIcon } from "@heroicons/react/solid";+import dayjs from "dayjs";+import utc from "dayjs/plugin/utc";+dayjs.extend(utc);+import timezone from "dayjs/plugin/timezone";+dayjs.extend(timezone);+- export default function EventType(props) {+ export default function EventType(props: any): JSX.Element {++const router = useRouter();const inputOptions: OptionBase[] = [@@@ -33,8 -26,8 +34,9 @@@const [showAddCustomModal, setShowAddCustomModal] = useState(false);const [selectedLocation, setSelectedLocation] = useState<OptionBase | undefined>(undefined);const [selectedInputOption, setSelectedInputOption] = useState<OptionBase>(inputOptions[0]);- const [selectedCustomInput, setSelectedCustomInput] = useState<EventTypeCustomInput | undefined>(undefined);const [locations, setLocations] = useState(props.eventType.locations || []);+ const [schedule, setSchedule] = useState(undefined);++ const [selectedCustomInput, setSelectedCustomInput] = useState<EventTypeCustomInput | undefined>(undefined);const [customInputs, setCustomInputs] = useState<EventTypeCustomInput[]>(props.eventType.customInputs.sort((a, b) => a.id - b.id) || []);@@@ -131,6 -103,13 +133,37 @@@const closeAddCustomModal = () => {setSelectedInputOption(inputOptions[0]);setShowAddCustomModal(false);+ setSelectedCustomInput(undefined);+ };+++ const updateLocations = (e) => {++ e.preventDefault();++++ let details = {};++ if (e.target.location.value === LocationType.InPerson) {++ details = { address: e.target.address.value };++ }++++ const existingIdx = locations.findIndex((loc) => e.target.location.value === loc.type);++ if (existingIdx !== -1) {++ const copy = locations;++ copy[existingIdx] = { ...locations[existingIdx], ...details };++ setLocations(copy);++ } else {++ setLocations(locations.concat({ type: e.target.location.value, ...details }));++ }++++ setShowLocationModal(false);++ };++++ const removeLocation = (selectedLocation) => {++ setLocations(locations.filter((location) => location.type !== selectedLocation.type));++ };+++ const openEditCustomModel = (customInput: EventTypeCustomInput) => {+ setSelectedCustomInput(customInput);+ setSelectedInputOption(inputOptions.find((e) => e.value === customInput.type));+ setShowAddCustomModal(true);};const LocationOptions = () => {@@@ -168,30 -147,30 +201,6 @@@return null;};-- const updateLocations = (e) => {-- e.preventDefault();---- let details = {};-- if (e.target.location.value === LocationType.InPerson) {-- details = { address: e.target.address.value };-- }---- const existingIdx = locations.findIndex((loc) => e.target.location.value === loc.type);-- if (existingIdx !== -1) {-- const copy = locations;-- copy[existingIdx] = { ...locations[existingIdx], ...details };-- setLocations(copy);-- } else {-- setLocations(locations.concat({ type: e.target.location.value, ...details }));-- }---- setShowLocationModal(false);-- };---- const removeLocation = (selectedLocation) => {-- setLocations(locations.filter((location) => location.type !== selectedLocation.type));-- };--const updateCustom = (e) => {e.preventDefault();@@@ -614,7 -649,9 +690,7 @@@Is required</label></div>--+ <input type="hidden" name="id" id="id" value={selectedCustomInput?.id} />-<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"><button type="submit" className="btn btn-primary">Save@@@ -732,25 -747,22 +808,25 @@@ export async function getServerSideProp// Assuming it's Microsoft Teams.}- const eventType = await prisma.eventType.findUnique({- where: {- id: parseInt(context.query.type),- },- select: {- id: true,- title: true,- slug: true,- description: true,- length: true,- hidden: true,- locations: true,- eventName: true,- customInputs: true,- },- });+ if (!eventType) {+ return {+ notFound: true,+ };+ }++ const getAvailability = (providesAvailability) =>+ providesAvailability.availability && providesAvailability.availability.length+ ? providesAvailability.availability+ : null;++ const schedules = getAvailability(eventType) ||+ getAvailability(user) || [+ {- days: [1, 2, 3, 4, 5, 6, 7],++ days: [0, 1, 2, 3, 4, 5, 6],+ startTime: user.startTime,+ length: user.endTime >= 1440 ? 1439 : user.endTime,+ },+ ];return {props: {