{"size": 1255, "ext": "js", "max_stars_repo_path": "src/ui_parts/PermissionWrapper.js", "max_stars_repo_name": "semarche/react-redux-boilerplate", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/ui_parts/PermissionWrapper.js", "max_issues_repo_name": "semarche/react-redux-boilerplate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/ui_parts/PermissionWrapper.js", "max_forks_repo_name": "semarche/react-redux-boilerplate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.2826086957, "max_line_length": 113, "alphanum_fraction": 0.7043824701}
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import get from 'lodash/get';
import isArray from 'lodash/isArray';
import { withRouter } from 'react-router';
import { key } from '../app/module/constants';
import { store } from '../Root';

export const roles = {
  ROOT: 'root',
  MANAGER: 'director',
  ACCOUNTANT: 'accountant',
};

export const permissionBool = (availableRoles = []) => {
  const userRole = get(store.getState()[key], 'account.user_role', '');
  if (!isArray(availableRoles) || !availableRoles.includes(userRole)) {
    return false
  }
  return true
};

const propTypes = {
  userRole: PropTypes.string.isRequired,
  children: PropTypes.node.isRequired,
  availableRoles: PropTypes.array,
  rescrictedRoutes: PropTypes.array,
};

const PermissionWrapper = ({ children, userRole, history: { location: { pathname } }, availableRoles = [] }) => {
  if (!isArray(availableRoles) || !availableRoles.includes(userRole)) {
    // check rescrictedRoutes and do redirect
    return null
  }
  return children
};

PermissionWrapper.propTypes = propTypes;

const mapStateToProps = (state) => ({
  userRole: 'root',
});

export default connect(mapStateToProps)(withRouter(PermissionWrapper));
