PromiseRender.tsx 2.38 KB
Newer Older
1
import React from 'react';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2 3 4
import { Spin } from 'antd';
import isEqual from 'lodash/isEqual';
import { isComponentClass } from './Secured';
5
// eslint-disable-next-line import/no-cycle
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6

7
interface PromiseRenderProps<T, K> {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8 9
  ok: T;
  error: K;
10
  promise: Promise<boolean>;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11 12
}

13 14
interface PromiseRenderState {
  component: React.ComponentClass | React.FunctionComponent;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
15 16
}

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17
export default class PromiseRender<T, K> extends React.Component<
18 19
  PromiseRenderProps<T, K>,
  PromiseRenderState
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
> {
21
  state: PromiseRenderState = {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
22 23 24 25 26 27 28
    component: () => null,
  };

  componentDidMount() {
    this.setRenderComponent(this.props);
  }

29
  shouldComponentUpdate = (nextProps: PromiseRenderProps<T, K>, nextState: PromiseRenderState) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30 31 32 33 34 35 36 37 38
    const { component } = this.state;
    if (!isEqual(nextProps, this.props)) {
      this.setRenderComponent(nextProps);
    }
    if (nextState.component !== component) return true;
    return false;
  };

  // set render Component : ok or error
39
  setRenderComponent(props: PromiseRenderProps<T, K>) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41 42 43 44 45 46
    const ok = this.checkIsInstantiation(props.ok);
    const error = this.checkIsInstantiation(props.error);
    props.promise
      .then(() => {
        this.setState({
          component: ok,
        });
47
        return true;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48 49 50 51 52 53 54 55 56 57 58 59 60
      })
      .catch(() => {
        this.setState({
          component: error,
        });
      });
  }

  // Determine whether the incoming component has been instantiated
  // AuthorizedRoute is already instantiated
  // Authorized  render is already instantiated, children is no instantiated
  // Secured is not instantiated
  checkIsInstantiation = (
61 62
    target: React.ReactNode | React.ComponentClass,
  ): React.FunctionComponent => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63
    if (isComponentClass(target)) {
64
      const Target = target as React.ComponentClass;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
65 66 67 68 69 70 71 72 73 74 75
      return (props: any) => <Target {...props} />;
    }
    if (React.isValidElement(target)) {
      return (props: any) => React.cloneElement(target, props);
    }
    return () => target as (React.ReactNode & null);
  };

  render() {
    const { component: Component } = this.state;
    const { ok, error, promise, ...rest } = this.props;
76

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    return Component ? (
      <Component {...rest} />
    ) : (
      <div
        style={{
          width: '100%',
          height: '100%',
          margin: 'auto',
          paddingTop: 50,
          textAlign: 'center',
        }}
      >
        <Spin size="large" />
      </div>
    );
  }
}