2023-05-24 01:01:31 +00:00
|
|
|
import { PaperclipIcon, UserIcon, Users } from "lucide-react";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { Trans } from "next-i18next";
|
2023-05-24 01:01:31 +00:00
|
|
|
import { useMemo, useState } from "react";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-05-24 01:01:31 +00:00
|
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
2023-01-17 19:10:47 +00:00
|
|
|
Checkbox as CheckboxField,
|
2022-11-23 02:55:25 +00:00
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
Form,
|
|
|
|
TextField,
|
2022-11-25 12:59:25 +00:00
|
|
|
Label,
|
|
|
|
ToggleGroup,
|
2023-05-24 01:01:31 +00:00
|
|
|
Select,
|
|
|
|
TextAreaField,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-04-05 18:14:46 +00:00
|
|
|
import type { PendingMember } from "../lib/types";
|
2023-05-24 01:01:31 +00:00
|
|
|
import { GoogleWorkspaceInviteButton } from "./GoogleWorkspaceInviteButton";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
type MemberInvitationModalProps = {
|
|
|
|
isOpen: boolean;
|
|
|
|
onExit: () => void;
|
2022-11-10 20:23:56 +00:00
|
|
|
onSubmit: (values: NewMemberForm) => void;
|
|
|
|
members: PendingMember[];
|
2022-09-12 22:04:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type MembershipRoleOption = {
|
|
|
|
value: MembershipRole;
|
2022-11-10 20:23:56 +00:00
|
|
|
label: string;
|
2022-09-12 22:04:33 +00:00
|
|
|
};
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
export interface NewMemberForm {
|
2023-05-24 01:01:31 +00:00
|
|
|
emailOrUsername: string | string[];
|
2022-11-25 12:59:25 +00:00
|
|
|
role: MembershipRole;
|
2022-11-10 20:23:56 +00:00
|
|
|
sendInviteEmail: boolean;
|
|
|
|
}
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-05-24 01:01:31 +00:00
|
|
|
type ModalMode = "INDIVIDUAL" | "BULK";
|
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
export default function MemberInvitationModal(props: MemberInvitationModalProps) {
|
2022-11-10 20:23:56 +00:00
|
|
|
const { t } = useLocale();
|
2023-05-24 01:01:31 +00:00
|
|
|
const [modalImportMode, setModalInputMode] = useState<ModalMode>("INDIVIDUAL");
|
2022-11-10 20:23:56 +00:00
|
|
|
const options: MembershipRoleOption[] = useMemo(() => {
|
|
|
|
return [
|
2023-05-24 01:01:31 +00:00
|
|
|
{ value: MembershipRole.MEMBER, label: t("member") },
|
|
|
|
{ value: MembershipRole.ADMIN, label: t("admin") },
|
|
|
|
{ value: MembershipRole.OWNER, label: t("owner") },
|
2022-11-10 20:23:56 +00:00
|
|
|
];
|
2022-09-12 22:04:33 +00:00
|
|
|
}, [t]);
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const newMemberFormMethods = useForm<NewMemberForm>();
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const validateUniqueInvite = (value: string) => {
|
|
|
|
return !(
|
|
|
|
props.members.some((member) => member?.username === value) ||
|
|
|
|
props.members.some((member) => member?.email === value)
|
|
|
|
);
|
|
|
|
};
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
return (
|
2022-11-10 20:23:56 +00:00
|
|
|
<Dialog
|
2023-05-24 01:01:31 +00:00
|
|
|
name="inviteModal"
|
2022-11-10 20:23:56 +00:00
|
|
|
open={props.isOpen}
|
|
|
|
onOpenChange={() => {
|
|
|
|
props.onExit();
|
|
|
|
newMemberFormMethods.reset();
|
|
|
|
}}>
|
2022-09-15 09:47:59 +00:00
|
|
|
<DialogContent
|
|
|
|
type="creation"
|
2023-05-24 01:01:31 +00:00
|
|
|
title={t("invite_team_member")}
|
2022-09-15 09:47:59 +00:00
|
|
|
description={
|
2022-11-10 20:23:56 +00:00
|
|
|
IS_TEAM_BILLING_ENABLED ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-subtle text-sm leading-tight">
|
2022-11-10 20:23:56 +00:00
|
|
|
<Trans i18nKey="invite_new_member_description">
|
2023-04-05 18:14:46 +00:00
|
|
|
Note: This will <span className="text-emphasis font-medium">cost an extra seat ($15/m)</span>{" "}
|
2022-11-10 20:23:56 +00:00
|
|
|
on your subscription.
|
|
|
|
</Trans>
|
|
|
|
</span>
|
2023-05-24 01:01:31 +00:00
|
|
|
) : null
|
2022-09-15 09:47:59 +00:00
|
|
|
}>
|
2023-05-24 01:01:31 +00:00
|
|
|
<div>
|
|
|
|
<Label className="sr-only" htmlFor="role">
|
|
|
|
{t("import_mode")}
|
|
|
|
</Label>
|
|
|
|
<ToggleGroup
|
|
|
|
isFullWidth={true}
|
|
|
|
onValueChange={(val) => setModalInputMode(val as ModalMode)}
|
|
|
|
defaultValue="INDIVIDUAL"
|
|
|
|
options={[
|
|
|
|
{
|
|
|
|
value: "INDIVIDUAL",
|
|
|
|
label: t("invite_team_individual_segment"),
|
|
|
|
iconLeft: <UserIcon />,
|
|
|
|
},
|
|
|
|
{ value: "BULK", label: t("invite_team_bulk_segment"), iconLeft: <Users /> },
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
<Form form={newMemberFormMethods} handleSubmit={(values) => props.onSubmit(values)}>
|
2023-05-24 01:01:31 +00:00
|
|
|
<div className="space-y-6">
|
|
|
|
{/* Indivdual Invite */}
|
|
|
|
{modalImportMode === "INDIVIDUAL" && (
|
|
|
|
<Controller
|
|
|
|
name="emailOrUsername"
|
|
|
|
control={newMemberFormMethods.control}
|
|
|
|
rules={{
|
|
|
|
required: t("enter_email_or_username"),
|
|
|
|
validate: (value) => {
|
|
|
|
if (typeof value === "string")
|
|
|
|
return validateUniqueInvite(value) || t("member_already_invited");
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
render={({ field: { onChange }, fieldState: { error } }) => (
|
|
|
|
<>
|
|
|
|
<TextField
|
|
|
|
label={t("email_or_username")}
|
|
|
|
id="inviteUser"
|
|
|
|
name="inviteUser"
|
|
|
|
placeholder="email@example.com"
|
|
|
|
required
|
|
|
|
onChange={(e) => onChange(e.target.value.trim().toLowerCase())}
|
|
|
|
/>
|
|
|
|
{error && <span className="text-sm text-red-800">{error.message}</span>}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{/* Bulk Invite */}
|
|
|
|
{modalImportMode === "BULK" && (
|
|
|
|
<div className="bg-muted flex flex-col rounded-md p-4">
|
|
|
|
<Controller
|
|
|
|
name="emailOrUsername"
|
|
|
|
control={newMemberFormMethods.control}
|
|
|
|
rules={{
|
|
|
|
required: t("enter_email_or_username"),
|
|
|
|
}}
|
|
|
|
render={({ field: { onChange, value }, fieldState: { error } }) => (
|
|
|
|
<>
|
|
|
|
{/* TODO: Make this a fancy email input that styles on a successful email. */}
|
|
|
|
<TextAreaField
|
|
|
|
name="emails"
|
|
|
|
label="Invite via email"
|
|
|
|
rows={4}
|
|
|
|
autoCorrect="off"
|
|
|
|
placeholder="john@doe.com, alex@smith.com"
|
|
|
|
required
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => {
|
|
|
|
const emails = e.target.value
|
|
|
|
.split(",")
|
|
|
|
.map((email) => email.trim().toLocaleLowerCase());
|
|
|
|
|
|
|
|
return onChange(emails);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{error && <span className="text-sm text-red-800">{error.message}</span>}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<GoogleWorkspaceInviteButton
|
|
|
|
onSuccess={(data) => {
|
|
|
|
newMemberFormMethods.setValue("emailOrUsername", data);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
disabled
|
|
|
|
type="button"
|
|
|
|
color="secondary"
|
|
|
|
StartIcon={PaperclipIcon}
|
|
|
|
className="mt-3 justify-center stroke-2">
|
|
|
|
Upload a .csv file
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-11-10 20:23:56 +00:00
|
|
|
<Controller
|
|
|
|
name="role"
|
|
|
|
control={newMemberFormMethods.control}
|
2022-11-25 12:59:25 +00:00
|
|
|
defaultValue={options[0].value}
|
2022-11-10 20:23:56 +00:00
|
|
|
render={({ field: { onChange } }) => (
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<Label className="text-emphasis font-medium" htmlFor="role">
|
2023-05-24 01:01:31 +00:00
|
|
|
{t("invite_as")}
|
2022-11-25 12:59:25 +00:00
|
|
|
</Label>
|
2023-05-24 01:01:31 +00:00
|
|
|
<Select
|
2022-11-10 20:23:56 +00:00
|
|
|
id="role"
|
2023-05-24 01:01:31 +00:00
|
|
|
defaultValue={options[0]}
|
|
|
|
options={options}
|
|
|
|
onChange={(val) => {
|
|
|
|
if (val) onChange(val.value);
|
|
|
|
}}
|
2022-11-10 20:23:56 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
name="sendInviteEmail"
|
|
|
|
control={newMemberFormMethods.control}
|
2022-11-25 12:59:25 +00:00
|
|
|
defaultValue={true}
|
2022-11-10 20:23:56 +00:00
|
|
|
render={() => (
|
2022-11-25 12:59:25 +00:00
|
|
|
<CheckboxField
|
|
|
|
className="mr-0"
|
|
|
|
defaultChecked={true}
|
|
|
|
description={t("send_invite_email")}
|
|
|
|
onChange={(e) => newMemberFormMethods.setValue("sendInviteEmail", e.target.checked)}
|
|
|
|
/>
|
2022-11-10 20:23:56 +00:00
|
|
|
)}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-05-24 01:01:31 +00:00
|
|
|
<DialogFooter showDivider>
|
2022-11-10 20:23:56 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
2022-11-25 12:59:25 +00:00
|
|
|
color="minimal"
|
2022-11-10 20:23:56 +00:00
|
|
|
onClick={() => {
|
|
|
|
props.onExit();
|
|
|
|
newMemberFormMethods.reset();
|
|
|
|
}}>
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
color="primary"
|
2023-04-09 09:13:57 +00:00
|
|
|
className="ms-2 me-2"
|
2022-09-12 22:04:33 +00:00
|
|
|
data-testid="invite-new-member-button">
|
2023-05-24 01:01:31 +00:00
|
|
|
{t("send_invite")}
|
2022-09-12 22:04:33 +00:00
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
2022-11-10 20:23:56 +00:00
|
|
|
</Form>
|
2022-09-12 22:04:33 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|