2022-09-12 22:04:33 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, Meta, showToast } from "@calcom/ui";
|
|
|
|
import { FiPlus } from "@calcom/ui/components/icon";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-01-05 17:00:16 +00:00
|
|
|
import { getLayout } from "../../../settings/layouts/SettingsLayout";
|
2022-09-12 22:04:33 +00:00
|
|
|
import DisableTeamImpersonation from "../components/DisableTeamImpersonation";
|
|
|
|
import MemberInvitationModal from "../components/MemberInvitationModal";
|
|
|
|
import MemberListItem from "../components/MemberListItem";
|
|
|
|
import TeamInviteList from "../components/TeamInviteList";
|
|
|
|
|
|
|
|
const MembersView = () => {
|
2022-11-10 20:23:56 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2022-09-12 22:04:33 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const session = useSession();
|
2022-11-10 20:23:56 +00:00
|
|
|
const utils = trpc.useContext();
|
|
|
|
const [showMemberInvitationModal, setShowMemberInvitationModal] = useState(false);
|
|
|
|
const teamId = Number(router.query.id);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: team, isLoading } = trpc.viewer.teams.get.useQuery(
|
|
|
|
{ teamId },
|
|
|
|
{
|
|
|
|
onError: () => {
|
|
|
|
router.push("/settings");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const inviteMemberMutation = trpc.viewer.teams.inviteMember.useMutation({
|
2023-04-01 20:12:48 +00:00
|
|
|
async onSuccess(data) {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
2022-11-10 20:23:56 +00:00
|
|
|
setShowMemberInvitationModal(false);
|
2023-04-01 20:12:48 +00:00
|
|
|
if (data.sendEmailInvitation) {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team", {
|
|
|
|
email: data.usernameOrEmail,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
}
|
2022-11-10 20:23:56 +00:00
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
showToast(error.message, "error");
|
|
|
|
},
|
|
|
|
});
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
const isInviteOpen = !team?.membership.accepted;
|
|
|
|
|
|
|
|
const isAdmin =
|
|
|
|
team && (team.membership.role === MembershipRole.OWNER || team.membership.role === MembershipRole.ADMIN);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-19 17:53:46 +00:00
|
|
|
<Meta
|
|
|
|
title={t("team_members")}
|
|
|
|
description={t("members_team_description")}
|
|
|
|
CTA={
|
|
|
|
isAdmin ? (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="primary"
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiPlus}
|
2022-12-19 17:53:46 +00:00
|
|
|
className="ml-auto"
|
|
|
|
onClick={() => setShowMemberInvitationModal(true)}
|
|
|
|
data-testid="new-member-button">
|
|
|
|
{t("add")}
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2022-09-12 22:04:33 +00:00
|
|
|
{!isLoading && (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
{team && (
|
|
|
|
<>
|
|
|
|
{isInviteOpen && (
|
|
|
|
<TeamInviteList
|
|
|
|
teams={[
|
|
|
|
{
|
|
|
|
id: team.id,
|
|
|
|
accepted: team.membership.accepted || false,
|
|
|
|
logo: team.logo,
|
|
|
|
name: team.name,
|
|
|
|
slug: team.slug,
|
|
|
|
role: team.membership.role,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<div>
|
|
|
|
<ul className="divide-y divide-gray-200 rounded-md border ">
|
|
|
|
{team?.members.map((member) => {
|
|
|
|
return <MemberListItem key={member.id} team={team} member={member} />;
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<hr className="my-8 border-gray-200" />
|
|
|
|
|
|
|
|
{team && session.data && (
|
|
|
|
<DisableTeamImpersonation
|
|
|
|
teamId={team.id}
|
|
|
|
memberId={session.data.user.id}
|
|
|
|
disabled={isInviteOpen}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<hr className="my-8 border-gray-200" />
|
|
|
|
</div>
|
|
|
|
{showMemberInvitationModal && team && (
|
|
|
|
<MemberInvitationModal
|
|
|
|
isOpen={showMemberInvitationModal}
|
2022-11-10 20:23:56 +00:00
|
|
|
members={team.members}
|
2022-09-12 22:04:33 +00:00
|
|
|
onExit={() => setShowMemberInvitationModal(false)}
|
2022-11-10 20:23:56 +00:00
|
|
|
onSubmit={(values) => {
|
|
|
|
inviteMemberMutation.mutate({
|
|
|
|
teamId,
|
|
|
|
language: i18n.language,
|
2022-11-25 12:59:25 +00:00
|
|
|
role: values.role,
|
2022-11-10 20:23:56 +00:00
|
|
|
usernameOrEmail: values.emailOrUsername,
|
|
|
|
sendEmailInvitation: values.sendInviteEmail,
|
|
|
|
});
|
|
|
|
}}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
MembersView.getLayout = getLayout;
|
|
|
|
|
|
|
|
export default MembersView;
|