MetaPhase-Consulting/State-TalentMAP

View on GitHub
src/Containers/AuthorizedWrapper/AuthorizedWrapper.jsx

Summary

Maintainability
A
0 mins
Test Coverage
B
88%
import { Component } from 'react';
import PropTypes from 'prop-types';

class AuthorizedWrapper extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isAuthorized: { value: false },
    };
  }
  UNSAFE_componentWillMount() {
    this.setAuthorization();
  }
  UNSAFE_componentWillReceiveProps() {
    this.setAuthorization();
  }
  setAuthorization() {
    const isAuthorized = { value: this.props.isAuthorized() };
    this.setState({ isAuthorized });
  }
  render() {
    const { isAuthorized } = this.state;
    return (
      <div className="authorized-wrapper">
        { isAuthorized.value && this.props.children }
      </div>
    );
  }
}

AuthorizedWrapper.propTypes = {
  isAuthorized: PropTypes.func.isRequired,
  children: PropTypes.node.isRequired,
};

export default AuthorizedWrapper;