Files
calendar/packages/features/users/components/UserTable/InviteMemberModal.tsx
T
5726d18027 feat: member management for platform (#16803)
* init members page for platform

* add missing icon

* add members tab to sidebar

* use data from useMe query instead of useSession

* return user profiles since platform users are stored there

* small refactor

* fixup

* remove unused logs from console

* fixup

* resolve merge conflicts

* add missing tabs for members and billing

* update schema

* fixup

* memoise selection options for org and platform

* update onSubmit handler

* resolve merge conflicts

* add members tab

* improve team billing view when user not subscribed

* update members tab

* add function to find if user is admin of team

* update invite members handler to handle platform user as well

---------

Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
2024-10-14 21:19:02 +03:00

78 lines
2.2 KiB
TypeScript

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 usePlatformMe from "@calcom/web/components/settings/platform/hooks/usePlatformMe";
import type { Action } from "./UserListTable";
interface Props {
dispatch: Dispatch<Action>;
}
export function InviteMemberModal(props: Props) {
const { data: session } = useSession();
const { data: platformUser } = usePlatformMe();
const utils = trpc.useUtils();
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 (Array.isArray(data.usernameOrEmail)) {
showToast(
t("email_invite_team_bulk", {
userCount: data.numUsersInvited,
}),
"success"
);
} else {
showToast(
t("email_invite_team", {
email: data.usernameOrEmail,
}),
"success"
);
}
},
onError: (error) => {
showToast(error.message, "error");
},
});
const orgId = session?.user.org?.id ?? platformUser?.organizationId;
if (!orgId) return null;
return (
<MemberInvitationModal
members={[]}
isOpen={true}
onExit={() => {
props.dispatch({
type: "CLOSE_MODAL",
});
}}
teamId={orgId}
isOrg={true}
isPending={inviteMemberMutation.isPending}
onSubmit={(values) => {
inviteMemberMutation.mutate({
teamId: orgId,
language: i18n.language,
role: values.role,
usernameOrEmail: values.emailOrUsername,
isPlatform: platformUser?.organization.isPlatform,
});
}}
/>
);
}