-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OM-345: extend import options by adding group picker (#80)
* OM-345: extend import options by adding group picker * OM-345: removee redundant state * OM-345: reset value if eu changed
- Loading branch information
1 parent
2d58424
commit c838669
Showing
5 changed files
with
87 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useEffect, useMemo } from 'react'; | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
|
||
import { useModulesManager, useTranslations, Autocomplete } from '@openimis/fe-core'; | ||
import { fetchGroupsAction } from '../actions'; | ||
import { ADMIN_RIGHT, MODULE_NAME } from '../constants'; | ||
|
||
function GroupPicker({ | ||
withLabel = true, withPlaceholder = true, label, onChange, currentGroup, | ||
}) { | ||
const modulesManager = useModulesManager(); | ||
const dispatch = useDispatch(); | ||
const { formatMessage } = useTranslations(MODULE_NAME, modulesManager); | ||
|
||
const { | ||
groups, fetchingGroups, fetchedGroups, errorGroups, | ||
} = useSelector((state) => state.workerVoucher); | ||
const { economicUnit } = useSelector((state) => state.policyHolder); | ||
const rights = useSelector((state) => state.core?.user?.i_user?.rights ?? []); | ||
|
||
const isAdmin = useMemo(() => rights.includes(ADMIN_RIGHT), [rights]); | ||
|
||
useEffect(() => { | ||
const actionParams = ['isDeleted: false']; | ||
|
||
if (!isAdmin && economicUnit?.code) { | ||
actionParams.push(`economicUnitCode:"${economicUnit.code}"`); | ||
} | ||
|
||
dispatch(fetchGroupsAction(modulesManager, actionParams)); | ||
}, [isAdmin, economicUnit, modulesManager, dispatch]); | ||
|
||
const groupLabel = (option) => option.name; | ||
|
||
const handleChange = (selectedGroup) => { | ||
onChange(selectedGroup); | ||
}; | ||
|
||
return ( | ||
<Autocomplete | ||
label={label ?? formatMessage('GroupPicker.label')} | ||
error={errorGroups} | ||
withLabel={withLabel} | ||
withPlaceholder={withPlaceholder} | ||
options={groups} | ||
isLoading={fetchingGroups} | ||
isFetched={fetchedGroups} | ||
value={currentGroup} | ||
getOptionLabel={groupLabel} | ||
onChange={handleChange} | ||
onInputChange={() => {}} | ||
/> | ||
); | ||
} | ||
|
||
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
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