2021-12-09 23:51:30 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
2022-06-06 18:24:37 +00:00
|
|
|
import { SyntheticEvent, useMemo, useState } from "react";
|
2021-12-09 23:51:30 +00:00
|
|
|
|
2022-03-17 13:20:49 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-03-16 23:36:43 +00:00
|
|
|
import Button from "@calcom/ui/Button";
|
|
|
|
|
2021-12-09 23:51:30 +00:00
|
|
|
import ModalContainer from "@components/ui/ModalContainer";
|
2022-04-14 14:58:23 +00:00
|
|
|
import Select from "@components/ui/form/Select";
|
|
|
|
|
|
|
|
type MembershipRoleOption = {
|
2022-06-06 18:24:37 +00:00
|
|
|
label: string;
|
2022-04-14 14:58:23 +00:00
|
|
|
value: MembershipRole;
|
|
|
|
};
|
|
|
|
|
2021-12-09 23:51:30 +00:00
|
|
|
export default function MemberChangeRoleModal(props: {
|
2022-03-17 13:20:49 +00:00
|
|
|
isOpen: boolean;
|
2022-04-28 15:51:10 +00:00
|
|
|
currentMember: MembershipRole;
|
2021-12-09 23:51:30 +00:00
|
|
|
memberId: number;
|
|
|
|
teamId: number;
|
|
|
|
initialRole: MembershipRole;
|
|
|
|
onExit: () => void;
|
|
|
|
}) {
|
2022-06-06 18:24:37 +00:00
|
|
|
const { t } = useLocale();
|
2022-04-14 14:58:23 +00:00
|
|
|
|
2022-06-06 18:24:37 +00:00
|
|
|
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<MembershipRoleOption>(
|
|
|
|
options.find((option) => option.value === props.initialRole) || {
|
|
|
|
label: t("member"),
|
|
|
|
value: MembershipRole.MEMBER,
|
|
|
|
}
|
2022-04-14 14:58:23 +00:00
|
|
|
);
|
2021-12-09 23:51:30 +00:00
|
|
|
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,
|
2022-04-14 14:58:23 +00:00
|
|
|
role: role.value,
|
2021-12-09 23:51:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
2022-03-17 13:20:49 +00:00
|
|
|
<ModalContainer isOpen={props.isOpen} onExit={props.onExit}>
|
2021-12-09 23:51:30 +00:00
|
|
|
<>
|
|
|
|
<div className="mb-4 sm:flex sm:items-start">
|
|
|
|
<div className="text-center sm:text-left">
|
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900" id="modal-title">
|
|
|
|
{t("change_member_role")}
|
|
|
|
</h3>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<form onSubmit={changeRole}>
|
|
|
|
<div className="mb-4">
|
2022-02-09 00:05:13 +00:00
|
|
|
<label className="mb-2 block text-sm font-medium tracking-wide text-gray-700" htmlFor="role">
|
2021-12-09 23:51:30 +00:00
|
|
|
{t("role")}
|
|
|
|
</label>
|
2022-04-14 14:58:23 +00:00
|
|
|
{/*<option value="OWNER">{t("owner")}</option> - needs dialog to confirm change of ownership */}
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-06-06 18:24:37 +00:00
|
|
|
options={options}
|
2021-12-09 23:51:30 +00:00
|
|
|
value={role}
|
2022-04-14 14:58:23 +00:00
|
|
|
onChange={(option) => option && setRole(option)}
|
2021-12-09 23:51:30 +00:00
|
|
|
id="role"
|
2022-07-27 02:24:00 +00:00
|
|
|
className="mt-1 block w-full rounded-md border-gray-300 text-sm"
|
2022-04-14 14:58:23 +00:00
|
|
|
/>
|
2021-12-09 23:51:30 +00:00
|
|
|
</div>
|
|
|
|
{errorMessage && (
|
|
|
|
<p className="text-sm text-red-700">
|
|
|
|
<span className="font-bold">Error: </span>
|
|
|
|
{errorMessage}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
|
2022-02-01 22:17:37 +00:00
|
|
|
<Button type="submit" color="primary" className="ltr:ml-2 rtl:mr-2">
|
2021-12-09 23:51:30 +00:00
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
<Button type="button" color="secondary" onClick={props.onExit}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
</ModalContainer>
|
|
|
|
);
|
|
|
|
}
|