WrappedErrorApp.tsx 1.77 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import renderRoutes from '@gringlobal/client/ui/renderRoutes';
// Actions
import { updateHistory } from '@gringlobal/client/action/history';
// Model
import SsrError from '@gringlobal/client/model/common/SsrError';
// Ui
import NotFound from '@gringlobal/client/ui/common/error/NotFound'


interface IWrappedErrorAppProps extends React.ClassAttributes<any>, RouteComponentProps {
  ssrError: SsrError;
  route?: any;
  updateHistory: (location: string) => void;
}

class WrappedErrorApp extends React.Component<IWrappedErrorAppProps, any> {

  public componentDidUpdate(prevProps: IWrappedErrorAppProps) {
    const { updateHistory, location: prevLocation } = prevProps;
    const { location } = this.props;
    if (prevLocation !== null && location !== null) {
      if (prevLocation !== location) {
        updateHistory(`${prevLocation.pathname}${prevLocation.search ? prevLocation.search : ''}`);
      }
    }
  }

  protected getErrorPage = (status: number) => {
    switch (+status) {
      case 404: return NotFound;
      default: return null;
    }
  };

  protected compileErrorRoute = (ssrError) => ({
    component: this.getErrorPage(ssrError.status),
    path: ssrError.route,
  });


  public render() {
    const { route: { routes }, ssrError } = this.props;
    return (
      <div>
        { renderRoutes([this.compileErrorRoute(ssrError), ...routes]) }
      </div>
    );
  }
}
const mapStateToProps = (state) => ({
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  updateHistory,
}, dispatch);

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(WrappedErrorApp));