joyent-portal/packages/cp-frontend/src/containers/navigation/not-found-hoc.js

69 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { Component } from 'react';
import { NotFound } from '@components/navigation';
export const GqlPaths = {
DEPLOYMENT_GROUP: 'deploymentGroup',
SERVICES: 'services'
};
export default paths => {
return WrappedComponent => {
return class extends Component {
componentWillReceiveProps(nextProps) {
if (paths) {
const { error, location, history } = nextProps;
if (
error &&
(!location.state || !location.state.notFound) &&
(error.graphQLErrors && error.graphQLErrors.length)
) {
const graphQLError = error.graphQLErrors[0];
if (graphQLError.message === 'Not Found') {
const notFound = graphQLError.path.pop();
if (paths.indexOf(notFound) > -1) {
history.replace(location.pathname, { notFound });
}
}
}
}
}
render() {
const { location, match } = this.props;
if (location.state && location.state.notFound) {
const notFound = location.state.notFound;
if (paths && paths.indexOf(notFound) > -1) {
let title;
let to;
let link;
if (notFound === 'services' || notFound === 'service') {
title = 'This service doesnt exist';
to = match.url
.split('/')
.slice(0, 3)
.join('/');
link = 'Back to services';
} else if (notFound === 'deploymentGroup') {
title = 'This deployment group doesnt exist';
to = '/deployment-group';
link = 'Back to dashboard';
}
return (
<NotFound
title={title}
message="Sorry, but our princess is in another castle."
to={to}
link={link}
/>
);
}
return null;
}
return <WrappedComponent {...this.props} />;
}
};
};
};