Skip to content

Commit

Permalink
New case role selection (#809)
Browse files Browse the repository at this point in the history
* fix: making sure a user is able to see its own role

* feat: prompt instead of radio button if less than 2 roles are selectable

* fix: detection of default assignee for new case invitation

* fix: selection of default role for case creation
  • Loading branch information
nbiton authored and franck-boullier committed May 31, 2019
1 parent 627487e commit 42791f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
7 changes: 6 additions & 1 deletion imports/api/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ const rolesSelectionByOwnership = (userId, unitItem) => {
'members.id': userId
}, {
fields: {
'members.$': 1
'members.$': 1,
roleType: 1
}
})
const { roleVisibility } = meRole.members[0]
Expand All @@ -318,6 +319,10 @@ const rolesSelectionByOwnership = (userId, unitItem) => {
return all
}, [])

if (!types.includes(meRole.roleType)) {
types.push(meRole.roleType)
}

return {
roleType: {
$in: types
Expand Down
45 changes: 27 additions & 18 deletions imports/ui/case-wizard/case-wizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ErrorDialog from '../dialogs/error-dialog'
import Preloader from '../preloader/preloader'
import Units, { collectionName as unitsCollName } from '../../api/units'
import { createCase, clearError } from './case-wizard.actions'
import { placeholderEmailMatcher, roleCanBeOccupantMatcher } from '../../util/matchers'
import { roleCanBeOccupantMatcher } from '../../util/matchers'
import { emailValidator } from '../../util/validators'
import InputRow from '../components/input-row'
import { infoItemMembers } from '../util/static-info-rendering'
Expand Down Expand Up @@ -65,7 +65,7 @@ class CaseWizard extends Component {
}
const { availableRoles } = this.props
const { inputValues } = this.state
if (availableRoles && prevProps.availableRoles === null) {
if (availableRoles.length && !prevProps.availableRoles.length) {
const defaultRole = availableRoles.find(roleObj => roleObj.assignedToYou).type
this.setState({
inputValues: Object.assign({}, inputValues, {
Expand All @@ -79,11 +79,6 @@ class CaseWizard extends Component {

renderRadioButtons = () => {
const { unitItem, userId, inProgress, availableRoles } = this.props
const roleRenderer = ({ type, assignedToYou }) => (
<RadioButton
key={type} value={type} label={type + (assignedToYou ? ' (you)' : '')} disabled={inProgress}
/>
)
let rolesToRender
if (unitItem.ownerIds && unitItem.ownerIds.includes(userId)) {
rolesToRender = availableRoles
Expand All @@ -92,20 +87,24 @@ class CaseWizard extends Component {
// The last bool is used in case of no default assignee, but role is assigned to user (not sure if it's possible)
rolesToRender = availableRoles.filter(role => role.type !== 'Contractor' && (role.hasDefaultAssignee || role.assignedToYou))
}
return rolesToRender.map(roleRenderer)
return rolesToRender.map(({ type, assignedToYou }) => (
<RadioButton
key={type} value={type} label={type + (assignedToYou ? ' (you)' : '')} disabled={inProgress}
/>
))
}

handleRoleChanged = (evt, val) => {
const { inputValues } = this.state
const { default_assigned_to: assignedTo } = this.props.unitItem.components.find(({ name }) => name === val)
const needsNewUser = !this.props.availableRoles.find(role => role.type === val).hasDefaultAssignee

this.setState({
inputValues: Object.assign({}, inputValues, {
mandatory: Object.assign({}, inputValues.mandatory, {
assignedUnitRole: val
})
}),
needsNewUser: placeholderEmailMatcher(assignedTo),
needsNewUser,
newUserCanBeOccupant: roleCanBeOccupantMatcher(val),
newUserIsOccupant: false
})
Expand Down Expand Up @@ -141,7 +140,7 @@ class CaseWizard extends Component {
}

render () {
const { isLoading, fieldValues, unitItem, dispatch, error, inProgress, reportItem } = this.props
const { isLoading, fieldValues, unitItem, dispatch, error, inProgress, reportItem, availableRoles } = this.props
if (isLoading) {
return <Preloader />
}
Expand Down Expand Up @@ -251,13 +250,23 @@ class CaseWizard extends Component {
</div>
</div>
<p className='pv0 f6 bondi-blue'>Assign this case to *</p>
<RadioButtonGroup
name='assignedUnitRole'
onChange={this.handleRoleChanged}
valueSelected={assignedUnitRole}
>
{this.renderRadioButtons()}
</RadioButtonGroup>
{availableRoles.length < 2 ? (
<p className='f7 gray ma0 mt1'>
{
availableRoles.length === 0
? 'This case can\'t be created as you aren\'t allowed to assign anyone to it'
: `This case will be assigned to${availableRoles[0].assignedToYou ? ' you as' : ''} the ${availableRoles[0].type} of this unit`
}
</p>
) : (
<RadioButtonGroup
name='assignedUnitRole'
onChange={this.handleRoleChanged}
valueSelected={assignedUnitRole}
>
{this.renderRadioButtons()}
</RadioButtonGroup>
)}
{needsNewUser && (
<div className='mt3'>
<p className='mv0 pv0 f7 warn-crimson lh-copy'>
Expand Down

0 comments on commit 42791f8

Please sign in to comment.