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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7 8 9
interface IPromiseRenderProps<T, K> {
  ok: T;
  error: K;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10 11 12 13 14 15 16
  promise: Promise<any>;
}

interface IPromiseRenderState {
  component: React.ComponentClass<any, any> | React.FunctionComponent<any>;
}

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

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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29 30 31 32
  shouldComponentUpdate = (
    nextProps: IPromiseRenderProps<T, K>,
    nextState: IPromiseRenderState,
  ) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33 34 35 36 37 38 39 40 41
    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
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42
  setRenderComponent(props: IPromiseRenderProps<T, K>) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    const ok = this.checkIsInstantiation(props.ok);
    const error = this.checkIsInstantiation(props.error);
    props.promise
      .then(() => {
        this.setState({
          component: ok,
        });
      })
      .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 = (
    target: React.ReactNode | React.ComponentClass<any, any>,
  ): React.FunctionComponent<any> => {
    if (isComponentClass(target)) {
      const Target = target as React.ComponentClass<any, any>;
      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;
    return Component ? (
      <Component {...rest} />
    ) : (
      <div
        style={{
          width: '100%',
          height: '100%',
          margin: 'auto',
          paddingTop: 50,
          textAlign: 'center',
        }}
      >
        <Spin size="large" />
      </div>
    );
  }
}