| 4 | import { isComponentClass } from './Secured'; // eslint-disable-next-line import/no-cycle |
| 5 | |
| 6 | export default class PromiseRender extends React.Component { |
| 7 | state = { |
| 8 | component: () => null, |
| 9 | }; |
| 10 | |
| 11 | componentDidMount() { |
| 12 | this.setRenderComponent(this.props); |
| 13 | } |
| 14 | |
| 15 | shouldComponentUpdate = (nextProps, nextState) => { |
| 16 | const { component } = this.state; |
| 17 | |
| 18 | if (!isEqual(nextProps, this.props)) { |
| 19 | this.setRenderComponent(nextProps); |
| 20 | } |
| 21 | |
| 22 | if (nextState.component !== component) return true; |
| 23 | return false; |
| 24 | }; // set render Component : ok or error |
| 25 | |
| 26 | setRenderComponent(props) { |
| 27 | const ok = this.checkIsInstantiation(props.ok); |
| 28 | const error = this.checkIsInstantiation(props.error); |
| 29 | props.promise |
| 30 | .then(() => { |
| 31 | this.setState({ |
| 32 | component: ok, |
| 33 | }); |
| 34 | return true; |
| 35 | }) |
| 36 | .catch(() => { |
| 37 | this.setState({ |
| 38 | component: error, |
| 39 | }); |
| 40 | }); |
| 41 | } // Determine whether the incoming component has been instantiated |
| 42 | // AuthorizedRoute is already instantiated |
| 43 | // Authorized render is already instantiated, children is no instantiated |
| 44 | // Secured is not instantiated |
| 45 | |
| 46 | checkIsInstantiation = target => { |
| 47 | if (isComponentClass(target)) { |
| 48 | const Target = target; |
| 49 | return props => <Target {...props} />; |
| 50 | } |
| 51 | |
| 52 | if (React.isValidElement(target)) { |
| 53 | return props => React.cloneElement(target, props); |
| 54 | } |
| 55 | |
| 56 | return () => target; |
| 57 | }; |
| 58 | |
| 59 | render() { |
| 60 | const { component: Component } = this.state; |
| 61 | const { ok, error, promise, ...rest } = this.props; |
| 62 | return Component ? ( |
| 63 | <Component {...rest} /> |
nothing calls this directly
no test coverage detected