2022-11-10 20:23:56 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-11-29 21:38:54 +00:00
|
|
|
import { z } from "zod";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
2023-01-05 19:55:55 +00:00
|
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import slugify from "@calcom/lib/slugify";
|
2023-02-10 09:53:03 +00:00
|
|
|
import { telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Avatar, Button, Form, ImageUploader, TextField } from "@calcom/ui";
|
|
|
|
import { FiArrowRight } from "@calcom/ui/components/icon";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
|
|
|
import { NewTeamFormValues } from "../lib/types";
|
|
|
|
|
2022-12-19 14:38:33 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
returnTo: z.string(),
|
|
|
|
});
|
2022-11-29 21:38:54 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
export const CreateANewTeamForm = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
2023-02-10 09:53:03 +00:00
|
|
|
const telemetry = useTelemetry();
|
2022-12-19 14:38:33 +00:00
|
|
|
const returnToParsed = querySchema.safeParse(router.query);
|
|
|
|
|
2023-01-05 19:55:55 +00:00
|
|
|
const returnToParam =
|
|
|
|
(returnToParsed.success ? getSafeRedirectUrl(returnToParsed.data.returnTo) : "/settings/teams") ||
|
|
|
|
"/settings/teams";
|
2022-11-29 21:38:54 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const newTeamFormMethods = useForm<NewTeamFormValues>();
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const createTeamMutation = trpc.viewer.teams.create.useMutation({
|
2022-11-10 20:23:56 +00:00
|
|
|
onSuccess: (data) => {
|
2023-02-10 09:53:03 +00:00
|
|
|
telemetry.event(telemetryEventTypes.team_created);
|
2022-11-10 20:23:56 +00:00
|
|
|
router.push(`/settings/teams/${data.id}/onboard-members`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const validateTeamSlugQuery = trpc.viewer.teams.validateTeamSlug.useQuery(
|
|
|
|
{ slug: newTeamFormMethods.watch("slug") },
|
2022-11-10 20:23:56 +00:00
|
|
|
{
|
|
|
|
enabled: false,
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const validateTeamSlug = async () => {
|
|
|
|
await validateTeamSlugQuery.refetch();
|
|
|
|
if (validateTeamSlugQuery.isFetched) return validateTeamSlugQuery.data || t("team_url_taken");
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-11-12 13:31:11 +00:00
|
|
|
<Form
|
|
|
|
form={newTeamFormMethods}
|
|
|
|
handleSubmit={(v) => {
|
|
|
|
if (!createTeamMutation.isLoading) createTeamMutation.mutate(v);
|
|
|
|
}}>
|
2022-11-10 20:23:56 +00:00
|
|
|
<div className="mb-8">
|
|
|
|
<Controller
|
|
|
|
name="name"
|
|
|
|
control={newTeamFormMethods.control}
|
|
|
|
defaultValue=""
|
|
|
|
rules={{
|
|
|
|
required: t("must_enter_team_name"),
|
|
|
|
}}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<>
|
|
|
|
<TextField
|
|
|
|
className="mt-2"
|
|
|
|
name="name"
|
|
|
|
label={t("team_name")}
|
2022-11-30 20:51:44 +00:00
|
|
|
defaultValue={value}
|
2022-11-10 20:23:56 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
newTeamFormMethods.setValue("name", e?.target.value);
|
|
|
|
if (newTeamFormMethods.formState.touchedFields["slug"] === undefined) {
|
|
|
|
newTeamFormMethods.setValue("slug", slugify(e?.target.value));
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="mb-8">
|
|
|
|
<Controller
|
|
|
|
name="slug"
|
|
|
|
control={newTeamFormMethods.control}
|
|
|
|
rules={{ required: t("team_url_required"), validate: async () => await validateTeamSlug() }}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<TextField
|
|
|
|
className="mt-2"
|
|
|
|
name="slug"
|
|
|
|
label={t("team_url")}
|
2022-11-25 12:59:25 +00:00
|
|
|
addOnLeading={`${process.env.NEXT_PUBLIC_WEBSITE_URL?.replace("https://", "")?.replace(
|
|
|
|
"http://",
|
|
|
|
""
|
2023-01-02 18:34:32 +00:00
|
|
|
)}/team/`}
|
2022-11-30 20:51:44 +00:00
|
|
|
defaultValue={value}
|
2022-11-10 20:23:56 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
newTeamFormMethods.setValue("slug", slugify(e?.target.value), {
|
|
|
|
shouldTouch: true,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="mb-8">
|
|
|
|
<Controller
|
|
|
|
control={newTeamFormMethods.control}
|
|
|
|
name="logo"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<Avatar alt="" imageSrc={value || null} gravatarFallbackMd5="newTeam" size="lg" />
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="ltr:ml-4 rtl:mr-4">
|
2022-11-10 20:23:56 +00:00
|
|
|
<ImageUploader
|
|
|
|
target="avatar"
|
|
|
|
id="avatar-upload"
|
|
|
|
buttonMsg={t("update")}
|
|
|
|
handleAvatarChange={(newAvatar: string) => {
|
|
|
|
newTeamFormMethods.setValue("logo", newAvatar);
|
|
|
|
}}
|
|
|
|
imageSrc={value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="flex space-x-2 rtl:space-x-reverse">
|
2022-11-12 13:31:11 +00:00
|
|
|
<Button
|
|
|
|
disabled={createTeamMutation.isLoading}
|
|
|
|
color="secondary"
|
2022-11-29 21:38:54 +00:00
|
|
|
href={returnToParam}
|
2022-11-12 13:31:11 +00:00
|
|
|
className="w-full justify-center">
|
2022-11-10 20:23:56 +00:00
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
2022-11-12 13:31:11 +00:00
|
|
|
<Button
|
|
|
|
disabled={createTeamMutation.isLoading}
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
2023-01-23 23:08:01 +00:00
|
|
|
EndIcon={FiArrowRight}
|
2022-11-12 13:31:11 +00:00
|
|
|
className="w-full justify-center">
|
2022-11-10 20:23:56 +00:00
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{createTeamMutation.isError && (
|
|
|
|
<p className="mt-4 text-red-700">{createTeamMutation.error.message}</p>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|