Skip to content

Commit ac63c21

Browse files
committed
Show friendly message when table has no data
1 parent 4d46064 commit ac63c21

File tree

2 files changed

+36
-17
lines changed
  • packages/fhir-location-management/src/components

2 files changed

+36
-17
lines changed

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

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import React, { useState } from 'react';
2-
import { useSimpleTabularView } from '@opensrp/react-utils';
2+
import { useSimpleTabularView, NoData } from '@opensrp/react-utils';
33
import { RouteComponentProps } from 'react-router';
44
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation';
55
import {
66
locationResourceType,
77
URL_LOCATION_UNIT_EDIT,
88
URL_LOCATION_UNIT_ADD,
99
} from '../../constants';
10-
import {
11-
BrokenPage,
12-
TableLayout,
13-
PageHeader,
14-
SearchForm,
15-
} from '@opensrp/react-utils';
10+
import { BrokenPage, TableLayout, PageHeader, SearchForm } from '@opensrp/react-utils';
1611
import { Dictionary } from '@onaio/utils';
1712
import { Helmet } from 'react-helmet';
1813
import { useTranslation } from '../../mls';
@@ -32,9 +27,9 @@ interface Props {
3227
}
3328

3429
const getSearchParams = (search: string | null) => {
35-
const baseSearchParam = {"_include": "Location:partof"};
36-
if(search) {
37-
return {"name:contains": search, ...baseSearchParam}
30+
const baseSearchParam = { _include: 'Location:partof' };
31+
if (search) {
32+
return { 'name:contains': search, ...baseSearchParam };
3833
}
3934
return baseSearchParam;
4035
};
@@ -48,7 +43,7 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
4843
const [_, setDetailId] = useState<string>();
4944

5045
const {
51-
queryValues: { data, isFetching, isLoading, error},
46+
queryValues: { data, isFetching, isLoading, error },
5247
tablePaginationProps,
5348
searchFormProps,
5449
} = useSimpleTabularView<ILocation>(fhirBaseURL, locationResourceType, getSearchParams);
@@ -78,7 +73,8 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
7873
</Button>
7974
),
8075
},
81-
]};
76+
];
77+
};
8278

8379
const columns = [
8480
{
@@ -129,11 +125,34 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
129125
},
130126
];
131127

128+
const AddLocationBtn = () => (
129+
<Button type="primary" onClick={() => history.push(URL_LOCATION_UNIT_ADD)}>
130+
<PlusOutlined />
131+
{t('Add Location')}
132+
</Button>
133+
);
134+
135+
const getTableLocale = () => {
136+
const urlQuery = history.location.search;
137+
const nameSearchActive = urlQuery.includes('search=');
138+
if (!tableData.length && (!isFetching || !isLoading)) {
139+
const description = nameSearchActive
140+
? ''
141+
: t('No data available to display, you can start adding data now ');
142+
return {
143+
emptyText: (
144+
<NoData description={description}>{!nameSearchActive && <AddLocationBtn />}</NoData>
145+
),
146+
};
147+
}
148+
};
149+
132150
const tableProps = {
133151
datasource: tableData,
134152
columns,
135153
loading: isFetching || isLoading,
136154
pagination: tablePaginationProps,
155+
locale: getTableLocale(),
137156
};
138157

139158
return (
@@ -148,10 +167,7 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
148167
<SearchForm {...searchFormProps} />
149168
<RbacCheck permissions={['Location.create']}>
150169
<Link to={URL_LOCATION_UNIT_ADD}>
151-
<Button type="primary" onClick={() => history.push(URL_LOCATION_UNIT_ADD)}>
152-
<PlusOutlined />
153-
{t('Add Location')}
154-
</Button>
170+
<AddLocationBtn />
155171
</Link>
156172
</RbacCheck>
157173
</div>

packages/fhir-location-management/src/components/LocationUnitList/Table.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { URL_LOCATION_UNIT_EDIT } from '../../constants';
77
import { Column, TableLayout } from '@opensrp/react-utils';
88
import { useTranslation } from '../../mls';
99
import { RbacCheck } from '@opensrp/rbac';
10+
import { TableLocale } from 'antd/es/table/interface';
1011

1112
export interface TableData {
1213
id: string;
@@ -21,10 +22,11 @@ export interface TableData {
2122
export interface Props {
2223
data: TableData[];
2324
onViewDetails?: (row: TableData) => void;
25+
locale?: TableLocale;
2426
}
2527

2628
const Table: React.FC<Props> = (props: Props) => {
27-
const { onViewDetails } = props;
29+
const { onViewDetails, ...rest } = props;
2830
const { t } = useTranslation();
2931
const columns: Column<TableData>[] = [
3032
{
@@ -93,6 +95,7 @@ const Table: React.FC<Props> = (props: Props) => {
9395
</>
9496
),
9597
}}
98+
{...rest}
9699
/>
97100
);
98101
};

0 commit comments

Comments
 (0)