<commit_before>import React, {PropTypes} from 'react'
import _ from 'lodash'

import MultiSelectDropdown from 'shared/components/MultiSelectDropdown'
import DeleteRow from 'src/admin/components/DeleteRow'

// TODO: replace with permissions list from server
const ALL_PERMISSIONS = [
  "NoPermissions",
  "ViewAdmin",
  "ViewChronograf",
  "CreateDatabase",
  "CreateUserAndRole",
  "AddRemoveNode",
  "DropDatabase",
  "DropData",
  "ReadData",
  "WriteData",
  "Rebalance",
  "ManageShard",
  "ManageContinuousQuery",
  "ManageQuery",
  "ManageSubscription",
  "Monitor",
  "CopyShard",
  "KapacitorAPI",
  "KapacitorConfigAPI",
]

const RoleRow = ({
  role: {name, permissions, users},
  role,
  allUsers,
  onDelete,
  onUpdateRoleUsers,
  onUpdateRolePermissions,
}) => {
  const handleUpdateUsers = (u) => {
    onUpdateRoleUsers(role, u.map((n) => ({name: n})))
  }

  const handleUpdatePermissions = (allowed) => {
    onUpdateRolePermissions(role, [{scope: 'all', allowed}])
  }

  const perms = _.get(permissions, ['0', 'allowed'], [])

  return (
    <tr>
      <td>{name}</td>
      <td>
        {
          permissions && permissions.length ?
            <MultiSelectDropdown
              items={ALL_PERMISSIONS}
              selectedItems={perms}
              label={perms.length ? '' : 'Select Permissions'}
              onApply={handleUpdatePermissions}
            /> : '\u2014'
        }
      </td>
      <td>
        {
          allUsers && allUsers.length ?
            <MultiSelectDropdown
              items={allUsers.map((u) => u.name)}
              selectedItems={users.map((u) => u.name)}
              label={users.length ? '' : 'Select Users'}
              onApply={handleUpdateUsers}
            /> : '\u2014'
        }
      </td>
      <td className="text-right" style={{width: "85px"}}>
        <DeleteRow onDelete={onDelete} item={role} />
      </td>
    </tr>
  )
}

const {
  arrayOf,
  func,
  shape,
  string,
} = PropTypes

RoleRow.propTypes = {
  role: shape({
    name: string,
    permissions: arrayOf(shape({
      name: string,
    })),
    users: arrayOf(shape({
      name: string,
    })),
  }).isRequired,
  onDelete: func.isRequired,
  allUsers: arrayOf(shape()),
  onUpdateRoleUsers: func.isRequired,
  onUpdateRolePermissions: func.isRequired,
}

export default RoleRow
<commit_msg>Fix roles bug when no users<commit_after>import React, {PropTypes} from 'react'
import _ from 'lodash'

import MultiSelectDropdown from 'shared/components/MultiSelectDropdown'
import DeleteRow from 'src/admin/components/DeleteRow'

// TODO: replace with permissions list from server
const ALL_PERMISSIONS = [
  "NoPermissions",
  "ViewAdmin",
  "ViewChronograf",
  "CreateDatabase",
  "CreateUserAndRole",
  "AddRemoveNode",
  "DropDatabase",
  "DropData",
  "ReadData",
  "WriteData",
  "Rebalance",
  "ManageShard",
  "ManageContinuousQuery",
  "ManageQuery",
  "ManageSubscription",
  "Monitor",
  "CopyShard",
  "KapacitorAPI",
  "KapacitorConfigAPI",
]

const RoleRow = ({
  role: {name, permissions, users},
  role,
  allUsers,
  onDelete,
  onUpdateRoleUsers,
  onUpdateRolePermissions,
}) => {
  const handleUpdateUsers = (u) => {
    onUpdateRoleUsers(role, u.map((n) => ({name: n})))
  }

  const handleUpdatePermissions = (allowed) => {
    onUpdateRolePermissions(role, [{scope: 'all', allowed}])
  }

  const perms = _.get(permissions, ['0', 'allowed'], [])

  return (
    <tr>
      <td>{name}</td>
      <td>
        {
          permissions && permissions.length ?
            <MultiSelectDropdown
              items={ALL_PERMISSIONS}
              selectedItems={perms}
              label={perms.length ? '' : 'Select Permissions'}
              onApply={handleUpdatePermissions}
            /> : '\u2014'
        }
      </td>
      <td>
        {
          allUsers && allUsers.length ?
            <MultiSelectDropdown
              items={allUsers.map((u) => u.name)}
              selectedItems={users === undefined ? [] : users.map((u) => u.name)}
              label={users && users.length ? '' : 'Select Users'}
              onApply={handleUpdateUsers}
            /> : '\u2014'
        }
      </td>
      <td className="text-right" style={{width: "85px"}}>
        <DeleteRow onDelete={onDelete} item={role} />
      </td>
    </tr>
  )
}

const {
  arrayOf,
  func,
  shape,
  string,
} = PropTypes

RoleRow.propTypes = {
  role: shape({
    name: string,
    permissions: arrayOf(shape({
      name: string,
    })),
    users: arrayOf(shape({
      name: string,
    })),
  }).isRequired,
  onDelete: func.isRequired,
  allUsers: arrayOf(shape()),
  onUpdateRoleUsers: func.isRequired,
  onUpdateRolePermissions: func.isRequired,
}

export default RoleRow

{'fraction_non_alphanumeric': 0.14657360406091371, 'fraction_numerical': 0.004653130287648054, 'mean_word_length': 2.700312989045383, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4421543', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1525, 'n_words': 294}