|
1 |
| -import React from "react" |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { useSimpleTabularView } from '@opensrp/react-utils'; |
| 3 | +import { RouteComponentProps } from 'react-router'; |
| 4 | +import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation'; |
| 5 | +import { locationResourceType, URL_LOCATION_UNIT_EDIT, URL_LOCATION_UNIT_ADD } from '../../constants'; |
| 6 | +import { |
| 7 | + BrokenPage, |
| 8 | + useSearchParams, |
| 9 | + TableLayout, |
| 10 | + viewDetailsQuery, |
| 11 | + PageHeader, |
| 12 | + SearchForm, |
| 13 | +} from '@opensrp/react-utils'; |
| 14 | +import { Dictionary } from '@onaio/utils'; |
| 15 | +import { Helmet } from 'react-helmet'; |
| 16 | +import { useTranslation } from '../../mls'; |
| 17 | +import { Row, Col, Button, Divider, Dropdown, Popconfirm } from 'antd'; |
| 18 | +import { useHistory, Link } from 'react-router-dom'; |
| 19 | +import { RbacCheck, useUserRole } from '@opensrp/rbac'; |
| 20 | +import type { MenuProps } from 'antd'; |
| 21 | +import { MoreOutlined, PlusOutlined } from '@ant-design/icons'; |
| 22 | +import { LocationUnitDetail } from '../LocationUnitDetail'; |
2 | 23 |
|
3 |
| -export const AllLocationListFlat = ()=>{ |
4 |
| - return ( |
5 |
| - <div>All Locations Rendered</div> |
6 |
| - ) |
| 24 | + |
| 25 | + |
| 26 | +interface RouteParams { |
| 27 | + locationId: string | undefined; |
| 28 | +} |
| 29 | + |
| 30 | +interface Props { |
| 31 | + fhirBaseURL: string; |
| 32 | + LocationPageSize: number; |
7 | 33 | }
|
| 34 | + |
| 35 | +const getSearchParams = () => { |
| 36 | + return { "_include": "Location:partof" }; |
| 37 | +}; |
| 38 | + |
| 39 | +export type LocationListPropTypes = Props & RouteComponentProps<RouteParams>; |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => { |
| 44 | + const { fhirBaseURL } = props; |
| 45 | + const { t } = useTranslation(); |
| 46 | + const history = useHistory(); |
| 47 | + const [detailId, setDetailId] = useState<string>(''); |
| 48 | + const { addParam, sParams } = useSearchParams(); |
| 49 | + const userRole = useUserRole(); |
| 50 | + const resourceId = sParams.get(viewDetailsQuery) ?? undefined; |
| 51 | + |
| 52 | + const { |
| 53 | + queryValues: { data, isFetching, isLoading, error, refetch }, |
| 54 | + tablePaginationProps, |
| 55 | + searchFormProps, |
| 56 | + } = useSimpleTabularView<ILocation>(fhirBaseURL, locationResourceType, getSearchParams()); |
| 57 | + |
| 58 | + if (error && !data) { |
| 59 | + return <BrokenPage errorMessage={(error as Error).message} />; |
| 60 | + } |
| 61 | + |
| 62 | + // this is for debugging |
| 63 | + useEffect(() => { |
| 64 | + console.log("Component mounted"); |
| 65 | + console.log("Data:", data); |
| 66 | + console.log("Is Fetching:", isFetching); |
| 67 | + console.log("Is Loading:", isLoading); |
| 68 | + console.log("Error:", error); |
| 69 | + |
| 70 | + return () => { |
| 71 | + console.log("Component unmounted"); |
| 72 | + }; |
| 73 | + }, [data, isFetching, isLoading, error]); |
| 74 | + // TODO delete this (useEffect) |
| 75 | + |
| 76 | + |
| 77 | + const tableData: any[] = (data?.records ?? []).map((datum: Dictionary) => ({ |
| 78 | + key: datum.id, |
| 79 | + id: datum.id, |
| 80 | + name: datum.name, |
| 81 | + })); |
| 82 | + |
| 83 | + type TableData = typeof tableData[0]; |
| 84 | + |
| 85 | + |
| 86 | + const getItems = (record: TableData): MenuProps['items'] => { |
| 87 | + return [ |
| 88 | + |
| 89 | + { |
| 90 | + key: '1', |
| 91 | + permissions: [], |
| 92 | + label: ( |
| 93 | + <Button type="link" onClick={() => addParam(viewDetailsQuery, record.id)}> |
| 94 | + View Details |
| 95 | + </Button> |
| 96 | + ), |
| 97 | + }, |
| 98 | + ] |
| 99 | + .filter((item) => userRole.hasPermissions(item.permissions)) |
| 100 | + .map((item) => { |
| 101 | + const { permissions, ...rest } = item; |
| 102 | + return rest; |
| 103 | + }); |
| 104 | + }; |
| 105 | + |
| 106 | + const columns = [ |
| 107 | + // { |
| 108 | + // title: t('Name'), |
| 109 | + // dataIndex: 'name' as const, |
| 110 | + // editable: true, |
| 111 | + // }, |
| 112 | + { |
| 113 | + title: t('Type'), |
| 114 | + dataIndex: 'resourceType' as const, |
| 115 | + editable: true, |
| 116 | + }, |
| 117 | + { |
| 118 | + title: t('Status'), |
| 119 | + dataIndex: 'status' as const, |
| 120 | + editable: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + title: t('Parent'), |
| 124 | + dataIndex: 'parent' as const, |
| 125 | + editable: true, |
| 126 | + }, |
| 127 | + { |
| 128 | + title: t('Actions'), |
| 129 | + width: '10%', |
| 130 | + |
| 131 | + // eslint-disable-next-line react/display-name |
| 132 | + render: (_: unknown, record: TableData) => ( |
| 133 | + <span className="d-flex align-items-center"> |
| 134 | + <RbacCheck permissions={['Location.update']}> |
| 135 | + <> |
| 136 | + <Link to={`${URL_LOCATION_UNIT_EDIT}/${record.id.toString()}`} className="m-0 p-1"> |
| 137 | + {t('Edit')} |
| 138 | + </Link> |
| 139 | + <Divider type="vertical" /> |
| 140 | + </> |
| 141 | + </RbacCheck> |
| 142 | + <Dropdown |
| 143 | + menu={{ items: getItems(record) }} |
| 144 | + placement="bottomRight" |
| 145 | + arrow |
| 146 | + trigger={['click']} |
| 147 | + > |
| 148 | + <MoreOutlined className="more-options" data-testid="action-dropdown" /> |
| 149 | + </Dropdown> |
| 150 | + </span> |
| 151 | + ), |
| 152 | + }, |
| 153 | + ]; |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + const tableProps = { |
| 158 | + datasource: tableData, |
| 159 | + columns, |
| 160 | + loading: isFetching || isLoading, |
| 161 | + pagination: tablePaginationProps, |
| 162 | + }; |
| 163 | + |
| 164 | + return ( |
| 165 | + <div className="content-section"> |
| 166 | + <Helmet> |
| 167 | + <title>{t('All Locations List')}</title> |
| 168 | + </Helmet> |
| 169 | + <PageHeader title={t('All Locations Flat')} /> |
| 170 | + <Row className="list-view"> |
| 171 | + <Col className="main-content"> |
| 172 | + <div className="main-content__header"> |
| 173 | + <SearchForm {...searchFormProps} /> |
| 174 | + <RbacCheck permissions={['Location.create']}> |
| 175 | + <Link to={URL_LOCATION_UNIT_ADD}> |
| 176 | + <Button type="primary" onClick={() => history.push(URL_LOCATION_UNIT_ADD)}> |
| 177 | + <PlusOutlined /> |
| 178 | + {t('Add Location')} |
| 179 | + </Button> |
| 180 | + </Link> |
| 181 | + </RbacCheck> |
| 182 | + </div> |
| 183 | + <TableLayout {...tableProps} /> |
| 184 | + </Col> |
| 185 | + {detailId ? ( |
| 186 | + <LocationUnitDetail |
| 187 | + fhirBaseUrl={fhirBaseURL} |
| 188 | + onClose={() => setDetailId('')} |
| 189 | + detailId={detailId} |
| 190 | + /> |
| 191 | + ) : null} |
| 192 | + </Row> |
| 193 | + </div> |
| 194 | + ); |
| 195 | +}; |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
0 commit comments