mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 10:00:01 -03:30
Merge pull request #4435 from keithjgrant/4244-not-found-route
Add NotFound route handling Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
commit
e9df4ed800
@ -1,6 +1,8 @@
|
||||
import React from 'react';
|
||||
import { t } from '@lingui/macro';
|
||||
import styled from 'styled-components';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { bool, instanceOf } from 'prop-types';
|
||||
import { t } from '@lingui/macro';
|
||||
import { withI18n } from '@lingui/react';
|
||||
import {
|
||||
Title,
|
||||
@ -9,30 +11,53 @@ import {
|
||||
EmptyStateBody,
|
||||
} from '@patternfly/react-core';
|
||||
import { ExclamationTriangleIcon } from '@patternfly/react-icons';
|
||||
|
||||
import { RootAPI } from '@api';
|
||||
import ErrorDetail from '@components/ErrorDetail';
|
||||
|
||||
const EmptyState = styled(PFEmptyState)`
|
||||
width: var(--pf-c-empty-state--m-lg--MaxWidth);
|
||||
`;
|
||||
|
||||
class ContentError extends React.Component {
|
||||
render() {
|
||||
const { error, i18n } = this.props;
|
||||
return (
|
||||
<EmptyState>
|
||||
<EmptyStateIcon icon={ExclamationTriangleIcon} />
|
||||
<Title size="lg">{i18n._(t`Something went wrong...`)}</Title>
|
||||
<EmptyStateBody>
|
||||
{i18n._(
|
||||
t`There was an error loading this content. Please reload the page.`
|
||||
)}
|
||||
</EmptyStateBody>
|
||||
{error && <ErrorDetail error={error} />}
|
||||
</EmptyState>
|
||||
);
|
||||
}
|
||||
async function logout() {
|
||||
await RootAPI.logout();
|
||||
window.location.replace('/#/login');
|
||||
}
|
||||
|
||||
function ContentError({ error, children, isNotFound, i18n }) {
|
||||
if (error && error.response && error.response.status === 401) {
|
||||
if (!error.response.headers['session-timeout']) {
|
||||
logout();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const is404 =
|
||||
isNotFound || (error && error.response && error.response.status === 404);
|
||||
return (
|
||||
<EmptyState>
|
||||
<EmptyStateIcon icon={ExclamationTriangleIcon} />
|
||||
<Title size="lg">
|
||||
{is404 ? i18n._(t`Not Found`) : i18n._(t`Something went wrong...`)}
|
||||
</Title>
|
||||
<EmptyStateBody>
|
||||
{is404
|
||||
? i18n._(t`The page you requested could not be found.`)
|
||||
: i18n._(
|
||||
t`There was an error loading this content. Please reload the page.`
|
||||
)}{' '}
|
||||
{children || <Link to="/home">{i18n._(t`Back to Dashboard.`)}</Link>}
|
||||
</EmptyStateBody>
|
||||
{error && <ErrorDetail error={error} />}
|
||||
</EmptyState>
|
||||
);
|
||||
}
|
||||
ContentError.propTypes = {
|
||||
error: instanceOf(Error),
|
||||
isNotFound: bool,
|
||||
};
|
||||
ContentError.defaultProps = {
|
||||
error: null,
|
||||
isNotFound: false,
|
||||
};
|
||||
|
||||
export { ContentError as _ContentError };
|
||||
export default withI18n()(ContentError);
|
||||
|
||||
@ -32,6 +32,7 @@ import License from '@screens/License';
|
||||
import Teams from '@screens/Team';
|
||||
import Templates from '@screens/Template';
|
||||
import Users from '@screens/User';
|
||||
import NotFound from '@screens/NotFound';
|
||||
|
||||
import App from './App';
|
||||
import RootProvider from './RootProvider';
|
||||
@ -224,8 +225,8 @@ export function main(render) {
|
||||
],
|
||||
},
|
||||
]}
|
||||
render={({ routeGroups }) =>
|
||||
routeGroups
|
||||
render={({ routeGroups }) => {
|
||||
const routeList = routeGroups
|
||||
.reduce(
|
||||
(allRoutes, { routes }) => allRoutes.concat(routes),
|
||||
[]
|
||||
@ -238,8 +239,16 @@ export function main(render) {
|
||||
<PageComponent match={match} />
|
||||
)}
|
||||
/>
|
||||
))
|
||||
}
|
||||
));
|
||||
routeList.push(
|
||||
<Route
|
||||
key="not-found"
|
||||
path="*"
|
||||
component={NotFound}
|
||||
/>
|
||||
);
|
||||
return <Switch>{routeList}</Switch>;
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Route, withRouter, Switch, Redirect } from 'react-router-dom';
|
||||
import { Route, withRouter, Switch, Redirect, Link } from 'react-router-dom';
|
||||
import { withI18n } from '@lingui/react';
|
||||
import { t } from '@lingui/macro';
|
||||
import styled from 'styled-components';
|
||||
@ -99,7 +99,14 @@ class Job extends Component {
|
||||
return (
|
||||
<PageSection>
|
||||
<Card className="awx-c-card">
|
||||
<ContentError error={contentError} />
|
||||
<ContentError error={contentError}>
|
||||
{contentError.response.status === 404 && (
|
||||
<span>
|
||||
{i18n._(`The page you requested could not be found.`)}{' '}
|
||||
<Link to="/jobs">{i18n._(`View all Jobs.`)}</Link>
|
||||
</span>
|
||||
)}
|
||||
</ContentError>
|
||||
</Card>
|
||||
</PageSection>
|
||||
);
|
||||
@ -139,6 +146,19 @@ class Job extends Component {
|
||||
path="/jobs/:type/:id/output"
|
||||
render={() => <JobOutput type={match.params.type} job={job} />}
|
||||
/>,
|
||||
<Route
|
||||
key="not-found"
|
||||
path="*"
|
||||
render={() => (
|
||||
<ContentError isNotFound>
|
||||
<Link
|
||||
to={`/jobs/${match.params.type}/${match.params.id}/details`}
|
||||
>
|
||||
{i18n._(`View Job Details`)}
|
||||
</Link>
|
||||
</ContentError>
|
||||
)}
|
||||
/>,
|
||||
]}
|
||||
</Switch>
|
||||
</Card>
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Redirect } from 'react-router-dom';
|
||||
import { Redirect, Link } from 'react-router-dom';
|
||||
import { PageSection, Card } from '@patternfly/react-core';
|
||||
import { withI18n } from '@lingui/react';
|
||||
import { UnifiedJobsAPI } from '@api';
|
||||
import ContentError from '@components/ContentError';
|
||||
import { JOB_TYPE_URL_SEGMENTS } from '../../constants';
|
||||
|
||||
const NOT_FOUND = 'not found';
|
||||
|
||||
class JobTypeRedirect extends Component {
|
||||
static defaultProps = {
|
||||
view: 'details',
|
||||
@ -12,8 +17,9 @@ class JobTypeRedirect extends Component {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
hasError: false,
|
||||
error: null,
|
||||
job: null,
|
||||
isLoading: true,
|
||||
};
|
||||
this.loadJob = this.loadJob.bind(this);
|
||||
}
|
||||
@ -24,24 +30,44 @@ class JobTypeRedirect extends Component {
|
||||
|
||||
async loadJob() {
|
||||
const { id } = this.props;
|
||||
this.setState({ isLoading: true });
|
||||
try {
|
||||
const { data } = await UnifiedJobsAPI.read({ id });
|
||||
const job = data.results[0];
|
||||
this.setState({
|
||||
job: data.results[0],
|
||||
job,
|
||||
isLoading: false,
|
||||
error: job ? null : NOT_FOUND,
|
||||
});
|
||||
} catch (error) {
|
||||
this.setState({
|
||||
error,
|
||||
isLoading: false,
|
||||
});
|
||||
} catch (err) {
|
||||
this.setState({ hasError: true });
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { path, view } = this.props;
|
||||
const { hasError, job } = this.state;
|
||||
const { path, view, i18n } = this.props;
|
||||
const { error, job, isLoading } = this.state;
|
||||
|
||||
if (hasError) {
|
||||
return <div>Error</div>;
|
||||
if (error) {
|
||||
return (
|
||||
<PageSection>
|
||||
<Card className="awx-c-card">
|
||||
{error === NOT_FOUND ? (
|
||||
<ContentError isNotFound>
|
||||
<Link to="/jobs">{i18n._(`View all Jobs`)}</Link>
|
||||
</ContentError>
|
||||
) : (
|
||||
<ContentError error={error} />
|
||||
)}
|
||||
</Card>
|
||||
</PageSection>
|
||||
);
|
||||
}
|
||||
if (!job) {
|
||||
if (isLoading) {
|
||||
// TODO show loading state
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
const type = JOB_TYPE_URL_SEGMENTS[job.type];
|
||||
@ -49,4 +75,4 @@ class JobTypeRedirect extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
export default JobTypeRedirect;
|
||||
export default withI18n()(JobTypeRedirect);
|
||||
|
||||
15
awx/ui_next/src/screens/NotFound.jsx
Normal file
15
awx/ui_next/src/screens/NotFound.jsx
Normal file
@ -0,0 +1,15 @@
|
||||
import React from 'react';
|
||||
import { PageSection, Card } from '@patternfly/react-core';
|
||||
import ContentError from '@components/ContentError';
|
||||
|
||||
function NotFound() {
|
||||
return (
|
||||
<PageSection>
|
||||
<Card className="awx-c-card">
|
||||
<ContentError isNotFound />
|
||||
</Card>
|
||||
</PageSection>
|
||||
);
|
||||
}
|
||||
|
||||
export default NotFound;
|
||||
@ -1,7 +1,7 @@
|
||||
import React, { Component } from 'react';
|
||||
import { withI18n } from '@lingui/react';
|
||||
import { t } from '@lingui/macro';
|
||||
import { Switch, Route, withRouter, Redirect } from 'react-router-dom';
|
||||
import { Switch, Route, withRouter, Redirect, Link } from 'react-router-dom';
|
||||
import {
|
||||
Card,
|
||||
CardHeader as PFCardHeader,
|
||||
@ -168,7 +168,16 @@ class Organization extends Component {
|
||||
return (
|
||||
<PageSection>
|
||||
<Card className="awx-c-card">
|
||||
<ContentError error={contentError} />
|
||||
<ContentError error={contentError}>
|
||||
{contentError.response.status === 404 && (
|
||||
<span>
|
||||
{i18n._(`Organization not found.`)}{' '}
|
||||
<Link to="/organizations">
|
||||
{i18n._(`View all Organizations.`)}
|
||||
</Link>
|
||||
</span>
|
||||
)}
|
||||
</ContentError>
|
||||
</Card>
|
||||
</PageSection>
|
||||
);
|
||||
@ -226,6 +235,20 @@ class Organization extends Component {
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
<Route
|
||||
key="not-found"
|
||||
path="*"
|
||||
render={() => (
|
||||
<ContentError isNotFound>
|
||||
{match.params.id && (
|
||||
<Link to={`/organizations/${match.params.id}/details`}>
|
||||
{i18n._(`View Organization Details`)}
|
||||
</Link>
|
||||
)}
|
||||
</ContentError>
|
||||
)}
|
||||
/>
|
||||
,
|
||||
</Switch>
|
||||
</Card>
|
||||
</PageSection>
|
||||
|
||||
@ -149,7 +149,7 @@ class OrganizationsList extends Component {
|
||||
<PageSection variant={medium}>
|
||||
<Card>
|
||||
<PaginatedDataList
|
||||
error={contentError}
|
||||
contentError={contentError}
|
||||
hasContentLoading={hasContentLoading}
|
||||
items={organizations}
|
||||
itemCount={itemCount}
|
||||
|
||||
@ -2,7 +2,7 @@ import React, { Component } from 'react';
|
||||
import { t } from '@lingui/macro';
|
||||
import { withI18n } from '@lingui/react';
|
||||
import { Card, CardHeader, PageSection } from '@patternfly/react-core';
|
||||
import { Switch, Route, Redirect, withRouter } from 'react-router-dom';
|
||||
import { Switch, Route, Redirect, withRouter, Link } from 'react-router-dom';
|
||||
import CardCloseButton from '@components/CardCloseButton';
|
||||
import ContentError from '@components/ContentError';
|
||||
import RoutedTabs from '@components/RoutedTabs';
|
||||
@ -77,7 +77,14 @@ class Template extends Component {
|
||||
return (
|
||||
<PageSection>
|
||||
<Card className="awx-c-card">
|
||||
<ContentError error={contentError} />
|
||||
<ContentError error={contentError}>
|
||||
{contentError.response.status === 404 && (
|
||||
<span>
|
||||
{i18n._(`Template not found.`)}{' '}
|
||||
<Link to="/templates">{i18n._(`View all Templates.`)}</Link>
|
||||
</span>
|
||||
)}
|
||||
</ContentError>
|
||||
</Card>
|
||||
</PageSection>
|
||||
);
|
||||
@ -92,8 +99,9 @@ class Template extends Component {
|
||||
to="/templates/:templateType/:id/details"
|
||||
exact
|
||||
/>
|
||||
{template && (
|
||||
{template && [
|
||||
<Route
|
||||
key="details"
|
||||
path="/templates/:templateType/:id/details"
|
||||
render={() => (
|
||||
<JobTemplateDetail
|
||||
@ -102,14 +110,28 @@ class Template extends Component {
|
||||
template={template}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
{template && (
|
||||
/>,
|
||||
<Route
|
||||
key="edit"
|
||||
path="/templates/:templateType/:id/edit"
|
||||
render={() => <JobTemplateEdit template={template} />}
|
||||
/>
|
||||
)}
|
||||
/>,
|
||||
<Route
|
||||
key="not-found"
|
||||
path="*"
|
||||
render={() => (
|
||||
<ContentError isNotFound>
|
||||
{match.params.id && (
|
||||
<Link
|
||||
to={`/templates/${match.params.templateType}/${match.params.id}/details`}
|
||||
>
|
||||
{i18n._(`View Template Details`)}
|
||||
</Link>
|
||||
)}
|
||||
</ContentError>
|
||||
)}
|
||||
/>,
|
||||
]}
|
||||
</Switch>
|
||||
</Card>
|
||||
</PageSection>
|
||||
|
||||
@ -175,7 +175,7 @@ class TemplatesList extends Component {
|
||||
<PageSection variant={medium}>
|
||||
<Card>
|
||||
<PaginatedDataList
|
||||
err={contentError}
|
||||
contentError={contentError}
|
||||
hasContentLoading={hasContentLoading}
|
||||
items={templates}
|
||||
itemCount={itemCount}
|
||||
|
||||
@ -5,7 +5,6 @@ import { Route, withRouter, Switch } from 'react-router-dom';
|
||||
|
||||
import { Config } from '@contexts/Config';
|
||||
import Breadcrumbs from '@components/Breadcrumbs/Breadcrumbs';
|
||||
|
||||
import { TemplateList } from './TemplateList';
|
||||
import Template from './Template';
|
||||
import JobTemplateAdd from './JobTemplateAdd';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user