2022-11-10 20:23:56 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
2023-08-02 09:35:48 +00:00
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2023-06-06 23:34:14 +00:00
|
|
|
import InviteLinkSettingsModal from "@calcom/features/ee/teams/components/InviteLinkSettingsModal";
|
2022-11-10 20:23:56 +00:00
|
|
|
import MemberInvitationModal from "@calcom/features/ee/teams/components/MemberInvitationModal";
|
|
|
|
import { classNames } from "@calcom/lib";
|
2023-06-06 23:34:14 +00:00
|
|
|
import { APP_NAME, WEBAPP_URL } from "@calcom/lib/constants";
|
2023-07-13 13:52:21 +00:00
|
|
|
import { useBookerUrl } from "@calcom/lib/hooks/useBookerUrl";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-04-01 20:12:48 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-06-06 23:34:14 +00:00
|
|
|
import {
|
|
|
|
Avatar,
|
|
|
|
Badge,
|
|
|
|
Button,
|
|
|
|
showToast,
|
|
|
|
SkeletonButton,
|
|
|
|
SkeletonContainer,
|
|
|
|
SkeletonText,
|
|
|
|
} from "@calcom/ui";
|
|
|
|
import { ArrowRight, Plus, Trash2 } from "@calcom/ui/components/icon";
|
2022-11-10 20:23:56 +00:00
|
|
|
|
|
|
|
const querySchema = z.object({
|
|
|
|
id: z.string().transform((val) => parseInt(val)),
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
type TeamMember = RouterOutputs["viewer"]["teams"]["get"]["members"][number];
|
2022-11-10 20:23:56 +00:00
|
|
|
|
|
|
|
type FormValues = {
|
|
|
|
members: TeamMember[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddNewTeamMembers = () => {
|
2023-08-02 09:35:48 +00:00
|
|
|
const searchParams = useSearchParams();
|
2022-11-10 20:23:56 +00:00
|
|
|
const session = useSession();
|
2023-08-02 09:35:48 +00:00
|
|
|
const teamId = searchParams?.get("id") ? Number(searchParams.get("id")) : -1;
|
|
|
|
const teamQuery = trpc.viewer.teams.get.useQuery(
|
|
|
|
{ teamId },
|
|
|
|
{ enabled: session.status === "authenticated" }
|
|
|
|
);
|
2022-11-10 20:23:56 +00:00
|
|
|
if (session.status === "loading" || !teamQuery.data) return <AddNewTeamMemberSkeleton />;
|
|
|
|
|
|
|
|
return <AddNewTeamMembersForm defaultValues={{ members: teamQuery.data.members }} teamId={teamId} />;
|
|
|
|
};
|
|
|
|
|
2023-01-21 15:32:00 +00:00
|
|
|
export const AddNewTeamMembersForm = ({
|
|
|
|
defaultValues,
|
|
|
|
teamId,
|
|
|
|
}: {
|
|
|
|
defaultValues: FormValues;
|
|
|
|
teamId: number;
|
|
|
|
}) => {
|
2023-08-02 09:35:48 +00:00
|
|
|
const searchParams = useSearchParams();
|
2022-11-10 20:23:56 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2023-06-06 23:34:14 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const router = useRouter();
|
2023-06-06 23:34:14 +00:00
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
2023-08-02 09:35:48 +00:00
|
|
|
const showDialog = searchParams?.get("inviteModal") === "true";
|
2023-05-24 01:01:31 +00:00
|
|
|
const [memberInviteModal, setMemberInviteModal] = useState(showDialog);
|
2023-06-06 23:34:14 +00:00
|
|
|
const [inviteLinkSettingsModal, setInviteLinkSettingsModal] = useState(false);
|
|
|
|
|
2023-08-02 09:35:48 +00:00
|
|
|
const { data: team, isLoading } = trpc.viewer.teams.get.useQuery({ teamId }, { enabled: !!teamId });
|
2023-06-06 23:34:14 +00:00
|
|
|
|
2023-06-16 12:45:20 +00:00
|
|
|
const inviteMemberMutation = trpc.viewer.teams.inviteMember.useMutation();
|
2023-06-06 23:34:14 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const publishTeamMutation = trpc.viewer.teams.publish.useMutation({
|
2022-11-10 20:23:56 +00:00
|
|
|
onSuccess(data) {
|
|
|
|
router.push(data.url);
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
showToast(error.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<ul className="border-subtle rounded-md border" data-testid="pending-member-list">
|
2022-11-10 20:23:56 +00:00
|
|
|
{defaultValues.members.map((member, index) => (
|
|
|
|
<PendingMemberItem key={member.email} member={member} index={index} teamId={teamId} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
data-testid="new-member-button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Plus}
|
2022-11-10 20:23:56 +00:00
|
|
|
onClick={() => setMemberInviteModal(true)}
|
|
|
|
className="mt-6 w-full justify-center">
|
|
|
|
{t("add_team_member")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-06-06 23:34:14 +00:00
|
|
|
{isLoading ? (
|
|
|
|
<SkeletonButton />
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<MemberInvitationModal
|
2023-07-06 09:33:31 +00:00
|
|
|
isLoading={inviteMemberMutation.isLoading}
|
2023-06-06 23:34:14 +00:00
|
|
|
isOpen={memberInviteModal}
|
|
|
|
teamId={teamId}
|
|
|
|
token={team?.inviteToken?.token}
|
|
|
|
onExit={() => setMemberInviteModal(false)}
|
2023-06-16 12:45:20 +00:00
|
|
|
onSubmit={(values, resetFields) => {
|
|
|
|
inviteMemberMutation.mutate(
|
|
|
|
{
|
|
|
|
teamId,
|
|
|
|
language: i18n.language,
|
|
|
|
role: values.role,
|
|
|
|
usernameOrEmail: values.emailOrUsername,
|
|
|
|
sendEmailInvitation: values.sendInviteEmail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onSuccess: async (data) => {
|
|
|
|
await utils.viewer.teams.get.invalidate();
|
|
|
|
setMemberInviteModal(false);
|
|
|
|
if (data.sendEmailInvitation) {
|
|
|
|
if (Array.isArray(data.usernameOrEmail)) {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team_bulk", {
|
|
|
|
userCount: data.usernameOrEmail.length,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
resetFields();
|
|
|
|
} else {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team", {
|
|
|
|
email: data.usernameOrEmail,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
showToast(error.message, "error");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2023-06-06 23:34:14 +00:00
|
|
|
}}
|
|
|
|
onSettingsOpen={() => {
|
|
|
|
setMemberInviteModal(false);
|
|
|
|
setInviteLinkSettingsModal(true);
|
|
|
|
}}
|
|
|
|
members={defaultValues.members}
|
|
|
|
/>
|
|
|
|
{team?.inviteToken && (
|
|
|
|
<InviteLinkSettingsModal
|
|
|
|
isOpen={inviteLinkSettingsModal}
|
|
|
|
teamId={team.id}
|
|
|
|
token={team.inviteToken?.token}
|
|
|
|
expiresInDays={team.inviteToken?.expiresInDays || undefined}
|
|
|
|
onExit={() => {
|
|
|
|
setInviteLinkSettingsModal(false);
|
|
|
|
setMemberInviteModal(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle my-6" />
|
2022-11-10 20:23:56 +00:00
|
|
|
<Button
|
2023-04-12 15:26:31 +00:00
|
|
|
EndIcon={ArrowRight}
|
2023-04-20 22:33:49 +00:00
|
|
|
color="primary"
|
2022-11-10 20:23:56 +00:00
|
|
|
className="mt-6 w-full justify-center"
|
|
|
|
disabled={publishTeamMutation.isLoading}
|
|
|
|
onClick={() => {
|
|
|
|
publishTeamMutation.mutate({ teamId });
|
|
|
|
}}>
|
|
|
|
{t("team_publish")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AddNewTeamMembers;
|
|
|
|
|
|
|
|
const AddNewTeamMemberSkeleton = () => {
|
|
|
|
return (
|
2023-04-20 22:33:49 +00:00
|
|
|
<SkeletonContainer className="border-subtle rounded-md border">
|
2022-11-10 20:23:56 +00:00
|
|
|
<div className="flex w-full justify-between p-4">
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-emphasis text-sm font-medium">
|
2022-11-10 20:23:56 +00:00
|
|
|
<SkeletonText className="h-4 w-56" />
|
|
|
|
</p>
|
|
|
|
<div className="mt-2.5 w-max">
|
|
|
|
<SkeletonText className="h-5 w-28" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</SkeletonContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PendingMemberItem = (props: { member: TeamMember; index: number; teamId: number }) => {
|
|
|
|
const { member, index, teamId } = props;
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2023-07-13 13:52:21 +00:00
|
|
|
const bookerUrl = useBookerUrl();
|
2022-11-10 23:40:01 +00:00
|
|
|
const removeMemberMutation = trpc.viewer.teams.removeMember.useMutation({
|
2022-11-10 20:23:56 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
2023-04-13 02:10:23 +00:00
|
|
|
await utils.viewer.eventTypes.invalidate();
|
2022-11-10 20:23:56 +00:00
|
|
|
showToast("Member removed", "success");
|
|
|
|
},
|
|
|
|
async onError(err) {
|
|
|
|
showToast(err.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={member.email}
|
2023-08-07 11:32:35 +00:00
|
|
|
className={classNames(
|
|
|
|
"flex items-center justify-between p-6 text-sm",
|
|
|
|
index !== 0 && "border-subtle border-t"
|
|
|
|
)}
|
2022-11-10 20:23:56 +00:00
|
|
|
data-testid="pending-member-item">
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="flex space-x-2 rtl:space-x-reverse">
|
2022-11-10 20:23:56 +00:00
|
|
|
<Avatar
|
|
|
|
gravatarFallbackMd5="teamMember"
|
|
|
|
size="mdLg"
|
2023-07-13 13:52:21 +00:00
|
|
|
imageSrc={bookerUrl + "/" + member.username + "/avatar.png"}
|
2022-11-10 20:23:56 +00:00
|
|
|
alt="owner-avatar"
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<div className="flex space-x-1">
|
|
|
|
<p>{member.name || member.email || t("team_member")}</p>
|
|
|
|
{/* Assume that the first member of the team is the creator */}
|
|
|
|
{index === 0 && <Badge variant="green">{t("you")}</Badge>}
|
|
|
|
{!member.accepted && <Badge variant="orange">{t("pending")}</Badge>}
|
|
|
|
{member.role === "MEMBER" && <Badge variant="gray">{t("member")}</Badge>}
|
|
|
|
{member.role === "ADMIN" && <Badge variant="default">{t("admin")}</Badge>}
|
|
|
|
</div>
|
|
|
|
{member.username ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default">{`${WEBAPP_URL}/${member.username}`}</p>
|
2022-11-10 20:23:56 +00:00
|
|
|
) : (
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default">{t("not_on_cal", { appName: APP_NAME })}</p>
|
2022-11-10 20:23:56 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{member.role !== "OWNER" && (
|
|
|
|
<Button
|
|
|
|
data-testid="remove-member-button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash2}
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2022-11-10 20:23:56 +00:00
|
|
|
color="secondary"
|
|
|
|
className="h-[36px] w-[36px]"
|
|
|
|
onClick={() => {
|
|
|
|
removeMemberMutation.mutate({ teamId, memberId: member.id });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|