1
- import { get } from '@Common/index'
1
+ import { get , getUrlWithSearchParams } from '@Common/index'
2
2
import { SelectPickerCustomOptionType } from '@Shared/Components'
3
3
import { API_TOKEN_PREFIX } from '@Shared/constants'
4
4
import { stringComparatorBySortOrder } from '@Shared/Helpers'
5
- import { DefaultUserKey } from '@Shared/types'
5
+ import { DefaultUserKey , BaseFilterQueryParams } from '@Shared/types'
6
6
import { GroupBase } from 'react-select'
7
7
8
8
// FIXME: Common out the typing and url from dashboard
9
9
export const getUserAndApiTokenOptions = async ( ) : Promise < GroupBase < SelectPickerCustomOptionType < string > > [ ] > => {
10
- const [ { result : userResponse } , { result : apiTokenResponse } ] = await Promise . all ( [
11
- get ( 'user/v2' ) ,
12
- get ( 'api-token' ) ,
13
- ] )
10
+ const userUrl = getUrlWithSearchParams ( 'user/v2' , {
11
+ showAll : true ,
12
+ } as BaseFilterQueryParams < never > )
14
13
15
- return [
16
- {
14
+ const [ userPromise , apiTokenPromise ] = await Promise . allSettled ( [ get ( userUrl ) , get ( 'api-token' ) ] )
15
+
16
+ const options : GroupBase < SelectPickerCustomOptionType < string > > [ ] = [ ]
17
+
18
+ if ( userPromise . status === 'fulfilled' ) {
19
+ const { result : userResponse } = userPromise . value
20
+
21
+ options . push ( {
17
22
label : 'Users' ,
18
23
options : ( userResponse ?. users ?? [ ] )
19
24
. sort ( ( a , b ) => stringComparatorBySortOrder ( a . email_id , b . email_id ) )
@@ -22,8 +27,13 @@ export const getUserAndApiTokenOptions = async (): Promise<GroupBase<SelectPicke
22
27
label : emailId ,
23
28
value : emailId ,
24
29
} ) ) ,
25
- } ,
26
- {
30
+ } )
31
+ }
32
+
33
+ if ( apiTokenPromise . status === 'fulfilled' ) {
34
+ const { result : apiTokenResponse } = apiTokenPromise . value
35
+
36
+ options . push ( {
27
37
label : 'API Tokens' ,
28
38
options : ( apiTokenResponse ?? [ ] )
29
39
. sort ( ( a , b ) => stringComparatorBySortOrder ( a . userIdentifier , b . userIdentifier ) )
@@ -32,6 +42,8 @@ export const getUserAndApiTokenOptions = async (): Promise<GroupBase<SelectPicke
32
42
label : userIdentifier . startsWith ( API_TOKEN_PREFIX ) ? userIdentifier . split ( ':' ) [ 1 ] : userIdentifier ,
33
43
value : userIdentifier ,
34
44
} ) ) ,
35
- } ,
36
- ]
45
+ } )
46
+ }
47
+
48
+ return options
37
49
}
0 commit comments