2022-11-10 20:23:56 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-04-03 14:37:58 +00:00
|
|
|
import { useState } from "react";
|
2022-11-10 20:23:56 +00:00
|
|
|
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-04-03 14:37:58 +00:00
|
|
|
import { Avatar, Button, Form, ImageUploader, TextField, Alert } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ArrowRight } from "@calcom/ui/components/icon";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
2023-03-07 23:02:05 +00:00
|
|
|
import type { NewTeamFormValues } from "../lib/types";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
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-04-03 14:37:58 +00:00
|
|
|
const [serverErrorMessage, setServerErrorMessage] = useState<string | null>(null);
|
2022-12-19 14:38:33 +00:00
|
|
|
|
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`);
|
|
|
|
},
|
2023-04-03 14:37:58 +00:00
|
|
|
onError: (err) => {
|
|
|
|
if (err.message === "team_url_taken") {
|
|
|
|
newTeamFormMethods.setError("slug", { type: "custom", message: t("team_url_taken") });
|
|
|
|
} else {
|
|
|
|
setServerErrorMessage(err.message);
|
|
|
|
}
|
|
|
|
},
|
2022-11-10 20:23:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-11-12 13:31:11 +00:00
|
|
|
<Form
|
|
|
|
form={newTeamFormMethods}
|
|
|
|
handleSubmit={(v) => {
|
2023-04-03 14:37:58 +00:00
|
|
|
if (!createTeamMutation.isLoading) {
|
|
|
|
setServerErrorMessage(null);
|
|
|
|
createTeamMutation.mutate(v);
|
|
|
|
}
|
2022-11-12 13:31:11 +00:00
|
|
|
}}>
|
2022-11-10 20:23:56 +00:00
|
|
|
<div className="mb-8">
|
2023-04-03 14:37:58 +00:00
|
|
|
{serverErrorMessage && (
|
|
|
|
<div className="mb-4">
|
|
|
|
<Alert severity="error" message={serverErrorMessage} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
<Controller
|
|
|
|
name="name"
|
|
|
|
control={newTeamFormMethods.control}
|
|
|
|
defaultValue=""
|
|
|
|
rules={{
|
|
|
|
required: t("must_enter_team_name"),
|
|
|
|
}}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<>
|
|
|
|
<TextField
|
|
|
|
className="mt-2"
|
2023-04-05 18:14:46 +00:00
|
|
|
placeholder="Acme Inc."
|
2022-11-10 20:23:56 +00:00
|
|
|
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}
|
2023-04-03 14:37:58 +00:00
|
|
|
rules={{ required: t("team_url_required") }}
|
2022-11-10 20:23:56 +00:00
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<TextField
|
|
|
|
className="mt-2"
|
|
|
|
name="slug"
|
2023-04-05 18:14:46 +00:00
|
|
|
placeholder="acme"
|
2022-11-10 20:23:56 +00:00
|
|
|
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,
|
|
|
|
});
|
2023-04-03 14:37:58 +00:00
|
|
|
newTeamFormMethods.clearErrors("slug");
|
2022-11-10 20:23:56 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</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-04-09 09:13:57 +00:00
|
|
|
<div className="ms-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);
|
2023-03-27 16:35:46 +00:00
|
|
|
createTeamMutation.reset();
|
2022-11-10 20:23:56 +00:00
|
|
|
}}
|
|
|
|
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
|
2023-04-03 14:37:58 +00:00
|
|
|
disabled={newTeamFormMethods.formState.isSubmitting || createTeamMutation.isLoading}
|
2022-11-12 13:31:11 +00:00
|
|
|
color="primary"
|
2023-04-12 15:26:31 +00:00
|
|
|
EndIcon={ArrowRight}
|
2023-03-07 23:02:05 +00:00
|
|
|
type="submit"
|
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>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|