| 
									
										
										
										
											2021-12-09 23:51:30 +00:00
										 |  |  | import { MembershipRole } from "@prisma/client"; | 
					
						
							|  |  |  | import { useState } from "react"; | 
					
						
							|  |  |  | import React, { SyntheticEvent } from "react"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default function MemberChangeRoleModal(props: { | 
					
						
							| 
									
										
										
										
											2022-03-17 13:20:49 +00:00
										 |  |  |   isOpen: boolean; | 
					
						
							| 
									
										
										
										
											2021-12-09 23:51:30 +00:00
										 |  |  |   memberId: number; | 
					
						
							|  |  |  |   teamId: number; | 
					
						
							|  |  |  |   initialRole: MembershipRole; | 
					
						
							|  |  |  |   onExit: () => void; | 
					
						
							|  |  |  | }) { | 
					
						
							|  |  |  |   const [role, setRole] = useState(props.initialRole || MembershipRole.MEMBER); | 
					
						
							|  |  |  |   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, | 
					
						
							|  |  |  |       role, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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> | 
					
						
							|  |  |  |             <select | 
					
						
							|  |  |  |               value={role} | 
					
						
							|  |  |  |               onChange={(e) => setRole(e.target.value as MembershipRole)} | 
					
						
							|  |  |  |               id="role" | 
					
						
							| 
									
										
										
										
											2022-02-09 22:32:31 +00:00
										 |  |  |               className="focus:border-brand mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:ring-black sm:text-sm"> | 
					
						
							| 
									
										
										
										
											2021-12-09 23:51:30 +00:00
										 |  |  |               <option value="MEMBER">{t("member")}</option> | 
					
						
							|  |  |  |               <option value="ADMIN">{t("admin")}</option> | 
					
						
							| 
									
										
										
										
											2021-12-23 01:05:49 +00:00
										 |  |  |               {/*<option value="OWNER">{t("owner")}</option> - needs dialog to confirm change of ownership */} | 
					
						
							| 
									
										
										
										
											2021-12-09 23:51:30 +00:00
										 |  |  |             </select> | 
					
						
							|  |  |  |           </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> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } |