-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CM-404: move individual to another group (#36)
Co-authored-by: Jan <j.dolkowski@soldevelo.com>
- Loading branch information
1 parent
d0eb0c7
commit 1ac979e
Showing
4 changed files
with
183 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useState } from 'react'; | ||
import { injectIntl } from 'react-intl'; | ||
|
||
import { withTheme, withStyles } from '@material-ui/core/styles'; | ||
import { | ||
Button, Dialog, DialogActions, DialogContent, DialogTitle, | ||
} from '@material-ui/core'; | ||
import { useTranslations, useModulesManager } from '@openimis/fe-core'; | ||
import GroupPicker from '../pickers/GroupPicker'; | ||
|
||
const styles = (theme) => ({ | ||
primaryButton: theme.dialog.primaryButton, | ||
secondaryButton: theme.dialog.secondaryButton, | ||
}); | ||
|
||
function GroupChangeDialog({ | ||
classes, | ||
confirmState, | ||
onClose, | ||
onConfirm, | ||
groupIndividual, | ||
}) { | ||
const modulesManager = useModulesManager(); | ||
const { formatMessage, formatMessageWithValues } = useTranslations('individual', modulesManager); | ||
const [groupToBeChanged, setGroupToBeChanged] = useState(null); | ||
|
||
const handleConfirm = (groupToBeChanged) => { | ||
onConfirm(groupToBeChanged); | ||
onClose(); | ||
}; | ||
|
||
return ( | ||
<Dialog open={confirmState} onClose={onClose}> | ||
<DialogTitle> | ||
{formatMessageWithValues('groupChangeDialog.confirmTitle', { | ||
firstName: groupIndividual?.individual?.firstName, lastName: groupIndividual?.individual?.lastName, | ||
})} | ||
</DialogTitle> | ||
<DialogContent> | ||
<GroupPicker | ||
groupIndividual={groupIndividual} | ||
onChange={setGroupToBeChanged} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={() => handleConfirm(groupToBeChanged)} | ||
autoFocus | ||
className={classes.primaryButton} | ||
disabled={!groupToBeChanged} | ||
> | ||
{formatMessage('confirm')} | ||
</Button> | ||
<Button onClick={onClose} className={classes.secondaryButton}> | ||
{formatMessage('cancel')} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
} | ||
|
||
export default injectIntl(withTheme(withStyles(styles)(GroupChangeDialog))); |
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,60 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
import { | ||
useModulesManager, useTranslations, Autocomplete, | ||
} from '@openimis/fe-core'; | ||
import { fetchGroups } from '../actions'; | ||
|
||
function GroupPicker(props) { | ||
const { | ||
withLabel = true, | ||
withPlaceholder, | ||
label, | ||
groupIndividual, | ||
onChange, | ||
} = props; | ||
|
||
const modulesManager = useModulesManager(); | ||
const dispatch = useDispatch(); | ||
const { formatMessage } = useTranslations('individual', modulesManager); | ||
const fetchingGroups = useSelector((state) => state.individual.fetchingGroups); | ||
const fetchedGroups = useSelector((state) => state.individual.fetchedGroups); | ||
const errorGroups = useSelector((state) => state.individual.errorGroups); | ||
const groups = useSelector((state) => state.individual.groups); | ||
const [group, setGroup] = useState(null); | ||
|
||
useEffect(() => { | ||
if (!fetchingGroups && !fetchedGroups) { | ||
dispatch(fetchGroups({})); | ||
} | ||
}, []); | ||
|
||
const groupLabel = (option) => option.id; | ||
|
||
const getGroupsWithoutCurrentGroup = (options) => options.filter( | ||
(option) => option?.id !== groupIndividual?.group?.id, | ||
); | ||
|
||
const handleChange = (group) => { | ||
onChange(group); | ||
setGroup(group); | ||
}; | ||
|
||
return ( | ||
<Autocomplete | ||
label={label ?? formatMessage('groupPicker.label')} | ||
error={errorGroups} | ||
withLabel={withLabel} | ||
withPlaceholder={withPlaceholder} | ||
options={getGroupsWithoutCurrentGroup(groups)} | ||
isLoading={fetchingGroups} | ||
isFetched={fetchedGroups} | ||
value={group} | ||
getOptionLabel={groupLabel} | ||
onChange={handleChange} | ||
onInputChange={() => null} | ||
/> | ||
); | ||
} | ||
|
||
export default GroupPicker; |
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