2021-12-09 23:51:30 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
|
|
|
import { useState } from "react";
|
2022-04-14 14:58:23 +00:00
|
|
|
import React, { SyntheticEvent, useEffect } 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-03-16 23:36:43 +00:00
|
|
|
import Button from "@calcom/ui/Button";
|
|
|
|
|
2021-12-09 23:51:30 +00:00
|
|
|
import { trpc } from "@lib/trpc";
|
|
|
|
|
|
|
|
import ModalContainer from "@components/ui/ModalContainer";
|
2022-04-14 14:58:23 +00:00
|
|
|
import Select from "@components/ui/form/Select";
|
|
|
|
|
|
|
|
type MembershipRoleOption = {
|
|
|
|
value: MembershipRole;
|
|
|
|
label?: string;
|
|
|
|
};
|
|
|
|
|
2022-03-14 15:34:05 +00:00
|
|
|
const options: MembershipRoleOption[] = [{ value: "MEMBER" }, { value: "ADMIN" }, { value: "OWNER" }];
|
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-04-14 14:58:23 +00:00
|
|
|
useEffect(() => {
|
|
|
|
options.forEach((option, i) => {
|
|
|
|
options[i].label = t(option.value.toLowerCase());
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const [role, setRole] = useState(
|
|
|
|
options.find((option) => option.value === props.initialRole || MembershipRole.MEMBER)!
|
|
|
|
);
|
2021-12-09 23:51:30 +00:00
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
const { t } = useLocale();
|
|
|
|
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-04-28 15:51:10 +00:00
|
|
|
options={props.currentMember !== MembershipRole.OWNER ? options.slice(0, 2) : 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-04-14 14:58:23 +00:00
|
|
|
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm"
|
|
|
|
/>
|
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>
|
|
|
|
);
|
|
|
|
}
|