Avoids 404 responses when no container request is selected.
[arvados-workbench2.git] / src / views-components / owner-name-uuid-enhancer / owner-name-uuid-enhancer.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Dispatch } from 'redux';
7 import { RootState } from '~/store/store';
8 import { connect } from 'react-redux';
9 import { fetchOwnerNameByUuid } from '~/store/owner-name-uuid-enhancer/owner-name-uuid-enhancer-actions';
10
11 export interface OwnerNameUuidEnhancerOwnProps {
12     uuid: string;
13 }
14
15 export interface OwnerNameUuidEnhancerRootDataProps {
16     ownerNamesMap: any;
17 }
18
19 export interface OwnerNameUuidEnhancerDispatchProps {
20     fetchOwner: Function;
21 }
22
23 export type OwnerNameUuidEnhancerProps = OwnerNameUuidEnhancerOwnProps & OwnerNameUuidEnhancerRootDataProps & OwnerNameUuidEnhancerDispatchProps;
24
25 export const OwnerNameUuidEnhancer = ({ uuid, ownerNamesMap, fetchOwner }: OwnerNameUuidEnhancerProps) => {
26     React.useEffect(() => {
27         if (!ownerNamesMap[uuid]) {
28             fetchOwner(uuid);
29         }
30     }, [uuid, ownerNamesMap, fetchOwner]);
31
32     return <span>{uuid}{ownerNamesMap[uuid] ? ` (${ownerNamesMap[uuid]})` : ''}</span>;
33 };
34
35 const mapStateToProps = (state: RootState): OwnerNameUuidEnhancerRootDataProps => {
36     return {
37         ownerNamesMap: state.ownerNameUuidEnhancer,
38     };
39 };
40
41 const mapDispatchToProps = (dispatch: Dispatch): OwnerNameUuidEnhancerDispatchProps => ({
42     fetchOwner: (uuid: string) => dispatch<any>(fetchOwnerNameByUuid(uuid))
43 });
44
45 export default connect<OwnerNameUuidEnhancerRootDataProps, OwnerNameUuidEnhancerDispatchProps, OwnerNameUuidEnhancerOwnProps>(mapStateToProps, mapDispatchToProps)
46     (OwnerNameUuidEnhancer);