2023-07-19 19:45:13 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import type { Dispatch } from "react";
|
|
|
|
|
|
|
|
import MemberInvitationModal from "@calcom/features/ee/teams/components/MemberInvitationModal";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc";
|
|
|
|
import { showToast } from "@calcom/ui";
|
|
|
|
|
|
|
|
import type { Action } from "./UserListTable";
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
dispatch: Dispatch<Action>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function InviteMemberModal(props: Props) {
|
|
|
|
const { data: session } = useSession();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const { t, i18n } = useLocale();
|
|
|
|
const inviteMemberMutation = trpc.viewer.teams.inviteMember.useMutation({
|
|
|
|
async onSuccess(data) {
|
|
|
|
props.dispatch({ type: "CLOSE_MODAL" });
|
|
|
|
// Need to figure out if invalidating here is the right approach - we could have already
|
|
|
|
// loaded a bunch of data and idk how pagination works with invalidation. We may need to use
|
|
|
|
// Optimistic updates here instead.
|
|
|
|
await utils.viewer.organizations.listMembers.invalidate();
|
|
|
|
if (data.sendEmailInvitation) {
|
|
|
|
if (Array.isArray(data.usernameOrEmail)) {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team_bulk", {
|
|
|
|
userCount: data.usernameOrEmail.length,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team", {
|
|
|
|
email: data.usernameOrEmail,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
showToast(error.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-09-08 00:21:04 +00:00
|
|
|
if (!session?.user.org?.id) return null;
|
2023-07-19 19:45:13 +00:00
|
|
|
|
2023-09-08 00:21:04 +00:00
|
|
|
const orgId = session.user.org.id;
|
2023-07-19 19:45:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MemberInvitationModal
|
|
|
|
members={[]}
|
|
|
|
isOpen={true}
|
|
|
|
onExit={() => {
|
|
|
|
props.dispatch({
|
|
|
|
type: "CLOSE_MODAL",
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
teamId={orgId}
|
2023-09-19 10:16:33 +00:00
|
|
|
isOrg={true}
|
2023-09-01 12:01:45 +00:00
|
|
|
justEmailInvites={!!orgId}
|
2023-07-19 19:45:13 +00:00
|
|
|
isLoading={inviteMemberMutation.isLoading}
|
|
|
|
onSubmit={(values) => {
|
|
|
|
inviteMemberMutation.mutate({
|
|
|
|
teamId: orgId,
|
|
|
|
language: i18n.language,
|
|
|
|
role: values.role,
|
|
|
|
usernameOrEmail: values.emailOrUsername,
|
|
|
|
sendEmailInvitation: values.sendInviteEmail,
|
|
|
|
isOrg: true,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|