7
7
} from "@squonk/account-server-client/user" ;
8
8
import { type DmError } from "@squonk/data-manager-client" ;
9
9
10
- import { Typography } from "@mui/material" ;
11
10
import { useQueryClient } from "@tanstack/react-query" ;
12
11
13
12
import { useEnqueueError } from "../../hooks/useEnqueueStackError" ;
@@ -16,51 +15,59 @@ import { useSelectedOrganisation } from "../../state/organisationSelection";
16
15
import { CenterLoader } from "../CenterLoader" ;
17
16
import { ManageUsers } from "../ManageUsers" ;
18
17
19
- export interface UnitEditorsProps {
18
+ export interface UnitMembersProps {
20
19
/**
21
20
* Unit to be edited.
22
21
*/
23
22
unit : UnitDetail ;
24
23
}
25
24
26
25
/**
27
- * MuiAutocomplete to manage the current editors of the selected project
26
+ * MuiAutocomplete to manage the current members of the selected project
28
27
*/
29
- export const UnitEditors = ( { unit } : UnitEditorsProps ) => {
28
+ export const UnitMembers = ( { unit } : UnitMembersProps ) => {
30
29
const { user : currentUser } = useKeycloakUser ( ) ;
31
30
32
31
const [ organisation ] = useSelectedOrganisation ( ) ;
33
32
34
33
const { data, isLoading : isUsersLoading } = useGetOrganisationUnitUsers ( unit . id , {
35
34
query : { enabled : ! ! unit . caller_is_member || organisation ?. caller_is_member } ,
36
35
} ) ;
37
- const users = data ?. users ;
38
- const { mutateAsync : addEditor , isPending : isAdding } = useAddOrganisationUnitUser ( ) ;
39
- const { mutateAsync : removeEditor , isPending : isRemoving } = useDeleteOrganisationUnitUser ( ) ;
36
+ const users = data ?. users ?? [ ] ;
37
+ const { mutateAsync : addMember , isPending : isAdding } = useAddOrganisationUnitUser ( ) ;
38
+ const { mutateAsync : removeMember , isPending : isRemoving } = useDeleteOrganisationUnitUser ( ) ;
40
39
const queryClient = useQueryClient ( ) ;
41
40
42
41
const { enqueueError, enqueueSnackbar } = useEnqueueError < DmError > ( ) ;
43
42
43
+ const isOrganisationMember = organisation ?. caller_is_member ;
44
+ const isUnitMember = unit . caller_is_member ;
44
45
const isPersonalUnit = organisation ?. name === process . env . NEXT_PUBLIC_DEFAULT_ORG_NAME ;
45
46
47
+ const helperText = isPersonalUnit
48
+ ? "Members of personal unit may not be changed"
49
+ : ! isOrganisationMember || ! isUnitMember
50
+ ? "You must be a unit or organisation member to view and modify unit members"
51
+ : undefined ;
52
+
46
53
if ( isUsersLoading ) {
47
54
return < CenterLoader /> ;
48
55
}
49
56
50
- if ( users && currentUser . username ) {
57
+ if ( currentUser . username ) {
51
58
return (
52
59
< ManageUsers
53
- disabled = { isPersonalUnit }
60
+ disabled = { ! ! helperText }
54
61
disabledUsers = { [ unit . owner_id ] }
55
- helperText = { isPersonalUnit ? "Editors of personal unit may not be changed" : undefined }
62
+ helperText = { helperText }
56
63
isLoading = { isAdding || isRemoving || isUsersLoading }
57
- title = "Unit Editors "
64
+ title = "Unit Members "
58
65
users = { users . map ( ( user ) => user . id ) }
59
66
onRemove = { async ( value ) => {
60
- const user = users . find ( ( editor ) => ! value . includes ( editor . id ) ) ;
67
+ const user = users . find ( ( member ) => ! value . includes ( member . id ) ) ;
61
68
if ( user ) {
62
69
try {
63
- await removeEditor ( { unitId : unit . id , userId : user . id } ) ;
70
+ await removeMember ( { unitId : unit . id , userId : user . id } ) ;
64
71
} catch ( error ) {
65
72
enqueueError ( error ) ;
66
73
}
@@ -76,7 +83,7 @@ export const UnitEditors = ({ unit }: UnitEditorsProps) => {
76
83
const username = value . reverse ( ) . find ( ( user ) => ! users . map ( ( u ) => u . id ) . includes ( user ) ) ;
77
84
if ( username ) {
78
85
try {
79
- await addEditor ( { unitId : unit . id , userId : username } ) ;
86
+ await addMember ( { unitId : unit . id , userId : username } ) ;
80
87
} catch ( error ) {
81
88
enqueueError ( error ) ;
82
89
}
@@ -91,5 +98,6 @@ export const UnitEditors = ({ unit }: UnitEditorsProps) => {
91
98
/>
92
99
) ;
93
100
}
94
- return < Typography > You must be a unit or organisation member to modify unit editors</ Typography > ;
101
+
102
+ return null ;
95
103
} ;
0 commit comments