-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table, integrate search and i18n-lize
- Loading branch information
1 parent
6ec15bf
commit af9955f
Showing
1 changed file
with
197 additions
and
5 deletions.
There are no files selected for viewing
202 changes: 197 additions & 5 deletions
202
packages/fhir-location-management/src/components/AllLocationListFlat/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 |
---|---|---|
@@ -1,7 +1,199 @@ | ||
import React from "react" | ||
import React, { useEffect, useState } from "react"; | ||
import { useSimpleTabularView } from '@opensrp/react-utils'; | ||
import { RouteComponentProps } from 'react-router'; | ||
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation'; | ||
import { locationResourceType, URL_LOCATION_UNIT_EDIT, URL_LOCATION_UNIT_ADD } from '../../constants'; | ||
Check failure on line 5 in packages/fhir-location-management/src/components/AllLocationListFlat/index.tsx GitHub Actions / test (16.17.0, ubuntu-latest)
|
||
import { | ||
BrokenPage, | ||
useSearchParams, | ||
TableLayout, | ||
viewDetailsQuery, | ||
PageHeader, | ||
SearchForm, | ||
} from '@opensrp/react-utils'; | ||
import { Dictionary } from '@onaio/utils'; | ||
import { Helmet } from 'react-helmet'; | ||
import { useTranslation } from '../../mls'; | ||
import { Row, Col, Button, Divider, Dropdown, Popconfirm } from 'antd'; | ||
import { useHistory, Link } from 'react-router-dom'; | ||
import { RbacCheck, useUserRole } from '@opensrp/rbac'; | ||
import type { MenuProps } from 'antd'; | ||
import { MoreOutlined, PlusOutlined } from '@ant-design/icons'; | ||
import { LocationUnitDetail } from '../LocationUnitDetail'; | ||
|
||
export const AllLocationListFlat = ()=>{ | ||
return ( | ||
<div>All Locations Rendered</div> | ||
) | ||
|
||
|
||
interface RouteParams { | ||
locationId: string | undefined; | ||
} | ||
|
||
interface Props { | ||
fhirBaseURL: string; | ||
LocationPageSize: number; | ||
} | ||
|
||
const getSearchParams = () => { | ||
return { "_include": "Location:partof" }; | ||
}; | ||
|
||
export type LocationListPropTypes = Props & RouteComponentProps<RouteParams>; | ||
|
||
|
||
|
||
export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => { | ||
const { fhirBaseURL } = props; | ||
const { t } = useTranslation(); | ||
const history = useHistory(); | ||
const [detailId, setDetailId] = useState<string>(''); | ||
const { addParam, sParams } = useSearchParams(); | ||
const userRole = useUserRole(); | ||
const resourceId = sParams.get(viewDetailsQuery) ?? undefined; | ||
|
||
const { | ||
queryValues: { data, isFetching, isLoading, error, refetch }, | ||
tablePaginationProps, | ||
searchFormProps, | ||
} = useSimpleTabularView<ILocation>(fhirBaseURL, locationResourceType, getSearchParams()); | ||
|
||
if (error && !data) { | ||
return <BrokenPage errorMessage={(error as Error).message} />; | ||
} | ||
|
||
// this is for debugging | ||
useEffect(() => { | ||
console.log("Component mounted"); | ||
console.log("Data:", data); | ||
console.log("Is Fetching:", isFetching); | ||
console.log("Is Loading:", isLoading); | ||
console.log("Error:", error); | ||
|
||
return () => { | ||
console.log("Component unmounted"); | ||
}; | ||
}, [data, isFetching, isLoading, error]); | ||
// TODO delete this (useEffect) | ||
|
||
|
||
const tableData: any[] = (data?.records ?? []).map((datum: Dictionary) => ({ | ||
key: datum.id, | ||
id: datum.id, | ||
name: datum.name, | ||
})); | ||
|
||
type TableData = typeof tableData[0]; | ||
|
||
|
||
const getItems = (record: TableData): MenuProps['items'] => { | ||
return [ | ||
|
||
{ | ||
key: '1', | ||
permissions: [], | ||
label: ( | ||
<Button type="link" onClick={() => addParam(viewDetailsQuery, record.id)}> | ||
View Details | ||
</Button> | ||
), | ||
}, | ||
] | ||
.filter((item) => userRole.hasPermissions(item.permissions)) | ||
.map((item) => { | ||
const { permissions, ...rest } = item; | ||
return rest; | ||
}); | ||
}; | ||
|
||
const columns = [ | ||
// { | ||
// title: t('Name'), | ||
// dataIndex: 'name' as const, | ||
// editable: true, | ||
// }, | ||
{ | ||
title: t('Type'), | ||
dataIndex: 'resourceType' as const, | ||
editable: true, | ||
}, | ||
{ | ||
title: t('Status'), | ||
dataIndex: 'status' as const, | ||
editable: true, | ||
}, | ||
{ | ||
title: t('Parent'), | ||
dataIndex: 'parent' as const, | ||
editable: true, | ||
}, | ||
{ | ||
title: t('Actions'), | ||
width: '10%', | ||
|
||
// eslint-disable-next-line react/display-name | ||
render: (_: unknown, record: TableData) => ( | ||
<span className="d-flex align-items-center"> | ||
<RbacCheck permissions={['Location.update']}> | ||
<> | ||
<Link to={`${URL_LOCATION_UNIT_EDIT}/${record.id.toString()}`} className="m-0 p-1"> | ||
{t('Edit')} | ||
</Link> | ||
<Divider type="vertical" /> | ||
</> | ||
</RbacCheck> | ||
<Dropdown | ||
menu={{ items: getItems(record) }} | ||
placement="bottomRight" | ||
arrow | ||
trigger={['click']} | ||
> | ||
<MoreOutlined className="more-options" data-testid="action-dropdown" /> | ||
</Dropdown> | ||
</span> | ||
), | ||
}, | ||
]; | ||
|
||
|
||
|
||
const tableProps = { | ||
datasource: tableData, | ||
columns, | ||
loading: isFetching || isLoading, | ||
pagination: tablePaginationProps, | ||
}; | ||
|
||
return ( | ||
<div className="content-section"> | ||
<Helmet> | ||
<title>{t('All Locations List')}</title> | ||
</Helmet> | ||
<PageHeader title={t('All Locations Flat')} /> | ||
<Row className="list-view"> | ||
<Col className="main-content"> | ||
<div className="main-content__header"> | ||
<SearchForm {...searchFormProps} /> | ||
<RbacCheck permissions={['Location.create']}> | ||
<Link to={URL_LOCATION_UNIT_ADD}> | ||
<Button type="primary" onClick={() => history.push(URL_LOCATION_UNIT_ADD)}> | ||
<PlusOutlined /> | ||
{t('Add Location')} | ||
</Button> | ||
</Link> | ||
</RbacCheck> | ||
</div> | ||
<TableLayout {...tableProps} /> | ||
</Col> | ||
{detailId ? ( | ||
<LocationUnitDetail | ||
fhirBaseUrl={fhirBaseURL} | ||
onClose={() => setDetailId('')} | ||
detailId={detailId} | ||
/> | ||
) : null} | ||
</Row> | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
|
||
|