-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prompt user to create root location if its not uploaded yet. (#1303)
* Add Root location creation wizard * Allow to pass query options to locationHierarchy useQuery * Integrate root location wizard and test it * Add Rbac check on the create root location * Internationalize root location wizard
- Loading branch information
1 parent
90d8dca
commit 4f03122
Showing
5 changed files
with
305 additions
and
14 deletions.
There are no files selected for viewing
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
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
198 changes: 198 additions & 0 deletions
198
packages/fhir-location-management/src/components/RootLocationWizard/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 |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/** Give users option to crete root location if one with configured id is not found. */ | ||
import React from 'react'; | ||
import { Alert, Button, Card, Col, Popconfirm, Row, Space, Spin, Typography } from 'antd'; | ||
import { Helmet } from 'react-helmet'; | ||
import { PageHeader, loadAllResources } from '@opensrp/react-utils'; | ||
import { useTranslation } from '../../mls'; | ||
import { URL_LOCATION_UNIT, locationResourceType } from '../../constants'; | ||
import { useHistory } from 'react-router'; | ||
import { postPutLocationUnit } from '../LocationForm/utils'; | ||
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation'; | ||
import { useQuery } from 'react-query'; | ||
import { sendErrorNotification, sendSuccessNotification } from '@opensrp/notifications'; | ||
import { RbacCheck } from '@opensrp/rbac'; | ||
import { Trans } from '@opensrp/i18n'; | ||
|
||
const { Text } = Typography; | ||
|
||
export interface RootLocationWizardProps { | ||
rootLocationId: string; | ||
fhirBaseUrl: string; | ||
} | ||
|
||
export const RootLocationWizard = (props: RootLocationWizardProps) => { | ||
const { rootLocationId, fhirBaseUrl } = props; | ||
const { t } = useTranslation(); | ||
|
||
const { | ||
data: LocationCount, | ||
error, | ||
isLoading, | ||
} = useQuery( | ||
[locationResourceType], | ||
() => { | ||
return loadAllResources(fhirBaseUrl, locationResourceType, { _summary: 'count' }); | ||
}, | ||
{ | ||
select: (res) => res.total, | ||
} | ||
); | ||
|
||
const pageTitle = t('Location Unit Management'); | ||
|
||
return ( | ||
<section className="content-section"> | ||
<Helmet> | ||
<title>{pageTitle}</title> | ||
</Helmet> | ||
<PageHeader title={pageTitle} /> | ||
|
||
<Row className="list-view"> | ||
<Col className="main-content"> | ||
<Card title={t('Root location was not found')} style={{ minHeight: '60vh' }}> | ||
<p> | ||
{t(`Root location with id: {{rootLocationId}} was not found on the server.`, { | ||
rootLocationId, | ||
})} | ||
</p> | ||
|
||
<CardBodyContent | ||
fetching={isLoading} | ||
locationNum={LocationCount} | ||
fhirBaseUrl={fhirBaseUrl} | ||
rootLocationId={rootLocationId} | ||
error={error} | ||
/> | ||
</Card> | ||
</Col> | ||
</Row> | ||
</section> | ||
); | ||
}; | ||
|
||
interface CardBodyContentProps { | ||
fetching: boolean; | ||
locationNum?: number; | ||
fhirBaseUrl: string; | ||
rootLocationId: string; | ||
error?: unknown; | ||
} | ||
|
||
const CardBodyContent = ({ | ||
fetching, | ||
locationNum, | ||
fhirBaseUrl, | ||
rootLocationId, | ||
error, | ||
}: CardBodyContentProps) => { | ||
const { t } = useTranslation(); | ||
const createRootConfirmProps = { | ||
fhirBaseUrl, | ||
rootLocationId, | ||
}; | ||
|
||
if (fetching) { | ||
return ( | ||
<Trans i18nKey="LookingForUploadedLocations" t={t}> | ||
<Spin size="small" /> Looking for uploaded locations on the server. | ||
</Trans> | ||
); | ||
} else if (error || locationNum === undefined) { | ||
return ( | ||
<Space direction="vertical"> | ||
<Alert type="error" message={t('Unable to check if the server has any locations.')} /> | ||
<CreateRootConfirm {...createRootConfirmProps} /> | ||
</Space> | ||
); | ||
} else if (locationNum === 0) { | ||
return ( | ||
<Space direction="vertical"> | ||
<Text>{t('No locations have been uploaded yet.')}</Text> | ||
<CreateRootConfirm {...createRootConfirmProps} /> | ||
</Space> | ||
); | ||
} else { | ||
return ( | ||
<Space direction="vertical"> | ||
<Trans i18nKey={'locationsOnServer'} t={t} locationNum={locationNum}> | ||
<Text>There exists {{ locationNum }} locations on the server.</Text> | ||
<Text> One of these could be the intended but wrongly configured, root location. </Text> | ||
<Text> If you are not sure, kindly reach out to the web administrator for help.</Text> | ||
</Trans> | ||
<CreateRootConfirm {...createRootConfirmProps} /> | ||
</Space> | ||
); | ||
} | ||
}; | ||
|
||
interface CreateRootConfirmProps { | ||
fhirBaseUrl: string; | ||
rootLocationId: string; | ||
} | ||
|
||
const CreateRootConfirm = (props: CreateRootConfirmProps) => { | ||
const { fhirBaseUrl, rootLocationId } = props; | ||
const history = useHistory(); | ||
const { t } = useTranslation(); | ||
|
||
const onOk = () => history.push(URL_LOCATION_UNIT); | ||
|
||
const rootLocationPayload = { | ||
...rootLocationTemplate, | ||
id: rootLocationId, | ||
identifier: [ | ||
{ | ||
use: 'official', | ||
value: rootLocationId, | ||
}, | ||
], | ||
} as ILocation; | ||
|
||
return ( | ||
<RbacCheck | ||
permissions={['Location.create']} | ||
fallback={<Text type="warning">Missing required permissions to create locations</Text>} | ||
> | ||
<Popconfirm | ||
title={t( | ||
`This action will create a new location with id {{rootLocationId}}. The web application will then use the created location as the root location.`, | ||
{ rootLocationId } | ||
)} | ||
okText={t('Proceed')} | ||
cancelText={t('Cancel')} | ||
onConfirm={async () => { | ||
await postPutLocationUnit(rootLocationPayload, fhirBaseUrl) | ||
.then(() => { | ||
sendSuccessNotification(t('Root location uploaded to the server.')); | ||
onOk(); | ||
}) | ||
.catch(() => { | ||
sendErrorNotification( | ||
t('Could not upload the root location at this time, please try again later.') | ||
); | ||
}); | ||
}} | ||
> | ||
<Button type="primary">{t('Create root location.')}</Button> | ||
</Popconfirm> | ||
</RbacCheck> | ||
); | ||
}; | ||
|
||
const rootLocationTemplate = { | ||
resourceType: 'Location', | ||
status: 'active', | ||
name: 'Root FHIR Location', | ||
alias: ['Root Location'], | ||
description: | ||
'This is the Root Location that all other locations are part of. Any locations that are directly part of this should be displayed as the root location.', | ||
physicalType: { | ||
coding: [ | ||
{ | ||
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type', | ||
code: 'jdn', | ||
display: 'Jurisdiction', | ||
}, | ||
], | ||
}, | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/fhir-location-management/src/components/RootLocationWizard/tests/fixtures.ts
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const rootlocationFixture = { | ||
id: 'eff94f33-c356-4634-8795-d52340706ba9', | ||
identifier: [ | ||
{ | ||
use: 'official', | ||
value: 'eff94f33-c356-4634-8795-d52340706ba9', | ||
}, | ||
], | ||
resourceType: 'Location', | ||
status: 'active', | ||
name: 'Root FHIR Location', | ||
alias: ['Root Location'], | ||
description: | ||
'This is the Root Location that all other locations are part of. Any locations that are directly part of this should be displayed as the root location.', | ||
physicalType: { | ||
coding: [ | ||
{ | ||
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type', | ||
code: 'jdn', | ||
display: 'Jurisdiction', | ||
}, | ||
], | ||
}, | ||
}; |
Oops, something went wrong.