* add feature to change host schedule * update for type-check:ci * Update SingleForm.tsx for linterr * update as per design review * update for rerendering * migration files * On Delete set null for schedule if schedule is deleted, no need to delete the host * updated schema * added testcases * checking permissions before * updated for comments * update for initial value * updated and cleanup * set initial value * update after merge * updated nit style * update for console warning and added comments * nit/chore * chore * update for typechecks * update for scheduleId param after merge * update after merge or new changes in metadata.config * update after weights functionality * updated description text and column name as per review suggestion * resolved merge conflicts * fix typecheck due to changes in getScheduleSchema * update after merge having refactor * update refactored EventTypeWebWrapper after merge * updated props * updated trpc err code and added log * update due to refactor in EventAvailabilityTab * perf: Optimise TS type + select only the data that's needed for a specific function --------- Co-authored-by: Amit Sharma <[email protected]> Co-authored-by: Syed Ali Shahbaz <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
27 files changed
+600
-67
No files matched your search
@@ -119,6 +119,20 @@ export const getEventTypesFromDB = async (eventTypeId: number) => {
|
||||
...userSelect.select,
|
||||
},
|
||||
},
|
||||
schedule: {
|
||||
select: {
|
||||
availability: {
|
||||
select: {
|
||||
date: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
days: true,
|
||||
},
|
||||
},
|
||||
timeZone: true,
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
availability: {
|
||||
|
||||
@@ -101,6 +101,7 @@ export const roundRobinReassignment = async ({
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
schedule: null,
|
||||
}));
|
||||
|
||||
const roundRobinHosts = eventType.hosts.filter((host) => !host.isFixed);
|
||||
|
||||
@@ -16,16 +16,18 @@ interface IUserToValue {
|
||||
username: string | null;
|
||||
avatar: string;
|
||||
email: string;
|
||||
defaultScheduleId: number | null;
|
||||
}
|
||||
|
||||
export const mapUserToValue = (
|
||||
{ id, name, username, avatar, email }: IUserToValue,
|
||||
{ id, name, username, avatar, email, defaultScheduleId }: IUserToValue,
|
||||
pendingString: string
|
||||
) => ({
|
||||
value: `${id || ""}`,
|
||||
label: `${name || email || ""}${!username ? ` (${pendingString})` : ""}`,
|
||||
avatar,
|
||||
email,
|
||||
defaultScheduleId,
|
||||
});
|
||||
|
||||
const sortByLabel = (a: ReturnType<typeof mapUserToValue>, b: ReturnType<typeof mapUserToValue>) => {
|
||||
@@ -73,6 +75,7 @@ const CheckedHostField = ({
|
||||
priority: option.priority ?? 2,
|
||||
weight: option.weight ?? 100,
|
||||
weightAdjustment: option.weightAdjustment ?? 0,
|
||||
scheduleId: option.defaultScheduleId,
|
||||
}))
|
||||
);
|
||||
}}
|
||||
|
||||
@@ -17,6 +17,7 @@ export type CheckedSelectOption = {
|
||||
weightAdjustment?: number;
|
||||
isFixed?: boolean;
|
||||
disabled?: boolean;
|
||||
defaultScheduleId?: number | null;
|
||||
};
|
||||
|
||||
export const CheckedTeamSelect = ({
|
||||
|
||||
@@ -25,6 +25,7 @@ export type Host = {
|
||||
priority: number;
|
||||
weight: number;
|
||||
weightAdjustment: number;
|
||||
scheduleId?: number | null;
|
||||
};
|
||||
|
||||
export type CustomInputParsed = typeof customInputSchema._output;
|
||||
@@ -46,6 +47,7 @@ const tabs = [
|
||||
export type EventTypeSetup = RouterOutputs["viewer"]["eventTypes"]["get"]["eventType"];
|
||||
export type EventTypeAssignedUsers = RouterOutputs["viewer"]["eventTypes"]["get"]["eventType"]["children"];
|
||||
export type EventTypeHosts = RouterOutputs["viewer"]["eventTypes"]["get"]["eventType"]["hosts"];
|
||||
export type TeamMembers = RouterOutputs["viewer"]["eventTypes"]["get"]["teamMembers"];
|
||||
|
||||
export type EventTypeComponentProps = EventTypeSetupProps & {
|
||||
allActiveWorkflows?: Workflow[];
|
||||
|
||||
+65
-30
@@ -132,19 +132,25 @@ const FixedHosts = ({
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
automaticAddAllEnabled={!isRoundRobinEvent}
|
||||
isFixed={true}
|
||||
onActive={() =>
|
||||
onActive={() => {
|
||||
const currentHosts = getValues("hosts");
|
||||
setValue(
|
||||
"hosts",
|
||||
teamMembers.map((teamMember) => ({
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
teamMembers.map((teamMember) => {
|
||||
const host = currentHosts.find((host) => host.userId === parseInt(teamMember.value, 10));
|
||||
return {
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
// if host was already added, retain scheduleId
|
||||
scheduleId: host?.scheduleId || teamMember.defaultScheduleId,
|
||||
};
|
||||
}),
|
||||
{ shouldDirty: true }
|
||||
)
|
||||
}
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
@@ -175,19 +181,25 @@ const FixedHosts = ({
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
automaticAddAllEnabled={!isRoundRobinEvent}
|
||||
isFixed={true}
|
||||
onActive={() =>
|
||||
onActive={() => {
|
||||
const currentHosts = getValues("hosts");
|
||||
setValue(
|
||||
"hosts",
|
||||
teamMembers.map((teamMember) => ({
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
teamMembers.map((teamMember) => {
|
||||
const host = currentHosts.find((host) => host.userId === parseInt(teamMember.value, 10));
|
||||
return {
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
// if host was already added, retain scheduleId
|
||||
scheduleId: host?.scheduleId || teamMember.defaultScheduleId,
|
||||
};
|
||||
}),
|
||||
{ shouldDirty: true }
|
||||
)
|
||||
}
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</SettingsToggle>
|
||||
@@ -255,15 +267,21 @@ const RoundRobinHosts = ({
|
||||
isFixed={false}
|
||||
containerClassName={assignAllTeamMembers ? "-mt-4" : ""}
|
||||
onActive={() => {
|
||||
const currentHosts = getValues("hosts");
|
||||
setValue(
|
||||
"hosts",
|
||||
teamMembers.map((teamMember) => ({
|
||||
isFixed: false,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
teamMembers.map((teamMember) => {
|
||||
const host = currentHosts.find((host) => host.userId === parseInt(teamMember.value, 10));
|
||||
return {
|
||||
isFixed: false,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
// if host was already added, retain scheduleId
|
||||
scheduleId: host?.scheduleId || teamMember.defaultScheduleId,
|
||||
};
|
||||
}),
|
||||
{ shouldDirty: true }
|
||||
);
|
||||
setValue("isRRWeightsEnabled", false);
|
||||
@@ -346,6 +364,21 @@ const Hosts = ({
|
||||
);
|
||||
}, [schedulingType, setValue, getValues, submitCount]);
|
||||
|
||||
// To ensure existing host do not loose its scheduleId property, whenever a new host of same type is added.
|
||||
// This is because the host is created from list option in CheckedHostField component.
|
||||
const updatedHosts = (changedHosts: Host[]) => {
|
||||
const existingHosts = getValues("hosts");
|
||||
return changedHosts.map((newValue) => {
|
||||
const existingHost = existingHosts.find((host: Host) => host.userId === newValue.userId);
|
||||
return existingHost
|
||||
? {
|
||||
...newValue,
|
||||
scheduleId: existingHost.scheduleId,
|
||||
}
|
||||
: newValue;
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Controller<FormValues>
|
||||
name="hosts"
|
||||
@@ -355,7 +388,9 @@ const Hosts = ({
|
||||
<FixedHosts
|
||||
teamMembers={teamMembers}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
onChange={(changeValue) => {
|
||||
onChange([...updatedHosts(changeValue)]);
|
||||
}}
|
||||
assignAllTeamMembers={assignAllTeamMembers}
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
/>
|
||||
@@ -366,7 +401,7 @@ const Hosts = ({
|
||||
teamMembers={teamMembers}
|
||||
value={value}
|
||||
onChange={(changeValue) => {
|
||||
onChange([...value.filter((host: Host) => !host.isFixed), ...changeValue]);
|
||||
onChange([...value.filter((host: Host) => !host.isFixed), ...updatedHosts(changeValue)]);
|
||||
}}
|
||||
assignAllTeamMembers={assignAllTeamMembers}
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
@@ -376,7 +411,7 @@ const Hosts = ({
|
||||
teamMembers={teamMembers}
|
||||
value={value}
|
||||
onChange={(changeValue) => {
|
||||
const hosts = [...value.filter((host: Host) => host.isFixed), ...changeValue];
|
||||
const hosts = [...value.filter((host: Host) => host.isFixed), ...updatedHosts(changeValue)];
|
||||
onChange(hosts);
|
||||
}}
|
||||
assignAllTeamMembers={assignAllTeamMembers}
|
||||
|
||||
+177
-20
@@ -1,20 +1,31 @@
|
||||
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
||||
import { useState, memo, useEffect } from "react";
|
||||
import { Controller, useFormContext } from "react-hook-form";
|
||||
import type { OptionProps, SingleValueProps } from "react-select";
|
||||
import { components } from "react-select";
|
||||
|
||||
import type { EventAvailabilityTabWebWrapperProps } from "@calcom/atoms/event-types/wrappers/EventAvailabilityTabWebWrapper";
|
||||
import type {
|
||||
EventAvailabilityTabWebWrapperProps,
|
||||
GetAllSchedulesByUserIdQueryType,
|
||||
} from "@calcom/atoms/event-types/wrappers/EventAvailabilityTabWebWrapper";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { SelectSkeletonLoader } from "@calcom/features/availability/components/SkeletonLoader";
|
||||
import useLockedFieldsManager from "@calcom/features/ee/managed-event-types/hooks/useLockedFieldsManager";
|
||||
import type { AvailabilityOption, FormValues, EventTypeSetup } from "@calcom/features/eventtypes/lib/types";
|
||||
import type { TeamMembers } from "@calcom/features/eventtypes/components/EventType";
|
||||
import type {
|
||||
AvailabilityOption,
|
||||
FormValues,
|
||||
EventTypeSetup,
|
||||
Host,
|
||||
} from "@calcom/features/eventtypes/lib/types";
|
||||
import classNames from "@calcom/lib/classNames";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { weekdayNames } from "@calcom/lib/weekday";
|
||||
import { weekStartNum } from "@calcom/lib/weekstart";
|
||||
import { SchedulingType } from "@calcom/prisma/enums";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { Badge, Button, Icon, Select, SettingsToggle, SkeletonText } from "@calcom/ui";
|
||||
import { Avatar, Badge, Button, Icon, Label, Select, SettingsToggle, SkeletonText } from "@calcom/ui";
|
||||
import { Spinner } from "@calcom/ui/components/icon/Spinner";
|
||||
|
||||
type EventTypeScheduleDetailsProps = {
|
||||
scheduleQueryData?: RouterOutputs["viewer"]["availability"]["schedule"]["get"];
|
||||
@@ -23,11 +34,17 @@ type EventTypeScheduleDetailsProps = {
|
||||
editAvailabilityRedirectUrl?: string;
|
||||
};
|
||||
|
||||
type EventTypeTeamScheduleProps = {
|
||||
hostSchedulesQuery: GetAllSchedulesByUserIdQueryType;
|
||||
hosts?: Host[];
|
||||
};
|
||||
|
||||
type EventTypeScheduleProps = {
|
||||
availabilityQueryData?: RouterOutputs["viewer"]["availability"]["list"];
|
||||
isAvailabilityPending?: boolean;
|
||||
eventType: EventTypeSetup;
|
||||
} & EventTypeScheduleDetailsProps;
|
||||
} & EventTypeScheduleDetailsProps &
|
||||
EventTypeTeamScheduleProps;
|
||||
|
||||
type EventAvailabilityTabProps = EventAvailabilityTabWebWrapperProps & EventTypeScheduleProps;
|
||||
|
||||
@@ -270,32 +287,172 @@ const EventTypeSchedule = ({
|
||||
);
|
||||
};
|
||||
|
||||
const TeamMemberSchedule = ({
|
||||
host,
|
||||
index,
|
||||
teamMembers,
|
||||
hostScheduleQuery,
|
||||
}: {
|
||||
host: Host;
|
||||
index: number;
|
||||
teamMembers: TeamMembers;
|
||||
hostScheduleQuery: GetAllSchedulesByUserIdQueryType;
|
||||
}) => {
|
||||
const { t } = useLocale();
|
||||
|
||||
const formMethods = useFormContext<FormValues>();
|
||||
const { getValues } = formMethods;
|
||||
|
||||
const { data, isPending } = hostScheduleQuery({
|
||||
userId: host.userId,
|
||||
});
|
||||
|
||||
const schedules = data?.schedules;
|
||||
const options = schedules?.map((schedule) => ({
|
||||
value: schedule.id,
|
||||
label: schedule.name,
|
||||
isDefault: schedule.isDefault,
|
||||
isManaged: false,
|
||||
}));
|
||||
|
||||
//Set to defaultSchedule if Host Schedule is not previously selected
|
||||
const scheduleId = getValues(`hosts.${index}.scheduleId`);
|
||||
const value = options?.find((option) =>
|
||||
scheduleId
|
||||
? option.value === scheduleId
|
||||
: option.value === schedules?.find((schedule) => schedule.isDefault)?.id
|
||||
);
|
||||
|
||||
const member = teamMembers.find((mem) => mem.id === host.userId);
|
||||
const avatar = member?.avatar;
|
||||
const label = member?.name;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex w-full items-center">
|
||||
<Avatar size="sm" imageSrc={avatar} alt={label || ""} />
|
||||
<p className="text-emphasis my-auto ms-3 text-sm">{label}</p>
|
||||
</div>
|
||||
<div className="flex w-full flex-col pt-2 ">
|
||||
{isPending ? (
|
||||
<Spinner className="mt-2 h-6 w-6" />
|
||||
) : (
|
||||
<Controller
|
||||
name={`hosts.${index}.scheduleId`}
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<Select
|
||||
placeholder={t("select")}
|
||||
options={options}
|
||||
isSearchable={false}
|
||||
onChange={(selected) => {
|
||||
field.onChange(selected?.value || null);
|
||||
}}
|
||||
className="block w-full min-w-0 flex-1 rounded-sm text-sm"
|
||||
value={value as AvailabilityOption}
|
||||
components={{ Option, SingleValue }}
|
||||
isMulti={false}
|
||||
isDisabled={isPending}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const TeamAvailability = ({
|
||||
hosts,
|
||||
teamMembers,
|
||||
hostSchedulesQuery,
|
||||
}: EventTypeTeamScheduleProps & { teamMembers: TeamMembers }) => {
|
||||
const { t } = useLocale();
|
||||
const [animationRef] = useAutoAnimate<HTMLUListElement>();
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="border-subtle flex flex-col rounded-md">
|
||||
<div className="border-subtle mt-5 rounded-t-md border p-6 pb-5">
|
||||
<Label className="mb-1 text-sm font-semibold">{t("choose_hosts_schedule")}</Label>
|
||||
<p className="text-subtle max-w-full break-words text-sm leading-tight">
|
||||
{t("hosts_schedule_description")}
|
||||
</p>
|
||||
</div>
|
||||
<div className="border-subtle rounded-b-md border border-t-0 p-6">
|
||||
{hosts && hosts.length > 0 ? (
|
||||
<ul
|
||||
className={classNames("mb-4 mt-3 rounded-md", hosts.length >= 1 && "border-subtle border")}
|
||||
ref={animationRef}>
|
||||
{hosts?.map((host, index) => (
|
||||
<li
|
||||
key={host.userId}
|
||||
className={`flex flex-col px-3 py-2 ${
|
||||
index === hosts.length - 1 ? "" : "border-subtle border-b"
|
||||
}`}>
|
||||
<TeamMemberSchedule
|
||||
host={host}
|
||||
index={index}
|
||||
teamMembers={teamMembers}
|
||||
hostScheduleQuery={hostSchedulesQuery}
|
||||
/>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<p className="text-subtle max-w-full break-words text-sm leading-tight">
|
||||
{t("no_hosts_description")}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const UseCommonScheduleSettingsToggle = ({
|
||||
eventType,
|
||||
...rest
|
||||
}: Omit<EventAvailabilityTabProps, "isTeamEvent">) => {
|
||||
const { t } = useLocale();
|
||||
const { setValue, resetField, getFieldState, getValues } = useFormContext<FormValues>();
|
||||
const { setValue, resetField, getFieldState, getValues, watch } = useFormContext<FormValues>();
|
||||
|
||||
const [useHostSchedulesForTeamEvent, setUseHostSchedulesForTeamEvent] = useState(
|
||||
Boolean(getFieldState("schedule").isDirty ? getValues("schedule") : eventType.schedule)
|
||||
!Boolean(getFieldState("schedule").isDirty ? getValues("schedule") : eventType.schedule)
|
||||
);
|
||||
|
||||
const watchHosts = watch("hosts");
|
||||
|
||||
return (
|
||||
<SettingsToggle
|
||||
checked={useHostSchedulesForTeamEvent}
|
||||
onCheckedChange={(checked) => {
|
||||
setUseHostSchedulesForTeamEvent(checked);
|
||||
if (checked) {
|
||||
if (Boolean(eventType.schedule)) resetField("schedule");
|
||||
} else {
|
||||
setValue("schedule", null, { shouldDirty: Boolean(eventType.schedule) });
|
||||
}
|
||||
}}
|
||||
title={t("choose_common_schedule_team_event")}
|
||||
description={t("choose_common_schedule_team_event_description")}>
|
||||
<EventTypeSchedule eventType={eventType} {...rest} />
|
||||
</SettingsToggle>
|
||||
<>
|
||||
<SettingsToggle
|
||||
checked={!useHostSchedulesForTeamEvent}
|
||||
onCheckedChange={(checked) => {
|
||||
setUseHostSchedulesForTeamEvent(!checked);
|
||||
if (checked) {
|
||||
if (Boolean(eventType.schedule)) resetField("schedule");
|
||||
getValues("hosts").map((_, index) => {
|
||||
setValue(`hosts.${index}.scheduleId`, null, { shouldDirty: true });
|
||||
});
|
||||
} else {
|
||||
setValue("schedule", null, { shouldDirty: Boolean(eventType.schedule) });
|
||||
}
|
||||
}}
|
||||
title={t("choose_common_schedule_team_event")}
|
||||
description={t("choose_common_schedule_team_event_description")}>
|
||||
<EventTypeSchedule eventType={eventType} {...rest} />
|
||||
</SettingsToggle>
|
||||
{useHostSchedulesForTeamEvent && (
|
||||
<div className="lg:ml-14">
|
||||
<TeamAvailability
|
||||
hosts={watchHosts}
|
||||
teamMembers={rest.teamMembers}
|
||||
hostSchedulesQuery={rest.hostSchedulesQuery}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -27,12 +27,14 @@ export type Host = {
|
||||
priority: number;
|
||||
weight: number;
|
||||
weightAdjustment: number;
|
||||
scheduleId?: number | null;
|
||||
};
|
||||
export type TeamMember = {
|
||||
value: string;
|
||||
label: string;
|
||||
avatar: string;
|
||||
email: string;
|
||||
defaultScheduleId: number | null;
|
||||
};
|
||||
|
||||
export type FormValues = {
|
||||
|
||||
Reference in new issue
Block a user