9
9
useState ,
10
10
} from "react" ;
11
11
import { useQuery } from "react-query" ;
12
+ import { checkAuthorization } from "api/queries/authCheck" ;
12
13
import { organizations } from "api/queries/organizations" ;
13
- import type { Organization } from "api/typesGenerated" ;
14
+ import type { AuthorizationCheck , Organization } from "api/typesGenerated" ;
14
15
import { Avatar } from "components/Avatar/Avatar" ;
15
16
import { AvatarData } from "components/AvatarData/AvatarData" ;
16
17
import { useDebouncedFunction } from "hooks/debounce" ;
@@ -22,6 +23,7 @@ export type OrganizationAutocompleteProps = {
22
23
className ?: string ;
23
24
size ?: ComponentProps < typeof TextField > [ "size" ] ;
24
25
required ?: boolean ;
26
+ check ?: AuthorizationCheck ;
25
27
} ;
26
28
27
29
export const OrganizationAutocomplete : FC < OrganizationAutocompleteProps > = ( {
@@ -31,6 +33,7 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
31
33
className,
32
34
size = "small" ,
33
35
required,
36
+ check,
34
37
} ) => {
35
38
const [ autoComplete , setAutoComplete ] = useState < {
36
39
value : string ;
@@ -41,6 +44,22 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
41
44
} ) ;
42
45
const organizationsQuery = useQuery ( organizations ( ) ) ;
43
46
47
+ const permissionsQuery = useQuery (
48
+ check && organizationsQuery . data
49
+ ? checkAuthorization ( {
50
+ checks : Object . fromEntries (
51
+ organizationsQuery . data . map ( ( org ) => [
52
+ org . id ,
53
+ {
54
+ ...check ,
55
+ object : { ...check . object , organization_id : org . id } ,
56
+ } ,
57
+ ] ) ,
58
+ ) ,
59
+ } )
60
+ : { enabled : false } ,
61
+ ) ;
62
+
44
63
const { debounced : debouncedInputOnChange } = useDebouncedFunction (
45
64
( event : ChangeEvent < HTMLInputElement > ) => {
46
65
setAutoComplete ( ( state ) => ( {
@@ -51,11 +70,18 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
51
70
750 ,
52
71
) ;
53
72
73
+ // If an authorization check was provided, filter the organizations based on
74
+ // the results of that check.
75
+ let options = organizationsQuery . data ?? [ ] ;
76
+ if ( check && permissionsQuery . data ) {
77
+ options = options . filter ( ( org ) => permissionsQuery . data [ org . id ] ) ;
78
+ }
79
+
54
80
return (
55
81
< Autocomplete
56
82
noOptionsText = "No organizations found"
57
83
className = { className }
58
- options = { organizationsQuery . data ?? [ ] }
84
+ options = { options }
59
85
loading = { organizationsQuery . isLoading }
60
86
value = { value }
61
87
data-testid = "organization-autocomplete"
0 commit comments