|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Helmet } from 'react-helmet'; |
| 3 | +import { get } from 'lodash'; |
| 4 | +import { Row, Col, Button, Spin, Alert } from 'antd'; |
| 5 | +import { PageHeader } from '@opensrp/react-utils'; |
| 6 | +import { PlusOutlined } from '@ant-design/icons'; |
| 7 | +import { LocationUnitDetail } from '../LocationUnitDetail'; |
| 8 | +import { useHistory } from 'react-router-dom'; |
| 9 | +import { BrokenPage, Resource404 } from '@opensrp/react-utils'; |
| 10 | +import { URL_LOCATION_UNIT_ADD } from '../../constants'; |
| 11 | +import Table, { TableData } from './Table'; |
| 12 | +import Tree from '../LocationTree'; |
| 13 | +import { useGetLocationHierarchy } from '../../helpers/utils'; |
| 14 | +import './LocationUnitList.css'; |
| 15 | +import { TreeNode } from '../../helpers/types'; |
| 16 | +import { useSelector, useDispatch } from 'react-redux'; |
| 17 | +import reducerRegistry from '@onaio/redux-reducer-registry'; |
| 18 | +import { |
| 19 | + reducerName, |
| 20 | + reducer, |
| 21 | + setSelectedNode, |
| 22 | + getSelectedNode, |
| 23 | +} from '../../ducks/location-tree-state'; |
| 24 | +import { useTranslation } from '../../mls'; |
| 25 | +import { RbacCheck } from '@opensrp/rbac'; |
| 26 | +import { RootLocationWizard } from '../RootLocationWizard'; |
| 27 | + |
| 28 | +reducerRegistry.register(reducerName, reducer); |
| 29 | + |
| 30 | +interface LocationUnitListProps { |
| 31 | + fhirBaseURL: string; |
| 32 | + fhirRootLocationId: string; // This is the location.id field. |
| 33 | +} |
| 34 | + |
| 35 | +export interface AntTreeData { |
| 36 | + data: TreeNode; |
| 37 | + title: JSX.Element; |
| 38 | + key: string; |
| 39 | + children: AntTreeData[]; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Parse the hierarchy node into table data |
| 44 | + * |
| 45 | + * @param hierarchy - hierarchy node to be parsed |
| 46 | + * @returns array of table data |
| 47 | + */ |
| 48 | +export function parseTableData(hierarchy: TreeNode[]) { |
| 49 | + const data: TableData[] = []; |
| 50 | + hierarchy.forEach((location) => { |
| 51 | + const { model } = location; |
| 52 | + data.push({ |
| 53 | + id: model.node.id, |
| 54 | + key: model.nodeId, |
| 55 | + name: model.node.name, |
| 56 | + partOf: model.node.partOf?.display ?? '-', |
| 57 | + description: model.node?.description, |
| 58 | + status: model.node?.status, |
| 59 | + physicalType: get(model.node, 'physicalType.coding.0.display'), |
| 60 | + }); |
| 61 | + }); |
| 62 | + return data; |
| 63 | +} |
| 64 | + |
| 65 | +export const LocationUnitList: React.FC<LocationUnitListProps> = (props: LocationUnitListProps) => { |
| 66 | + const { fhirBaseURL, fhirRootLocationId } = props; |
| 67 | + const [detailId, setDetailId] = useState<string>(); |
| 68 | + const selectedNode = useSelector((state) => getSelectedNode(state)); |
| 69 | + const dispatch = useDispatch(); |
| 70 | + const { t } = useTranslation(); |
| 71 | + const history = useHistory(); |
| 72 | + const [showWizard, setShowWizard] = useState(false); |
| 73 | + |
| 74 | + // get parent location Id |
| 75 | + |
| 76 | + // get the root locations. the root node is the opensrp root location, its immediate children |
| 77 | + // are the user-defined root locations. |
| 78 | + const { |
| 79 | + data: treeData, |
| 80 | + isLoading: treeIsLoading, |
| 81 | + error: treeError, |
| 82 | + isFetching: treeIsFetching, |
| 83 | + } = useGetLocationHierarchy(fhirBaseURL, fhirRootLocationId, { |
| 84 | + enabled: !showWizard, |
| 85 | + onError: (error) => { |
| 86 | + if (error.statusCode === 404) { |
| 87 | + setShowWizard(true); |
| 88 | + } |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + if (treeIsLoading) { |
| 93 | + return <Spin size="large" className="custom-spinner" />; |
| 94 | + } |
| 95 | + |
| 96 | + if (showWizard) { |
| 97 | + return <RootLocationWizard fhirBaseUrl={fhirBaseURL} rootLocationId={fhirRootLocationId} />; |
| 98 | + } |
| 99 | + |
| 100 | + if (treeError && !treeData) { |
| 101 | + return <BrokenPage errorMessage={`${treeError.message}`} />; |
| 102 | + } |
| 103 | + |
| 104 | + if (!treeData) { |
| 105 | + return <Resource404 />; |
| 106 | + } |
| 107 | + |
| 108 | + // generate table data; consider if there is a selected node, sorting the data |
| 109 | + const toDispNodes = |
| 110 | + (selectedNode ? (selectedNode.children as TreeNode[]) : treeData.children) ?? []; |
| 111 | + const sortedNodes = [...toDispNodes].sort((a, b) => |
| 112 | + a.model.node.name.localeCompare(b.model.node.name) |
| 113 | + ); |
| 114 | + let tableNodes = sortedNodes; |
| 115 | + // if a node is selected only its children should be selected, the selected node should come first anyway. |
| 116 | + if (selectedNode) { |
| 117 | + tableNodes = [selectedNode, ...sortedNodes]; |
| 118 | + } |
| 119 | + const tableDispData = parseTableData(tableNodes); |
| 120 | + const pageTitle = t('Location Unit Management'); |
| 121 | + |
| 122 | + return ( |
| 123 | + <> |
| 124 | + {treeIsFetching && ( |
| 125 | + <Alert |
| 126 | + type="info" |
| 127 | + message={t('Refreshing data')} |
| 128 | + description={t( |
| 129 | + 'Request to update the location hierarchy is taking a bit long to respond.' |
| 130 | + )} |
| 131 | + banner |
| 132 | + showIcon |
| 133 | + /> |
| 134 | + )} |
| 135 | + <section className="content-section"> |
| 136 | + <Helmet> |
| 137 | + <title>{pageTitle}</title> |
| 138 | + </Helmet> |
| 139 | + <PageHeader title={pageTitle} /> |
| 140 | + <Row> |
| 141 | + <Col className="bg-white p-3" span={6}> |
| 142 | + <Tree |
| 143 | + data-testid="hierarchy-display" |
| 144 | + data={treeData.children} |
| 145 | + selectedNode={selectedNode} |
| 146 | + onSelect={(node) => { |
| 147 | + dispatch(setSelectedNode(node)); |
| 148 | + }} |
| 149 | + /> |
| 150 | + </Col> |
| 151 | + <Col className="bg-white p-3 border-left" span={detailId ? 13 : 18}> |
| 152 | + <div className="mb-3 d-flex justify-content-between p-3"> |
| 153 | + <h6 className="mt-4"> |
| 154 | + {selectedNode ? selectedNode.model.node.name : t('Location Unit')} |
| 155 | + </h6> |
| 156 | + <RbacCheck permissions={['Location.create']}> |
| 157 | + <div> |
| 158 | + <Button |
| 159 | + type="primary" |
| 160 | + onClick={() => { |
| 161 | + if (selectedNode) { |
| 162 | + const queryParams = { parentId: selectedNode.model.nodeId }; |
| 163 | + const searchString = new URLSearchParams(queryParams).toString(); |
| 164 | + history.push(`${URL_LOCATION_UNIT_ADD}?${searchString}`); |
| 165 | + } else { |
| 166 | + history.push(URL_LOCATION_UNIT_ADD); |
| 167 | + } |
| 168 | + }} |
| 169 | + > |
| 170 | + <PlusOutlined /> |
| 171 | + {t('Add Location Unit')} |
| 172 | + </Button> |
| 173 | + </div> |
| 174 | + </RbacCheck> |
| 175 | + </div> |
| 176 | + <div className="bg-white p-3"> |
| 177 | + <Table |
| 178 | + data={tableDispData} |
| 179 | + onViewDetails={async (row) => { |
| 180 | + setDetailId(row.id); |
| 181 | + }} |
| 182 | + /> |
| 183 | + </div> |
| 184 | + </Col> |
| 185 | + </Row> |
| 186 | + </section> |
| 187 | + </> |
| 188 | + ); |
| 189 | +}; |
0 commit comments