-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update user group form [DHIS2-12294] (#1328)
- Loading branch information
Showing
6 changed files
with
199 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { | ||
composeValidators, | ||
hasValue, | ||
createMaxCharacterLength, | ||
} from '@dhis2/ui' | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import { FormSection, TextField } from '../Form' | ||
import { | ||
useDebouncedUniqueGroupNameValidator, | ||
useDebouncedUniqueGroupCodeValidator, | ||
} from './validators' | ||
|
||
const codeLengthValidator = createMaxCharacterLength(50) | ||
|
||
const BasicInformationSection = React.memo(({ group }) => { | ||
const debouncedUniqueGroupNameValidator = | ||
useDebouncedUniqueGroupNameValidator({ groupName: group?.name }) | ||
const debouncedUniqueGroupCodeValidator = | ||
useDebouncedUniqueGroupCodeValidator({ groupCode: group?.code }) | ||
|
||
return ( | ||
<FormSection title={i18n.t('Basic information')}> | ||
<TextField | ||
required | ||
name="name" | ||
label={i18n.t('Name')} | ||
initialValue={group?.name} | ||
validate={composeValidators( | ||
hasValue, | ||
debouncedUniqueGroupNameValidator | ||
)} | ||
/> | ||
<TextField | ||
name="code" | ||
label={i18n.t('Code')} | ||
helpText={i18n.t('Used in analytics reports.')} | ||
initialValue={group?.code} | ||
validate={composeValidators( | ||
codeLengthValidator, | ||
debouncedUniqueGroupCodeValidator | ||
)} | ||
/> | ||
</FormSection> | ||
) | ||
}) | ||
|
||
BasicInformationSection.propTypes = { | ||
group: PropTypes.object, | ||
} | ||
|
||
export default BasicInformationSection |
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,28 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import { FormSection, TransferField } from '../Form' | ||
|
||
const UserGroupManagementSection = React.memo(({ group, userGroupOptions }) => ( | ||
<FormSection | ||
title={i18n.t('User group management')} | ||
description={i18n.t( | ||
'This group can manage other user groups. Add managed user groups below.' | ||
)} | ||
> | ||
<TransferField | ||
name="managedGroups" | ||
leftHeader={i18n.t('Available user groups')} | ||
rightHeader={i18n.t('Managed user groups')} | ||
options={userGroupOptions} | ||
initialValue={group?.managedGroups.map(({ id }) => id) || []} | ||
/> | ||
</FormSection> | ||
)) | ||
|
||
UserGroupManagementSection.propTypes = { | ||
userGroupOptions: PropTypes.array.isRequired, | ||
group: PropTypes.object, | ||
} | ||
|
||
export default UserGroupManagementSection |
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,74 @@ | ||
import { getAttributeValues } from '../../attributes.js' | ||
|
||
const ATTRIBUTE_VALUES = 'attributeValues' | ||
|
||
export const createPostRequestBody = ({ values, attributes }) => { | ||
const { name, code, members, managedGroups } = values | ||
|
||
return { | ||
name, | ||
code, | ||
users: members.additions.map(({ id }) => ({ id })), | ||
managedGroups: managedGroups.map(id => ({ id })), | ||
attributeValues: getAttributeValues({ attributes, values }), | ||
} | ||
} | ||
|
||
export const createJsonPatchRequestBody = ({ | ||
values, | ||
attributes, | ||
dirtyFields, | ||
}) => { | ||
const dirtyFieldsArray = Object.keys(dirtyFields).filter( | ||
key => dirtyFields[key] | ||
) | ||
const patch = dirtyFieldsArray.reduce((acc, key) => { | ||
if (key !== 'members' && !key.startsWith(ATTRIBUTE_VALUES)) { | ||
const value = | ||
key === 'managedGroups' | ||
? values[key].map(id => ({ id })) | ||
: values[key] | ||
|
||
acc.push({ | ||
op: 'replace', | ||
path: '/' + key, | ||
value: value ?? null, | ||
}) | ||
} | ||
return acc | ||
}, []) | ||
|
||
// Replace all attribute values if any were changed. | ||
// There is no way to update individual attributes. | ||
const anyDirtyAttributes = dirtyFieldsArray.some(key => | ||
key.startsWith(ATTRIBUTE_VALUES) | ||
) | ||
|
||
if (anyDirtyAttributes) { | ||
patch.push({ | ||
op: 'replace', | ||
path: `/${ATTRIBUTE_VALUES}`, | ||
value: getAttributeValues({ attributes, values }), | ||
}) | ||
} | ||
|
||
if (dirtyFields.members) { | ||
const { additions, removals } = values.members | ||
for (const addition of additions) { | ||
patch.push({ | ||
op: 'add', | ||
path: '/users/-', | ||
value: { id: addition.id }, | ||
}) | ||
} | ||
for (const removal of removals) { | ||
patch.push({ | ||
op: 'remove-by-id', | ||
path: '/users', | ||
id: removal.id, | ||
}) | ||
} | ||
} | ||
|
||
return patch | ||
} |
Oops, something went wrong.