Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e5bf3d

Browse files
committedMar 7, 2024··
Use computed variable instead of UseEffect
Update getItems - to handle onClick later
1 parent 16bf5a9 commit 5e5bf3d

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed
 

‎packages/fhir-location-management/src/components/AllLocationListFlat/index.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useMemo } from 'react';
22
import { useSimpleTabularView, NoData } from '@opensrp/react-utils';
33
import { RouteComponentProps } from 'react-router';
44
import {
@@ -7,7 +7,6 @@ import {
77
URL_LOCATION_UNIT_ADD,
88
} from '../../constants';
99
import { BrokenPage, TableLayout, PageHeader, SearchForm } from '@opensrp/react-utils';
10-
import { Dictionary } from '@onaio/utils';
1110
import { Helmet } from 'react-helmet';
1211
import { useTranslation } from '../../mls';
1312
import { Row, Col, Button, Divider, Dropdown } from 'antd';
@@ -45,8 +44,6 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
4544
const { fhirBaseURL } = props;
4645
const { t } = useTranslation();
4746
const history = useHistory();
48-
const [_, setDetailId] = useState<string>();
49-
const [tableData, setTableData] = useState<Dictionary<string>[]>([]);
5047

5148
const {
5249
queryValues: { data, isFetching, isLoading, error },
@@ -59,23 +56,21 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
5956
getEntryFromBundle
6057
);
6158

62-
useEffect(() => {
63-
const updatedTableData = getTableData(data?.records ?? []);
64-
setTableData(updatedTableData);
65-
}, [data]);
59+
const tableData = useMemo(() => getTableData(data?.records ?? []), [data]);
6660

6761
if (error && !data) {
6862
return <BrokenPage errorMessage={(error as Error).message} />;
6963
}
7064

7165
type TableData = typeof tableData[0];
7266

73-
const getItems = (record: TableData): MenuProps['items'] => {
67+
const getItems = (_: TableData): MenuProps['items'] => {
68+
// Todo: replace _ above when handling onClick
7469
return [
7570
{
7671
key: '1',
7772
label: (
78-
<Button disabled type="link" onClick={() => setDetailId(record.id)}>
73+
<Button disabled type="link">
7974
{t('View details')}
8075
</Button>
8176
),
@@ -145,7 +140,7 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
145140
if (!tableData.length && (!isFetching || !isLoading)) {
146141
const description = nameSearchActive
147142
? ''
148-
: t('No data available to display, you can start adding data now ');
143+
: t('No data available to display, you can start adding data now');
149144
return {
150145
emptyText: (
151146
<NoData description={description}>{!nameSearchActive && <AddLocationBtn />}</NoData>

‎packages/fhir-location-management/src/components/AllLocationListFlat/utils.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function getResourceParentName(
2727
return '';
2828
}
2929
const parentId = partOfRef.split('/')[1];
30-
const parentResource: Dictionary = resourcesById[parentId];
31-
const parentName = parentResource.name;
30+
const parentResource: Dictionary = resourcesById[parentId] ?? {};
31+
const parentName = parentResource?.name;
3232
return parentName || partOfDisplay || '';
3333
}
3434

@@ -52,7 +52,7 @@ export function getTableData(data: BundleEntry[]) {
5252
key: resource.id,
5353
id: resource.id,
5454
name: resource.name,
55-
type: resource.physicalType.coding[0].display,
55+
type: resource.physicalType?.coding[0]?.display,
5656
status: resource.status,
5757
parent: getResourceParentName(resource, resourcesById),
5858
};

0 commit comments

Comments
 (0)
Please sign in to comment.