| 15 | type EditMemberProps = IServiceMember; |
| 16 | |
| 17 | export default function EditMember(member: EditMemberProps) { |
| 18 | const queryClient = useQueryClient(); |
| 19 | const trpc = useTRPC(); |
| 20 | |
| 21 | const [access, setAccess] = useState<string[]>( |
| 22 | member.access?.map((a) => a.projectId) ?? [], |
| 23 | ); |
| 24 | |
| 25 | const projectsQuery = useQuery( |
| 26 | trpc.project.list.queryOptions({ organizationId: member.organizationId }), |
| 27 | ); |
| 28 | |
| 29 | const mutation = useMutation( |
| 30 | trpc.organization.updateMemberAccess.mutationOptions({ |
| 31 | onError(error) { |
| 32 | handleError(error); |
| 33 | setAccess(member.access?.map((a) => a.projectId) ?? []); |
| 34 | }, |
| 35 | onSuccess() { |
| 36 | toast.success('Access updated'); |
| 37 | // Refresh members list so access column reflects changes |
| 38 | queryClient.invalidateQueries(trpc.organization.members.pathFilter()); |
| 39 | popModal(); |
| 40 | }, |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const projects = projectsQuery.data ?? []; |
| 45 | |
| 46 | return ( |
| 47 | <ModalContent> |
| 48 | <ModalHeader |
| 49 | title={ |
| 50 | member.user |
| 51 | ? `Edit access for ${[member.user.firstName, member.user.lastName] |
| 52 | .filter(Boolean) |
| 53 | .join(' ')}` |
| 54 | : 'Edit member access' |
| 55 | } |
| 56 | /> |
| 57 | |
| 58 | <div className="col gap-4"> |
| 59 | <ComboboxAdvanced |
| 60 | placeholder="Restrict access to projects" |
| 61 | value={access} |
| 62 | onChange={(newAccess) => setAccess(newAccess as string[])} |
| 63 | items={projects.map((p) => ({ label: p.name, value: p.id }))} |
| 64 | /> |
| 65 | |
| 66 | <ButtonContainer> |
| 67 | <Button type="button" variant="outline" onClick={() => popModal()}> |
| 68 | Cancel |
| 69 | </Button> |
| 70 | <Button |
| 71 | onClick={() => |
| 72 | mutation.mutate({ |
| 73 | userId: member.user!.id, |
| 74 | organizationId: member.organizationId, |