2022-09-12 22:04:33 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
2023-04-05 18:14:46 +00:00
|
|
|
import type { SyntheticEvent } from "react";
|
|
|
|
import { useMemo, useState } from "react";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, Dialog, DialogContent, Select } from "@calcom/ui";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
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<MembershipRoleOption>(
|
|
|
|
options.find((option) => option.value === props.initialRole) || {
|
|
|
|
label: t("member"),
|
|
|
|
value: MembershipRole.MEMBER,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const changeRoleMutation = trpc.viewer.teams.changeMemberRole.useMutation({
|
2022-09-12 22:04:33 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
2022-09-12 22:04:33 +00:00
|
|
|
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 (
|
|
|
|
<Dialog open={props.isOpen} onOpenChange={props.onExit}>
|
2023-01-24 14:00:45 +00:00
|
|
|
<DialogContent type="creation">
|
2022-09-12 22:04:33 +00:00
|
|
|
<>
|
|
|
|
<div className="mb-4 sm:flex sm:items-start">
|
|
|
|
<div className="text-center sm:text-left">
|
2023-04-05 18:14:46 +00:00
|
|
|
<h3 className="text-emphasis text-lg font-medium leading-6" id="modal-title">
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("change_member_role")}
|
|
|
|
</h3>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<form onSubmit={changeRole}>
|
|
|
|
<div className="mb-4">
|
2023-04-05 18:14:46 +00:00
|
|
|
<label className="text-default mb-2 block text-sm font-medium tracking-wide" htmlFor="role">
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("role")}
|
|
|
|
</label>
|
|
|
|
{/*<option value="OWNER">{t("owner")}</option> - needs dialog to confirm change of ownership */}
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
|
|
|
options={options}
|
|
|
|
value={role}
|
|
|
|
onChange={(option) => option && setRole(option)}
|
|
|
|
id="role"
|
2023-04-05 18:14:46 +00:00
|
|
|
className="border-default mt-1 block w-full rounded-md text-sm"
|
2022-09-12 22:04:33 +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">
|
2023-04-09 09:13:57 +00:00
|
|
|
<Button type="submit" color="primary" className="ms-2 me-2">
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
<Button type="button" color="secondary" onClick={props.onExit}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|