|
1 |
| -import React from 'react'; |
2 |
| -import { Route, Redirect, Switch } from 'react-router-dom'; |
| 1 | +import React, { useEffect, useCallback } from 'react'; |
| 2 | +import { |
| 3 | + Link, |
| 4 | + Redirect, |
| 5 | + Route, |
| 6 | + Switch, |
| 7 | + useLocation, |
| 8 | + useParams, |
| 9 | +} from 'react-router-dom'; |
| 10 | +import { withI18n } from '@lingui/react'; |
| 11 | +import { t } from '@lingui/macro'; |
| 12 | +import { Card, PageSection } from '@patternfly/react-core'; |
| 13 | +import { CaretLeftIcon } from '@patternfly/react-icons'; |
| 14 | + |
| 15 | +import useRequest from '../../util/useRequest'; |
| 16 | +import { ExecutionEnvironmentsAPI } from '../../api'; |
| 17 | +import RoutedTabs from '../../components/RoutedTabs'; |
| 18 | +import ContentError from '../../components/ContentError'; |
| 19 | +import ContentLoading from '../../components/ContentLoading'; |
3 | 20 |
|
4 | 21 | import ExecutionEnvironmentDetails from './ExecutionEnvironmentDetails';
|
5 | 22 | import ExecutionEnvironmentEdit from './ExecutionEnvironmentEdit';
|
6 | 23 |
|
7 |
| -function ExecutionEnvironment() { |
| 24 | +function ExecutionEnvironment({ i18n, setBreadcrumb }) { |
| 25 | + const { id } = useParams(); |
| 26 | + const { pathname } = useLocation(); |
| 27 | + |
| 28 | + const { |
| 29 | + isLoading, |
| 30 | + error: contentError, |
| 31 | + request: fetchExecutionEnvironments, |
| 32 | + result: executionEnvironment, |
| 33 | + } = useRequest( |
| 34 | + useCallback(async () => { |
| 35 | + const { data } = await ExecutionEnvironmentsAPI.readDetail(id); |
| 36 | + return data; |
| 37 | + }, [id]), |
| 38 | + null |
| 39 | + ); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + fetchExecutionEnvironments(); |
| 43 | + }, [fetchExecutionEnvironments, pathname]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (executionEnvironment) { |
| 47 | + setBreadcrumb(executionEnvironment); |
| 48 | + } |
| 49 | + }, [executionEnvironment, setBreadcrumb]); |
| 50 | + |
| 51 | + const tabsArray = [ |
| 52 | + { |
| 53 | + name: ( |
| 54 | + <> |
| 55 | + <CaretLeftIcon /> |
| 56 | + {i18n._(t`Back to execution environments`)} |
| 57 | + </> |
| 58 | + ), |
| 59 | + link: '/execution_environments', |
| 60 | + id: 99, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: i18n._(t`Details`), |
| 64 | + link: `/execution_environments/${id}/details`, |
| 65 | + id: 0, |
| 66 | + }, |
| 67 | + ]; |
| 68 | + |
| 69 | + if (!isLoading && contentError) { |
| 70 | + return ( |
| 71 | + <PageSection> |
| 72 | + <Card> |
| 73 | + <ContentError error={contentError}> |
| 74 | + {contentError.response?.status === 404 && ( |
| 75 | + <span> |
| 76 | + {i18n._(t`Execution environment not found.`)}{' '} |
| 77 | + <Link to="/execution_environments"> |
| 78 | + {i18n._(t`View all execution environments`)} |
| 79 | + </Link> |
| 80 | + </span> |
| 81 | + )} |
| 82 | + </ContentError> |
| 83 | + </Card> |
| 84 | + </PageSection> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + let cardHeader = <RoutedTabs tabsArray={tabsArray} />; |
| 89 | + if (pathname.endsWith('edit')) { |
| 90 | + cardHeader = null; |
| 91 | + } |
| 92 | + |
8 | 93 | return (
|
9 |
| - <Switch> |
10 |
| - <Redirect |
11 |
| - from="/execution_environments/:id" |
12 |
| - to="/execution_environments/:id/details" |
13 |
| - exact |
14 |
| - /> |
15 |
| - <Route path="/execution_environments/:id/edit"> |
16 |
| - <ExecutionEnvironmentEdit /> |
17 |
| - </Route> |
18 |
| - <Route path="/execution_environments/:id/details"> |
19 |
| - <ExecutionEnvironmentDetails /> |
20 |
| - </Route> |
21 |
| - </Switch> |
| 94 | + <PageSection> |
| 95 | + <Card> |
| 96 | + {cardHeader} |
| 97 | + {isLoading && <ContentLoading />} |
| 98 | + {!isLoading && executionEnvironment && ( |
| 99 | + <Switch> |
| 100 | + <Redirect |
| 101 | + from="/execution_environments/:id" |
| 102 | + to="/execution_environments/:id/details" |
| 103 | + exact |
| 104 | + /> |
| 105 | + {executionEnvironment && ( |
| 106 | + <> |
| 107 | + <Route path="/execution_environments/:id/edit"> |
| 108 | + <ExecutionEnvironmentEdit |
| 109 | + executionEnvironment={executionEnvironment} |
| 110 | + /> |
| 111 | + </Route> |
| 112 | + <Route path="/execution_environments/:id/details"> |
| 113 | + <ExecutionEnvironmentDetails /> |
| 114 | + </Route> |
| 115 | + </> |
| 116 | + )} |
| 117 | + </Switch> |
| 118 | + )} |
| 119 | + </Card> |
| 120 | + </PageSection> |
22 | 121 | );
|
23 | 122 | }
|
24 | 123 |
|
25 |
| -export default ExecutionEnvironment; |
| 124 | +export default withI18n()(ExecutionEnvironment); |
0 commit comments