chore: refactor create event -type forms (#17106)
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
1 parent
92a48b466c
commit
8115c86e37
5 files changed
+226
-149
No files matched your search
@@ -2,9 +2,12 @@
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
|
||||
import { TeamEventTypeForm } from "@calcom/features/ee/teams/components/TeamEventTypeForm";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useCreateEventType } from "@calcom/lib/hooks/useCreateEventType";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { WizardLayout } from "@calcom/ui";
|
||||
import { Button, showToast } from "@calcom/ui";
|
||||
|
||||
@@ -35,13 +38,29 @@ export const CreateTeamEventType = () => {
|
||||
);
|
||||
};
|
||||
|
||||
const { data: team } = trpc.viewer.teams.get.useQuery(
|
||||
{ teamId: teamId ?? -1, isOrg: false },
|
||||
{ enabled: !!teamId }
|
||||
);
|
||||
|
||||
const { form, createMutation, isManagedEventType } = useCreateEventType(onSuccessMutation, onErrorMutation);
|
||||
|
||||
const orgBranding = useOrgBranding();
|
||||
const urlPrefix = orgBranding?.fullDomain ?? process.env.NEXT_PUBLIC_WEBSITE_URL;
|
||||
|
||||
return (
|
||||
<TeamEventTypeForm
|
||||
isTeamAdminOrOwner={true}
|
||||
teamSlug={team?.slug}
|
||||
teamId={teamId}
|
||||
isTeamAdminOrOwner={true}
|
||||
urlPrefix={urlPrefix}
|
||||
isPending={createMutation.isPending}
|
||||
form={form}
|
||||
isManagedEventType={isManagedEventType}
|
||||
handleSubmit={(values) => {
|
||||
createMutation.mutate(values);
|
||||
}}
|
||||
SubmitButton={SubmitButton}
|
||||
onSuccessMutation={onSuccessMutation}
|
||||
onErrorMutation={onErrorMutation}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,44 +1,45 @@
|
||||
import type { EventType } from "@prisma/client";
|
||||
import type { ReactNode } from "react";
|
||||
import type { UseFormReturn } from "react-hook-form";
|
||||
|
||||
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
|
||||
import { useIsPlatform } from "@calcom/atoms/monorepo";
|
||||
import { classNames } from "@calcom/lib";
|
||||
import { useCreateEventType } from "@calcom/lib/hooks/useCreateEventType";
|
||||
import type { CreateEventTypeFormValues } from "@calcom/lib/hooks/useCreateEventType";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import { SchedulingType } from "@calcom/prisma/enums";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Form, TextField, Tooltip } from "@calcom/ui";
|
||||
import { Alert, RadioGroup as RadioArea } from "@calcom/ui";
|
||||
|
||||
type props = {
|
||||
isTeamAdminOrOwner: boolean;
|
||||
teamSlug?: string | null;
|
||||
teamId: number;
|
||||
isPending: boolean;
|
||||
urlPrefix?: string;
|
||||
form: UseFormReturn<CreateEventTypeFormValues>;
|
||||
handleSubmit: (values: CreateEventTypeFormValues) => void;
|
||||
isManagedEventType: boolean;
|
||||
SubmitButton: (isPending: boolean) => ReactNode;
|
||||
onSuccessMutation: (eventType: EventType) => void;
|
||||
onErrorMutation: (message: string) => void;
|
||||
};
|
||||
export const TeamEventTypeForm = ({
|
||||
isTeamAdminOrOwner,
|
||||
teamSlug,
|
||||
teamId,
|
||||
form,
|
||||
urlPrefix,
|
||||
isPending,
|
||||
handleSubmit,
|
||||
isManagedEventType,
|
||||
SubmitButton,
|
||||
onSuccessMutation,
|
||||
onErrorMutation,
|
||||
}: props) => {
|
||||
const isPlatform = useIsPlatform();
|
||||
|
||||
const { t } = useLocale();
|
||||
const orgBranding = useOrgBranding();
|
||||
const { data: team } = trpc.viewer.teams.get.useQuery({ teamId, isOrg: false }, { enabled: !!teamId });
|
||||
const urlPrefix = orgBranding?.fullDomain ?? process.env.NEXT_PUBLIC_WEBSITE_URL;
|
||||
|
||||
const { form, createMutation, isManagedEventType } = useCreateEventType(onSuccessMutation, onErrorMutation);
|
||||
const { register, setValue, formState } = form;
|
||||
|
||||
return (
|
||||
<Form
|
||||
form={form}
|
||||
handleSubmit={(values) => {
|
||||
createMutation.mutate(values);
|
||||
}}>
|
||||
<Form form={form} handleSubmit={handleSubmit}>
|
||||
<div className="mt-3 space-y-6 pb-11">
|
||||
<TextField
|
||||
type="hidden"
|
||||
@@ -64,11 +65,13 @@ export const TeamEventTypeForm = ({
|
||||
label={`${t("url")}: ${urlPrefix}`}
|
||||
required
|
||||
addOnLeading={
|
||||
<Tooltip content={!isManagedEventType ? `team/${team?.slug}` : t("username_placeholder")}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
/{!isManagedEventType ? `team/${team?.slug}` : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
!isPlatform ? (
|
||||
<Tooltip content={!isManagedEventType ? `team/${teamSlug}` : t("username_placeholder")}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
/{!isManagedEventType ? `team/${teamSlug}` : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : undefined
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
@@ -86,14 +89,16 @@ export const TeamEventTypeForm = ({
|
||||
label={t("url")}
|
||||
required
|
||||
addOnLeading={
|
||||
<Tooltip
|
||||
content={`${urlPrefix}/${
|
||||
!isManagedEventType ? `team/${team?.slug}` : t("username_placeholder")
|
||||
}/`}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
{urlPrefix}/{!isManagedEventType ? `team/${team?.slug}` : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
!isPlatform ? (
|
||||
<Tooltip
|
||||
content={`${urlPrefix}/${
|
||||
!isManagedEventType ? `team/${teamSlug}` : t("username_placeholder")
|
||||
}/`}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
{urlPrefix}/{!isManagedEventType ? `team/${teamSlug}` : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : undefined
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
@@ -147,7 +152,7 @@ export const TeamEventTypeForm = ({
|
||||
</RadioArea.Group>
|
||||
</div>
|
||||
</div>
|
||||
{SubmitButton(createMutation.isPending)}
|
||||
{SubmitButton(isPending)}
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { EventType } from "@prisma/client";
|
||||
import { isValidPhoneNumber } from "libphonenumber-js";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { z } from "zod";
|
||||
|
||||
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
|
||||
@@ -9,22 +8,11 @@ import { TeamEventTypeForm } from "@calcom/features/ee/teams/components/TeamEven
|
||||
import { useCreateEventType } from "@calcom/lib/hooks/useCreateEventType";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { useTypedQuery } from "@calcom/lib/hooks/useTypedQuery";
|
||||
import { md } from "@calcom/lib/markdownIt";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import turndown from "@calcom/lib/turndownService";
|
||||
import { MembershipRole, SchedulingType } from "@calcom/prisma/enums";
|
||||
import {
|
||||
Button,
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
Editor,
|
||||
Form,
|
||||
showToast,
|
||||
TextField,
|
||||
Tooltip,
|
||||
} from "@calcom/ui";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Button, Dialog, DialogClose, DialogContent, DialogFooter, showToast } from "@calcom/ui";
|
||||
|
||||
import CreateEventTypeForm from "./CreateEventTypeForm";
|
||||
|
||||
// this describes the uniform data needed to create a new event type on Profile or Team
|
||||
export interface EventTypeParent {
|
||||
@@ -74,7 +62,6 @@ export default function CreateEventTypeDialog({
|
||||
}) {
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
const [firstRender, setFirstRender] = useState(true);
|
||||
const orgBranding = useOrgBranding();
|
||||
|
||||
const {
|
||||
@@ -115,9 +102,13 @@ export default function CreateEventTypeDialog({
|
||||
|
||||
const { form, createMutation, isManagedEventType } = useCreateEventType(onSuccessMutation, onErrorMutation);
|
||||
|
||||
const { register } = form;
|
||||
const urlPrefix = orgBranding?.fullDomain ?? process.env.NEXT_PUBLIC_WEBSITE_URL;
|
||||
|
||||
const { data: team } = trpc.viewer.teams.get.useQuery(
|
||||
{ teamId: teamId ?? -1, isOrg: false },
|
||||
{ enabled: !!teamId }
|
||||
);
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
name="new"
|
||||
@@ -138,106 +129,30 @@ export default function CreateEventTypeDialog({
|
||||
description={t("new_event_type_to_book_description")}>
|
||||
{teamId ? (
|
||||
<TeamEventTypeForm
|
||||
isTeamAdminOrOwner={isTeamAdminOrOwner}
|
||||
teamSlug={team?.slug}
|
||||
teamId={teamId}
|
||||
SubmitButton={SubmitButton}
|
||||
onSuccessMutation={onSuccessMutation}
|
||||
onErrorMutation={onErrorMutation}
|
||||
/>
|
||||
) : (
|
||||
<Form
|
||||
isTeamAdminOrOwner={isTeamAdminOrOwner}
|
||||
urlPrefix={urlPrefix}
|
||||
isPending={createMutation.isPending}
|
||||
form={form}
|
||||
isManagedEventType={isManagedEventType}
|
||||
handleSubmit={(values) => {
|
||||
createMutation.mutate(values);
|
||||
}}>
|
||||
<div className="mt-3 space-y-6 pb-11">
|
||||
<TextField
|
||||
label={t("title")}
|
||||
placeholder={t("quick_chat")}
|
||||
data-testid="event-type-quick-chat"
|
||||
{...register("title")}
|
||||
onChange={(e) => {
|
||||
form.setValue("title", e?.target.value);
|
||||
if (form.formState.touchedFields["slug"] === undefined) {
|
||||
form.setValue("slug", slugify(e?.target.value));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
{urlPrefix && urlPrefix.length >= 21 ? (
|
||||
<div>
|
||||
<TextField
|
||||
label={`${t("url")}: ${urlPrefix}`}
|
||||
required
|
||||
addOnLeading={
|
||||
<Tooltip content={!isManagedEventType ? pageSlug : t("username_placeholder")}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
/{!isManagedEventType ? pageSlug : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
form.setValue("slug", slugify(e?.target.value), { shouldTouch: true });
|
||||
}}
|
||||
/>
|
||||
|
||||
{isManagedEventType && (
|
||||
<p className="mt-2 text-sm text-gray-600">{t("managed_event_url_clarification")}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<TextField
|
||||
label={t("url")}
|
||||
required
|
||||
addOnLeading={
|
||||
<Tooltip
|
||||
content={`${urlPrefix}/${
|
||||
!isManagedEventType ? pageSlug : t("username_placeholder")
|
||||
}/`}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
{urlPrefix}/{!isManagedEventType ? pageSlug : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
form.setValue("slug", slugify(e?.target.value), { shouldTouch: true });
|
||||
}}
|
||||
/>
|
||||
{isManagedEventType && (
|
||||
<p className="mt-2 text-sm text-gray-600">{t("managed_event_url_clarification")}</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<>
|
||||
<Editor
|
||||
getText={() => md.render(form.getValues("description") || "")}
|
||||
setText={(value: string) => form.setValue("description", turndown(value))}
|
||||
excludedToolbarItems={["blockType", "link"]}
|
||||
placeholder={t("quick_video_meeting")}
|
||||
firstRender={firstRender}
|
||||
setFirstRender={setFirstRender}
|
||||
maxHeight="200px"
|
||||
/>
|
||||
|
||||
<div className="relative">
|
||||
<TextField
|
||||
type="number"
|
||||
required
|
||||
min="10"
|
||||
placeholder="15"
|
||||
label={t("duration")}
|
||||
className="pr-4"
|
||||
{...register("length", { valueAsNumber: true })}
|
||||
addOnSuffix={t("minutes")}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
</div>
|
||||
{SubmitButton(createMutation.isPending)}
|
||||
</Form>
|
||||
}}
|
||||
SubmitButton={SubmitButton}
|
||||
/>
|
||||
) : (
|
||||
<CreateEventTypeForm
|
||||
urlPrefix={urlPrefix}
|
||||
isPending={createMutation.isPending}
|
||||
form={form}
|
||||
isManagedEventType={isManagedEventType}
|
||||
handleSubmit={(values) => {
|
||||
createMutation.mutate(values);
|
||||
}}
|
||||
SubmitButton={SubmitButton}
|
||||
pageSlug={pageSlug}
|
||||
/>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useState } from "react";
|
||||
import type { UseFormReturn } from "react-hook-form";
|
||||
|
||||
import { useIsPlatform } from "@calcom/atoms/monorepo";
|
||||
import type { CreateEventTypeFormValues } from "@calcom/lib/hooks/useCreateEventType";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { md } from "@calcom/lib/markdownIt";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import turndown from "@calcom/lib/turndownService";
|
||||
import { Editor, Form, TextAreaField, TextField, Tooltip } from "@calcom/ui";
|
||||
|
||||
export default function CreateEventTypeForm({
|
||||
form,
|
||||
isManagedEventType,
|
||||
handleSubmit,
|
||||
pageSlug,
|
||||
isPending,
|
||||
urlPrefix,
|
||||
SubmitButton,
|
||||
}: {
|
||||
form: UseFormReturn<CreateEventTypeFormValues>;
|
||||
isManagedEventType: boolean;
|
||||
handleSubmit: (values: CreateEventTypeFormValues) => void;
|
||||
pageSlug?: string;
|
||||
isPending: boolean;
|
||||
urlPrefix?: string;
|
||||
SubmitButton: (isPending: boolean) => ReactNode;
|
||||
}) {
|
||||
const isPlatform = useIsPlatform();
|
||||
const { t } = useLocale();
|
||||
const [firstRender, setFirstRender] = useState(true);
|
||||
|
||||
const { register } = form;
|
||||
return (
|
||||
<Form
|
||||
form={form}
|
||||
handleSubmit={(values) => {
|
||||
handleSubmit(values);
|
||||
}}>
|
||||
<div className="mt-3 space-y-6 pb-11">
|
||||
<TextField
|
||||
label={t("title")}
|
||||
placeholder={t("quick_chat")}
|
||||
data-testid="event-type-quick-chat"
|
||||
{...register("title")}
|
||||
onChange={(e) => {
|
||||
form.setValue("title", e?.target.value);
|
||||
if (form.formState.touchedFields["slug"] === undefined) {
|
||||
form.setValue("slug", slugify(e?.target.value));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
{urlPrefix && urlPrefix.length >= 21 ? (
|
||||
<div>
|
||||
<TextField
|
||||
label={`${t("url")}: ${urlPrefix}`}
|
||||
required
|
||||
addOnLeading={
|
||||
!isPlatform ? (
|
||||
<Tooltip content={!isManagedEventType ? pageSlug : t("username_placeholder")}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
/{!isManagedEventType ? pageSlug : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : undefined
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
form.setValue("slug", slugify(e?.target.value), { shouldTouch: true });
|
||||
}}
|
||||
/>
|
||||
|
||||
{isManagedEventType && (
|
||||
<p className="mt-2 text-sm text-gray-600">{t("managed_event_url_clarification")}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<TextField
|
||||
label={t("url")}
|
||||
required
|
||||
addOnLeading={
|
||||
!isPlatform ? (
|
||||
<Tooltip
|
||||
content={`${urlPrefix}/${!isManagedEventType ? pageSlug : t("username_placeholder")}/`}>
|
||||
<span className="max-w-24 md:max-w-56">
|
||||
{urlPrefix}/{!isManagedEventType ? pageSlug : t("username_placeholder")}/
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : undefined
|
||||
}
|
||||
{...register("slug")}
|
||||
onChange={(e) => {
|
||||
form.setValue("slug", slugify(e?.target.value), { shouldTouch: true });
|
||||
}}
|
||||
/>
|
||||
{isManagedEventType && (
|
||||
<p className="mt-2 text-sm text-gray-600">{t("managed_event_url_clarification")}</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<>
|
||||
{isPlatform ? (
|
||||
<TextAreaField {...register("description")} placeholder={t("quick_video_meeting")} />
|
||||
) : (
|
||||
<Editor
|
||||
getText={() => md.render(form.getValues("description") || "")}
|
||||
setText={(value: string) => form.setValue("description", turndown(value))}
|
||||
excludedToolbarItems={["blockType", "link"]}
|
||||
placeholder={t("quick_video_meeting")}
|
||||
firstRender={firstRender}
|
||||
setFirstRender={setFirstRender}
|
||||
maxHeight="200px"
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="relative">
|
||||
<TextField
|
||||
type="number"
|
||||
required
|
||||
min="10"
|
||||
placeholder="15"
|
||||
label={t("duration")}
|
||||
className="pr-4"
|
||||
{...register("length", { valueAsNumber: true })}
|
||||
addOnSuffix={t("minutes")}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
</div>
|
||||
{SubmitButton(isPending)}
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -11,13 +11,15 @@ import { unlockedManagedEventTypeProps } from "@calcom/prisma/zod-utils";
|
||||
import { createEventTypeInput } from "@calcom/prisma/zod/custom/eventtype";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
export type CreateEventTypeFormValues = z.infer<typeof createEventTypeInput>;
|
||||
|
||||
export const useCreateEventType = (
|
||||
onSuccessMutation: (eventType: EventType) => void,
|
||||
onErrorMutation: (message: string) => void
|
||||
) => {
|
||||
const utils = trpc.useUtils();
|
||||
const { t } = useLocale();
|
||||
const form = useForm<z.infer<typeof createEventTypeInput>>({
|
||||
const form = useForm<CreateEventTypeFormValues>({
|
||||
defaultValues: {
|
||||
length: 15,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user