1
1
import React , { useState } from 'react' ;
2
- import { useSimpleTabularView } from '@opensrp/react-utils' ;
2
+ import { useSimpleTabularView , NoData } from '@opensrp/react-utils' ;
3
3
import { RouteComponentProps } from 'react-router' ;
4
4
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation' ;
5
5
import {
6
6
locationResourceType ,
7
7
URL_LOCATION_UNIT_EDIT ,
8
8
URL_LOCATION_UNIT_ADD ,
9
9
} 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' ;
16
11
import { Dictionary } from '@onaio/utils' ;
17
12
import { Helmet } from 'react-helmet' ;
18
13
import { useTranslation } from '../../mls' ;
@@ -32,9 +27,9 @@ interface Props {
32
27
}
33
28
34
29
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 } ;
38
33
}
39
34
return baseSearchParam ;
40
35
} ;
@@ -48,7 +43,7 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
48
43
const [ _ , setDetailId ] = useState < string > ( ) ;
49
44
50
45
const {
51
- queryValues : { data, isFetching, isLoading, error} ,
46
+ queryValues : { data, isFetching, isLoading, error } ,
52
47
tablePaginationProps,
53
48
searchFormProps,
54
49
} = useSimpleTabularView < ILocation > ( fhirBaseURL , locationResourceType , getSearchParams ) ;
@@ -78,7 +73,8 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
78
73
</ Button >
79
74
) ,
80
75
} ,
81
- ] } ;
76
+ ] ;
77
+ } ;
82
78
83
79
const columns = [
84
80
{
@@ -129,11 +125,34 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
129
125
} ,
130
126
] ;
131
127
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
+
132
150
const tableProps = {
133
151
datasource : tableData ,
134
152
columns,
135
153
loading : isFetching || isLoading ,
136
154
pagination : tablePaginationProps ,
155
+ locale : getTableLocale ( ) ,
137
156
} ;
138
157
139
158
return (
@@ -148,10 +167,7 @@ export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
148
167
< SearchForm { ...searchFormProps } />
149
168
< RbacCheck permissions = { [ 'Location.create' ] } >
150
169
< 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 />
155
171
</ Link >
156
172
</ RbacCheck >
157
173
</ div >
0 commit comments