Avoids 404 responses when no container request is selected.
[arvados-workbench2.git] / src / store / owner-name-uuid-enhancer / owner-name-uuid-enhancer-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { unionize, ofType, UnionOf } from '~/common/unionize';
7 import { extractUuidObjectType, ResourceObjectType } from '~/models/resource';
8 import { ServiceRepository } from '~/services/services';
9 import { RootState } from '../store';
10
11 export type OwnerNameUuidEnhancerAction = UnionOf<typeof ownerNameUuidEnhancerActions>;
12
13 export interface OwnerNameState {
14     name: string;
15     uuid: string;
16 }
17
18 export const ownerNameUuidEnhancerActions = unionize({
19     SET_OWNER_NAME_BY_UUID: ofType<OwnerNameState>()
20 });
21
22 export const fetchOwnerNameByUuid = (uuid: string) =>
23     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
24         const objectType = extractUuidObjectType(uuid);
25
26         switch (objectType) {
27             case ResourceObjectType.USER:
28                 services.userService.get(uuid, false)
29                     .then((data) =>
30                         dispatch(
31                             ownerNameUuidEnhancerActions.SET_OWNER_NAME_BY_UUID({
32                                 uuid,
33                                 name: (data as any).fullName,
34                             })
35                         )
36                     );
37                 break;
38             case ResourceObjectType.GROUP:
39                 services.groupsService.get(uuid, false)
40                     .then((data) =>
41                         dispatch(
42                             ownerNameUuidEnhancerActions.SET_OWNER_NAME_BY_UUID({
43                                 uuid,
44                                 name: (data as any).name,
45                             })
46                         )
47                     );
48                 break;
49             default:
50                 break;
51         }
52     };