Skip to content

Commit

Permalink
Abstract edit link
Browse files Browse the repository at this point in the history
  • Loading branch information
kahummer committed Jul 10, 2024
1 parent cd852a1 commit 7ce4198
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { useMls } from '../../mls';
import { URL_LOCATION_UNIT_EDIT, URL_SERVICE_POINT_ADD_EDIT } from '../../constants';
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation';

interface EditLinkProps {
location: ILocation;
}

const EditLink: React.FC<EditLinkProps> = ({ location }) => {
const { t } = useMls();
const { id, physicalType } = location;
const isBuilding = physicalType?.coding?.[0].code === 'bu';

return (
<Link
to={`${isBuilding ? URL_SERVICE_POINT_ADD_EDIT : URL_LOCATION_UNIT_EDIT}/${id}`}
className="m-0 p-1"
>
{t('Edit')}
</Link>
);
};

export { EditLink };
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation';

export const Location: ILocation = {
resourceType: 'Location',
id: '303',
meta: {
versionId: '4',
lastUpdated: '2021-10-14T13:12:22.740+00:00',
source: '#13bbc7f09daa1751',
},
identifier: [{ use: 'official', value: '93bc9c3d-6321-41b0-9b93-1275d7114e22' }],
status: 'active',
name: 'Yosemite',
description: 'The Sub location',
partOf: { reference: 'Location/2252', display: 'Root FHIR Location' },
type: [
{
coding: [
{
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type',
code: 'bu',
display: 'Building',
},
],
},
],
physicalType: {
coding: [
{
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type',
code: 'bu',
display: 'Building',
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import { render } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import '@testing-library/jest-dom/extend-expect';
import { URL_LOCATION_UNIT_EDIT, URL_SERVICE_POINT_ADD_EDIT } from '../../../constants';
import { EditLink } from '..';
import { Location } from './fixtures';

// Mock the useMls hook
jest.mock('../../mls', () => ({
useMls: () => ({ t: (key: string) => key }),
}));

describe('EditLink', () => {
it('renders the correct link for building locations', () => {
const { getByText } = render(
<Router>
<EditLink location={Location} />
</Router>
);

const link = getByText('Edit');
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute('href', `${URL_SERVICE_POINT_ADD_EDIT}/1`);
});

it('renders the correct link for non-building locations', () => {
const location = {
...Location,
physicalType: {
coding: [
{
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type',
code: 'jdn',
display: 'Jurisdiction',
},
],
},
};

const { getByText } = render(
<Router>
<EditLink location={location} />
</Router>
);

const link = getByText('Edit');
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute('href', `${URL_LOCATION_UNIT_EDIT}/2`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Link, useLocation } from 'react-router-dom';
import { parseLocationDetails } from '../utils';
import { BACK_SEARCH_PARAM, URL_LOCATION_UNIT_EDIT } from '../../../constants';

Check failure on line 7 in packages/fhir-location-management/src/components/ViewDetails/LocationDetails/index.tsx

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-latest)

'URL_LOCATION_UNIT_EDIT' is defined but never used. Allowed unused vars must match /^_/u
import { RbacCheck } from '@opensrp/rbac';
import { EditLink } from '../../EditLink';

const GeometryRender = ({ geometry }: { geometry?: string }) => {
let formattedGeo = geometry ?? '';
Expand Down Expand Up @@ -74,7 +75,7 @@ export const LocationDetails = ({ location }: { location: ILocation }) => {
headerRightData={dateCreatedKeyPairing}
headerActions={
<RbacCheck permissions={['Location.update']}>
<Link to={`${URL_LOCATION_UNIT_EDIT}/${id}?${backToParam}`}>{t('Edit details')}</Link>
<EditLink location={location} />
</RbacCheck>
}
bodyData={otherDetailsMap}
Expand Down

0 comments on commit 7ce4198

Please sign in to comment.