2022-09-12 22:04:33 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { Trans } from "next-i18next";
|
|
|
|
import { useMemo } from "react";
|
|
|
|
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";
|
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,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
import { PendingMember } from "../lib/types";
|
|
|
|
|
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 {
|
|
|
|
emailOrUsername: 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
|
|
|
|
|
|
|
export default function MemberInvitationModal(props: MemberInvitationModalProps) {
|
2022-11-10 20:23:56 +00:00
|
|
|
const { t } = useLocale();
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const options: MembershipRoleOption[] = useMemo(() => {
|
|
|
|
return [
|
|
|
|
{ value: "MEMBER", label: t("member") },
|
|
|
|
{ value: "ADMIN", label: t("admin") },
|
|
|
|
{ value: "OWNER", label: t("owner") },
|
|
|
|
];
|
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
|
|
|
|
open={props.isOpen}
|
|
|
|
onOpenChange={() => {
|
|
|
|
props.onExit();
|
|
|
|
newMemberFormMethods.reset();
|
|
|
|
}}>
|
2022-09-15 09:47:59 +00:00
|
|
|
<DialogContent
|
|
|
|
type="creation"
|
|
|
|
title={t("invite_new_member")}
|
|
|
|
description={
|
2022-11-10 20:23:56 +00:00
|
|
|
IS_TEAM_BILLING_ENABLED ? (
|
2022-11-25 12:59:25 +00:00
|
|
|
<span className="text-sm leading-tight text-gray-500">
|
2022-11-10 20:23:56 +00:00
|
|
|
<Trans i18nKey="invite_new_member_description">
|
|
|
|
Note: This will <span className="font-medium text-gray-900">cost an extra seat ($15/m)</span>{" "}
|
|
|
|
on your subscription.
|
|
|
|
</Trans>
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
""
|
|
|
|
)
|
2022-09-15 09:47:59 +00:00
|
|
|
}>
|
2022-11-10 20:23:56 +00:00
|
|
|
<Form form={newMemberFormMethods} handleSubmit={(values) => props.onSubmit(values)}>
|
2022-11-25 12:59:25 +00:00
|
|
|
<div className="mt-6 space-y-6">
|
2022-11-10 20:23:56 +00:00
|
|
|
<Controller
|
|
|
|
name="emailOrUsername"
|
|
|
|
control={newMemberFormMethods.control}
|
|
|
|
rules={{
|
|
|
|
required: t("enter_email_or_username"),
|
|
|
|
validate: (value) => 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={onChange}
|
|
|
|
/>
|
|
|
|
{error && <span className="text-sm text-red-800">{error.message}</span>}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<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>
|
2022-11-25 12:59:25 +00:00
|
|
|
<Label className="font-medium text-gray-900" htmlFor="role">
|
2022-11-10 20:23:56 +00:00
|
|
|
{t("role")}
|
2022-11-25 12:59:25 +00:00
|
|
|
</Label>
|
|
|
|
<ToggleGroup
|
|
|
|
isFullWidth={true}
|
2022-11-10 20:23:56 +00:00
|
|
|
id="role"
|
2022-11-25 12:59:25 +00:00
|
|
|
onValueChange={onChange}
|
|
|
|
defaultValue={options[0].value}
|
|
|
|
options={[
|
|
|
|
{ value: "ADMIN", label: t("admin") },
|
|
|
|
{ value: "MEMBER", label: t("member") },
|
|
|
|
]}
|
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>
|
|
|
|
<DialogFooter>
|
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-01-04 07:38:45 +00:00
|
|
|
className="ltr:ml-2 ltr:mr-2 rtl:ml-2"
|
2022-09-12 22:04:33 +00:00
|
|
|
data-testid="invite-new-member-button">
|
2023-01-31 18:16:43 +00:00
|
|
|
{t("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>
|
|
|
|
);
|
|
|
|
}
|