import { MembershipRole } from "@prisma/client"; import { SyntheticEvent, useMemo, useState } from "react"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import { Button } from "@calcom/ui/components"; import { Dialog, DialogContent, Select } from "@calcom/ui/v2"; type MembershipRoleOption = { label: string; value: MembershipRole; }; export default function MemberChangeRoleModal(props: { isOpen: boolean; currentMember: MembershipRole; memberId: number; teamId: number; initialRole: MembershipRole; onExit: () => void; }) { const { t } = useLocale(); const options = useMemo(() => { return [ { label: t("member"), value: MembershipRole.MEMBER, }, { label: t("admin"), value: MembershipRole.ADMIN, }, { label: t("owner"), value: MembershipRole.OWNER, }, ].filter(({ value }) => value !== MembershipRole.OWNER || props.currentMember === MembershipRole.OWNER); }, [t, props.currentMember]); const [role, setRole] = useState( options.find((option) => option.value === props.initialRole) || { label: t("member"), value: MembershipRole.MEMBER, } ); const [errorMessage, setErrorMessage] = useState(""); const utils = trpc.useContext(); const changeRoleMutation = trpc.useMutation("viewer.teams.changeMemberRole", { async onSuccess() { await utils.invalidateQueries(["viewer.teams.get"]); props.onExit(); }, async onError(err) { setErrorMessage(err.message); }, }); function changeRole(e: SyntheticEvent) { e.preventDefault(); changeRoleMutation.mutate({ teamId: props.teamId, memberId: props.memberId, role: role.value, }); } return ( <>
{/* - needs dialog to confirm change of ownership */}