|
1 |
| -import { get, getUrlWithSearchParams } from '@Common/index' |
| 1 | +import { get, ResponseType, ROUTES } 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, BaseFilterQueryParams } from '@Shared/types' |
| 5 | +import { DefaultUserKey } from '@Shared/types' |
6 | 6 | import { GroupBase } from 'react-select'
|
| 7 | +import { UserMinType } from './types' |
| 8 | +import { getUserAndApiTokenOption } from './utils' |
7 | 9 |
|
8 | 10 | // FIXME: Common out the typing and url from dashboard
|
9 | 11 | export const getUserAndApiTokenOptions = async (): Promise<GroupBase<SelectPickerCustomOptionType<string>>[]> => {
|
10 |
| - const userUrl = getUrlWithSearchParams('user/v2', { |
11 |
| - showAll: true, |
12 |
| - } as BaseFilterQueryParams<never>) |
| 12 | + const { result } = (await get(ROUTES.USER_LIST_MIN)) as ResponseType<UserMinType[]> |
13 | 13 |
|
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({ |
22 |
| - label: 'Users', |
23 |
| - options: (userResponse?.users ?? []) |
24 |
| - .sort((a, b) => stringComparatorBySortOrder(a.email_id, b.email_id)) |
25 |
| - .filter(({ email_id: emailId }) => emailId !== DefaultUserKey.system) |
26 |
| - .map(({ email_id: emailId }) => ({ |
27 |
| - label: emailId, |
28 |
| - value: emailId, |
29 |
| - })), |
30 |
| - }) |
| 14 | + if (!result) { |
| 15 | + return [] |
31 | 16 | }
|
32 | 17 |
|
33 |
| - if (apiTokenPromise.status === 'fulfilled') { |
34 |
| - const { result: apiTokenResponse } = apiTokenPromise.value |
| 18 | + const sortedEmailList = result.map(({ emailId }) => emailId).sort(stringComparatorBySortOrder) |
35 | 19 |
|
36 |
| - options.push({ |
| 20 | + return [ |
| 21 | + { |
| 22 | + label: 'Users', |
| 23 | + options: sortedEmailList |
| 24 | + .filter((emailId) => emailId !== DefaultUserKey.system && !emailId.startsWith(API_TOKEN_PREFIX)) |
| 25 | + .map((emailId) => getUserAndApiTokenOption(emailId)), |
| 26 | + }, |
| 27 | + { |
37 | 28 | label: 'API Tokens',
|
38 |
| - options: (apiTokenResponse ?? []) |
39 |
| - .sort((a, b) => stringComparatorBySortOrder(a.userIdentifier, b.userIdentifier)) |
40 |
| - .map(({ userIdentifier }) => ({ |
41 |
| - // Remove the API Token Prefix |
42 |
| - label: userIdentifier.startsWith(API_TOKEN_PREFIX) ? userIdentifier.split(':')[1] : userIdentifier, |
43 |
| - value: userIdentifier, |
44 |
| - })), |
45 |
| - }) |
46 |
| - } |
47 |
| - |
48 |
| - return options |
| 29 | + options: sortedEmailList |
| 30 | + .filter((emailId) => emailId.startsWith(API_TOKEN_PREFIX)) |
| 31 | + .map((emailId) => getUserAndApiTokenOption(emailId)), |
| 32 | + }, |
| 33 | + ] |
49 | 34 | }
|
0 commit comments