-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2707 from okauppinen/admin-users-role-fix
AdminUsers role select fix
- Loading branch information
Showing
10 changed files
with
161 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Select, Message } from 'oskari-ui'; | ||
|
||
const ADMIN = 'admin'; | ||
const GUEST = 'anonymous'; | ||
|
||
const getOptionGroup = (type, roles) => { | ||
const options = roles.map(({ name, id }) => ({ label: name, value: id })); | ||
return { | ||
title: type, | ||
label: <Message messageKey={`roles.types.${type}`} />, | ||
options | ||
}; | ||
}; | ||
|
||
export const RoleSelect = ({ state, value, error, multiple, onlyAdmin, onChange }) => { | ||
const { roles, systemRoles } = state; | ||
const system = onlyAdmin | ||
? systemRoles.filter(role => role.type === ADMIN) | ||
: systemRoles.filter(role => role.type !== GUEST); | ||
const options = [ | ||
getOptionGroup('system', system), | ||
getOptionGroup('other', roles) | ||
]; | ||
return <Select | ||
className='t_roles' | ||
mode={multiple ? 'multiple' : null} | ||
status={error ? 'error' : null} | ||
onChange={(value) => onChange(value)} | ||
placeholder={<Message messageKey='usersByRole.selectRole' />} | ||
value={value} | ||
style={multiple ? { width: 210 } : null} | ||
options={options}/>; | ||
}; | ||
|
||
RoleSelect.propTypes = { | ||
state: PropTypes.object.isRequired, | ||
value: PropTypes.any, | ||
onChange: PropTypes.func.isRequired, | ||
multiple: PropTypes.bool, | ||
onlyAdmin: PropTypes.bool, | ||
error: PropTypes.bool | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,40 @@ | ||
import React from 'react'; | ||
import { Select, Message } from 'oskari-ui'; | ||
import PropTypes from 'prop-types'; | ||
import { Message } from 'oskari-ui'; | ||
import styled from 'styled-components'; | ||
import { Block, Content } from './styled'; | ||
import { Block, Content, Button } from './styled'; | ||
import { RoleSelect } from './RoleSelect'; | ||
|
||
const StyledSelect = styled(Select)` | ||
const Margin = styled.div` | ||
margin-bottom: 20px; | ||
`; | ||
const ADMIN = 'admin'; | ||
|
||
const getOptionGroup = (type, roles) => { | ||
const options = roles.map(({ name, id }) => ({ label: name, value: id })); | ||
return { | ||
title: type, | ||
label: <Message messageKey={`roles.types.${type}`} />, | ||
options | ||
}; | ||
}; | ||
|
||
export const UsersByRoleTab = ({ state, controller }) => { | ||
const { usersByRole, roles, systemRoles } = state; | ||
const { users = [], roleId } = usersByRole; | ||
|
||
const { users = [], roleId } = state.usersByRole; | ||
const showNoUsers = roleId && users.length === 0; | ||
const onlyAdmin = systemRoles.filter(role => role.type === ADMIN); | ||
const options = [ | ||
getOptionGroup('system', onlyAdmin), | ||
getOptionGroup('other', roles) | ||
]; | ||
return ( | ||
<Content> | ||
<StyledSelect | ||
className='t_roles' | ||
onChange={(value) => controller.showUsersByRole(value)} | ||
placeholder={<Message messageKey='usersByRole.selectRole' />} | ||
defaultValue={roleId} | ||
options={options}/> | ||
<RoleSelect | ||
onlyAdmin | ||
state={state} | ||
value={roleId} | ||
onChange={controller.showUsersByRole}/> | ||
<Margin /> | ||
{users.map(item => { | ||
const { id, user, firstName, lastName } = item; | ||
const details = firstName || lastName ? ` (${firstName} ${lastName})` : ''; | ||
return ( | ||
<Block key={id}> | ||
<span>{user}{details}</span> | ||
<Button type='edit' onClick={() => controller.editUserFromRoles(id, user)} /> | ||
</Block> | ||
)}) | ||
} | ||
); | ||
})} | ||
{showNoUsers && <Message messageKey='usersByRole.noUsers' />} | ||
</Content> | ||
); | ||
}; | ||
UsersByRoleTab.propTypes = { | ||
state: PropTypes.object.isRequired, | ||
controller: PropTypes.object.isRequired | ||
}; |
Oops, something went wrong.