This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
357 additions
and
285 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 |
---|---|---|
|
@@ -6,5 +6,3 @@ module.exports = { | |
builder: "@storybook/builder-vite", | ||
}, | ||
}; | ||
|
||
export {}; |
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
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
68 changes: 68 additions & 0 deletions
68
src/components/OrgUnitSelector/components/LevelAndGroupSelector/index.tsx
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,68 @@ | ||
import i18n from "@dhis2/d2-i18n"; | ||
import { MultiSelectField, MultiSelectOption } from "@dhis2/ui"; | ||
import type { OrgUnitSelection } from "@hisptz/dhis2-utils"; | ||
import { intersectionWith } from "lodash"; | ||
import React from "react"; | ||
import { useOrgUnitLevelsAndGroups } from "../../hooks"; | ||
import { onGroupSelect, onLevelSelect } from "../../utils"; | ||
|
||
export function LevelAndGroupSelector({ | ||
showLevels, | ||
showGroups, | ||
value, | ||
onUpdate, | ||
disableSelections, | ||
}: { | ||
showLevels?: boolean; | ||
showGroups?: boolean; | ||
value: OrgUnitSelection | undefined; | ||
onUpdate: ((value: OrgUnitSelection) => void) | undefined; | ||
disableSelections?: boolean; | ||
}) { | ||
const { groups, levels, error: levelsAndGroupsError, loading: levelsAndGroupsLoading } = useOrgUnitLevelsAndGroups(); | ||
|
||
const sanitizedSelectedLevels = intersectionWith(value?.levels, levels, (levelId, level) => levelId === level.id); | ||
const sanitizedSelectedGroups = intersectionWith(value?.groups, groups, (groupId, group) => groupId === group.id); | ||
|
||
return ( | ||
<div className="row pt-32" style={{ gap: 16 }}> | ||
{showLevels && ( | ||
<div data-test="levels-selector" className="column w-100"> | ||
<MultiSelectField | ||
disabled={disableSelections || levelsAndGroupsLoading} | ||
clearable | ||
loading={levelsAndGroupsLoading} | ||
error={levelsAndGroupsError} | ||
validationText={levelsAndGroupsError?.message} | ||
onChange={onLevelSelect({ onUpdate, value })} | ||
selected={levelsAndGroupsLoading ? [] : sanitizedSelectedLevels ?? []} | ||
clearText={i18n.t("Clear")} | ||
label={i18n.t("Select Level(s)")}> | ||
{levels?.map(({ displayName, id }: any) => ( | ||
<MultiSelectOption dataTest={`${displayName}-option`} label={displayName} value={id} key={id} /> | ||
))} | ||
</MultiSelectField> | ||
</div> | ||
)} | ||
{showGroups && ( | ||
<div data-test="groups-selector" className="column w-100"> | ||
<MultiSelectField | ||
disabled={disableSelections || levelsAndGroupsLoading} | ||
clearable | ||
loading={levelsAndGroupsLoading} | ||
error={levelsAndGroupsError} | ||
validationText={levelsAndGroupsError?.message} | ||
onChange={onGroupSelect({ onUpdate, value })} | ||
selected={levelsAndGroupsLoading ? [] : sanitizedSelectedGroups ?? []} | ||
dataTest={"select-facility-group"} | ||
clearText={i18n.t("Clear")} | ||
label={i18n.t("Select Group(s)")}> | ||
{groups?.map(({ displayName, id }) => ( | ||
<MultiSelectOption dataTest={`${displayName}-option`} label={displayName} value={id} key={id} /> | ||
))} | ||
</MultiSelectField> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
87 changes: 87 additions & 0 deletions
87
src/components/OrgUnitSelector/components/OrgUnitTree/index.tsx
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,87 @@ | ||
import i18n from "@dhis2/d2-i18n"; | ||
import { OrganisationUnitTree } from "@dhis2/ui"; | ||
import type { OrganisationUnit, OrgUnitSelection } from "@hisptz/dhis2-utils"; | ||
import { isEmpty } from "lodash"; | ||
import React from "react"; | ||
import { isOrgUnitSelected, onDeselectOrgUnit, onSelectOrgUnit } from "../../utils"; | ||
|
||
export function CustomOrgUnitNodeLabel({ node, limitSelectionToLevels }: { node: { displayName: string; level: number }; limitSelectionToLevels?: number[] }) { | ||
const allowSelection = limitSelectionToLevels?.includes(node.level) ?? true; | ||
return <div style={!allowSelection ? { opacity: 0.5 } : undefined}>{node.displayName}</div>; | ||
} | ||
|
||
export function OrgUnitTree({ | ||
value, | ||
onUpdate, | ||
disableSelections, | ||
filter, | ||
expanded, | ||
handleExpand, | ||
roots, | ||
singleSelection, | ||
keyword, | ||
limitSelectionToLevels, | ||
}: { | ||
value: OrgUnitSelection | undefined; | ||
onUpdate: ((value: OrgUnitSelection) => void) | undefined; | ||
disableSelections?: boolean; | ||
singleSelection?: boolean; | ||
filter: string[]; | ||
expanded: string[]; | ||
handleExpand: (orgUnit: { path: string }) => void; | ||
roots: OrganisationUnit[]; | ||
keyword?: string; | ||
limitSelectionToLevels?: number[]; | ||
}) { | ||
const selectedOrgUnits = value?.orgUnits ?? []; | ||
|
||
const onSelect = (orgUnit: any) => { | ||
if (limitSelectionToLevels !== undefined) { | ||
if (!limitSelectionToLevels.includes(orgUnit.level)) { | ||
return; | ||
} | ||
} | ||
|
||
if (isOrgUnitSelected(selectedOrgUnits ?? [], orgUnit as OrganisationUnit)) { | ||
onDeselectOrgUnit(orgUnit as OrganisationUnit, selectedOrgUnits, { onUpdate, value }); | ||
} else { | ||
onSelectOrgUnit(orgUnit, selectedOrgUnits, { onUpdate, value }); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
style={ | ||
disableSelections | ||
? { | ||
opacity: 0.3, | ||
cursor: "not-allowed", | ||
overflow: "auto", | ||
} | ||
: { overflow: "auto", maxHeight: 400, height: 400 } | ||
}> | ||
{(keyword?.length ?? 0) > 3 && isEmpty(filter) ? ( | ||
<div className="column center align-items-center w-100 h-100"> | ||
<p> | ||
{i18n.t("Could not find organisation units matching keyword ")} | ||
<b>{keyword}</b> | ||
</p> | ||
</div> | ||
) : ( | ||
<OrganisationUnitTree | ||
forceReload | ||
filter={filter} | ||
disableSelection={disableSelections} | ||
selected={selectedOrgUnits?.map((orgUnit) => orgUnit.path)} | ||
expanded={expanded} | ||
handleExpand={handleExpand} | ||
handleCollapse={handleExpand} | ||
renderNodeLabel={(props: any) => <CustomOrgUnitNodeLabel {...props} limitSelectionToLevels={limitSelectionToLevels} />} | ||
roots={roots?.map((root) => root.id)} | ||
onChange={onSelect} | ||
singleSelection={singleSelection} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/components/OrgUnitSelector/components/OrgUnitUserOptions/index.tsx
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,32 @@ | ||
import i18n from "@dhis2/d2-i18n"; | ||
import { CheckboxField, colors } from "@dhis2/ui"; | ||
import { OrgUnitSelection } from "@hisptz/dhis2-utils"; | ||
import React from "react"; | ||
import { onUserOrUnitChange, onUserSubUnitsChange, onUserSubX2Units } from "../../utils"; | ||
|
||
export function OrgUnitUserOptions({ value, onUpdate }: { onUpdate: ((value: OrgUnitSelection) => void) | undefined; value: OrgUnitSelection | undefined }) { | ||
const { userSubX2Unit, userOrgUnit, userSubUnit } = value ?? {}; | ||
|
||
return ( | ||
<div data-test="user-options-selector" style={{ background: colors.grey200 }} className="row space-between p-16"> | ||
<CheckboxField | ||
dataTest={"user-org-unit"} | ||
checked={userOrgUnit} | ||
onChange={onUserOrUnitChange({ onUpdate, value })} | ||
label={i18n.t("User organisation unit")} | ||
/> | ||
<CheckboxField | ||
dataTest={"user-sub-org-unit"} | ||
checked={userSubUnit} | ||
onChange={onUserSubUnitsChange({ onUpdate, value })} | ||
label={i18n.t("User sub-units")} | ||
/> | ||
<CheckboxField | ||
dataTest={"user-sub-x2-org-unit"} | ||
checked={userSubX2Unit} | ||
onChange={onUserSubX2Units({ onUpdate, value })} | ||
label={i18n.t("User sub-x2-units")} | ||
/> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.