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
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import slugify from "@calcom/lib/slugify";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Avatar, Button, Form, Icon, ImageUploader, TextField } from "@calcom/ui";
|
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();
|
2022-11-29 21:38:54 +00:00
|
|
|
|
2022-12-19 14:38:33 +00:00
|
|
|
const returnToParsed = querySchema.safeParse(router.query);
|
|
|
|
|
|
|
|
const returnToParam = returnToParsed.success ? returnToParsed.data.returnTo : "/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) => {
|
|
|
|
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://",
|
|
|
|
""
|
|
|
|
)}/`}
|
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" />
|
|
|
|
<div className="ml-4">
|
|
|
|
<ImageUploader
|
|
|
|
target="avatar"
|
|
|
|
id="avatar-upload"
|
|
|
|
buttonMsg={t("update")}
|
|
|
|
handleAvatarChange={(newAvatar: string) => {
|
|
|
|
newTeamFormMethods.setValue("logo", newAvatar);
|
|
|
|
}}
|
|
|
|
imageSrc={value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex space-x-2">
|
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"
|
|
|
|
EndIcon={Icon.FiArrowRight}
|
|
|
|
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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|