Skip to content

Commit

Permalink
Fix useSearchParams to replace the url instead of appending to the ex…
Browse files Browse the repository at this point in the history
…isting (#1219)

* Fix useSearchParams to delete current appended url before appending another

* Add array to determine the key to be deleted

* Use set method instead of append

* Refactor care-teams to use useSearchParams hook

* Update tests

* Correct organization spelling

* Add test for when addParams is called with an existing key

* Refactor to use addParams instead of addParam

addParam only adds one keyValue pair to url at a time and commits this to the url

addParams can add several keyValue pairs in a single call before commiting to url

* Add pathname assertion in the test

---------

Co-authored-by: Peter Muriuki <pmuriuki98@gmail.com>
  • Loading branch information
mutuajames and peterMuriuki committed Jun 29, 2023
1 parent cbe0d8e commit ab58a5a
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 61 deletions.
9 changes: 0 additions & 9 deletions app/src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ import {
CreateEditCareTeam,
URL_CREATE_CARE_TEAM,
URL_EDIT_CARE_TEAM,
URL_CARE_TEAM,
} from '@opensrp/fhir-care-team';
import {
CreateEditUser as FHIRConnectedCreateEditUser,
Expand Down Expand Up @@ -350,14 +349,6 @@ const App: React.FC = () => {
path={URL_FHIR_CARE_TEAM}
component={CareTeamList}
/>
<PrivateComponent
redirectPath={APP_CALLBACK_URL}
disableLoginProtection={DISABLE_LOGIN_PROTECTION}
activeRoles={activeRoles.TEAMS && activeRoles.TEAMS.split(',')}
exact
path={`${URL_CARE_TEAM}/:${ROUTE_PARAM_CARE_TEAM_ID}`}
component={CareTeamList}
/>
<PrivateComponent
redirectPath={APP_CALLBACK_URL}
disableLoginProtection={DISABLE_LOGIN_PROTECTION}
Expand Down
13 changes: 9 additions & 4 deletions packages/fhir-care-team/src/components/ListView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import React from 'react';
import { Helmet } from 'react-helmet';
import { Row, Col, PageHeader, Button, Divider, Dropdown, Menu, Popconfirm } from 'antd';
import { MoreOutlined, PlusOutlined } from '@ant-design/icons';
import { RouteComponentProps, useParams } from 'react-router';
import { RouteComponentProps } from 'react-router';
import { Link } from 'react-router-dom';
import {
FHIRServiceClass,
useTabularViewWithLocalSearch,
BrokenPage,
SearchForm,
TableLayout,
useSearchParams,
viewDetailsQuery,
} from '@opensrp/react-utils';
import {
FHIR_CARE_TEAM,
URL_EDIT_CARE_TEAM,
URL_CARE_TEAM,
URL_CREATE_CARE_TEAM,
careTeamResourceType,
} from '../../constants';
Expand Down Expand Up @@ -59,7 +60,9 @@ export const deleteCareTeam = async (
export const CareTeamList: React.FC<CareTeamListPropTypes> = (props: CareTeamListPropTypes) => {
const { fhirBaseURL } = props;
const { t } = useTranslation();
const { careTeamId: resourceId } = useParams<RouteParams>();

const { addParam, sParams } = useSearchParams();
const resourceId = sParams.get(viewDetailsQuery) ?? undefined;

const {
queryValues: { data, isFetching, isLoading, error, refetch },
Expand Down Expand Up @@ -118,7 +121,9 @@ export const CareTeamList: React.FC<CareTeamListPropTypes> = (props: CareTeamLis
</Popconfirm>
</Menu.Item>
<Menu.Item key="view-details" className="view-details">
<Link to={`${URL_CARE_TEAM}/${record.id}`}>View Details</Link>
<Button type="link" onClick={() => addParam(viewDetailsQuery, record.id)}>
View Details
</Button>
</Menu.Item>
</Menu>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Care Teams list view renders correctly: Search 1 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 1 page 1 1`] = `
<td
class="ant-table-cell"
>
Care Team One
</td>
`;

exports[`Care Teams list view renders correctly: Search 1 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 1 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down Expand Up @@ -56,15 +56,15 @@ exports[`Care Teams list view renders correctly: Search 1 page 1 2`] = `
</td>
`;

exports[`Care Teams list view renders correctly: Search 2 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 2 page 1 1`] = `
<td
class="ant-table-cell"
>
Care Team Three
</td>
`;

exports[`Care Teams list view renders correctly: Search 2 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 2 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down Expand Up @@ -112,15 +112,15 @@ exports[`Care Teams list view renders correctly: Search 2 page 1 2`] = `
</td>
`;

exports[`Care Teams list view renders correctly: Search 3 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 3 page 1 1`] = `
<td
class="ant-table-cell"
>
Care Team Five
</td>
`;

exports[`Care Teams list view renders correctly: Search 3 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 3 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down Expand Up @@ -168,15 +168,15 @@ exports[`Care Teams list view renders correctly: Search 3 page 1 2`] = `
</td>
`;

exports[`Care Teams list view renders correctly: Search 4 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 4 page 1 1`] = `
<td
class="ant-table-cell"
>
test 33
</td>
`;

exports[`Care Teams list view renders correctly: Search 4 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 4 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down Expand Up @@ -224,15 +224,15 @@ exports[`Care Teams list view renders correctly: Search 4 page 1 2`] = `
</td>
`;

exports[`Care Teams list view renders correctly: Search 5 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 5 page 1 1`] = `
<td
class="ant-table-cell"
>
Care Team Six
</td>
`;

exports[`Care Teams list view renders correctly: Search 5 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 5 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down Expand Up @@ -280,15 +280,15 @@ exports[`Care Teams list view renders correctly: Search 5 page 1 2`] = `
</td>
`;

exports[`Care Teams list view renders correctly: Search 6 page 1 1`] = `
exports[`Care Teams list view renders correctly: table row 6 page 1 1`] = `
<td
class="ant-table-cell"
>
Care Team Four
</td>
`;

exports[`Care Teams list view renders correctly: Search 6 page 1 2`] = `
exports[`Care Teams list view renders correctly: table row 6 page 1 2`] = `
<td
class="ant-table-cell"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ describe('Care Teams list view', () => {

await waitForElementToBeRemoved(document.querySelector('.ant-spin'));

expect(document.querySelector('title')).toMatchInlineSnapshot(`
<title>
FHIR Care Team
</title>
`);

document.querySelectorAll('tr').forEach((tr, idx) => {
tr.querySelectorAll('td').forEach((td) => {
expect(td).toMatchSnapshot(`Search ${idx} page 1`);
expect(td).toMatchSnapshot(`table row ${idx} page 1`);
});
});

Expand All @@ -162,18 +168,21 @@ describe('Care Teams list view', () => {

const viewDetailsLink = getByText(/View Details/);
expect(viewDetailsLink).toMatchInlineSnapshot(`
<a
href="/admin/CareTeams/308"
>
<span>
View Details
</a>
</span>
`);
fireEvent.click(viewDetailsLink);
expect(history.location.pathname).toEqual('/admin/CareTeams/308');
expect(history.location.pathname).toEqual('/admin/CareTeams');
expect(history.location.search).toEqual('?viewDetails=308');

await waitForElementToBeRemoved(queryByText(/Fetching Care team/i));
document
.querySelectorAll('.display-block')
.forEach((block) => expect(block).toMatchSnapshot('view details display block'));
document.querySelectorAll('.display-block').forEach((block) => {
expect(block).toMatchSnapshot('view details display block');
});

// close view details
const closeButton = document.querySelector('[data-testid="cancel"]');
fireEvent.click(closeButton);
});
});
13 changes: 7 additions & 6 deletions packages/fhir-care-team/src/components/ViewDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Fragment } from 'react';
import { Col, Button, Alert } from 'antd';
import { CloseOutlined, SyncOutlined } from '@ant-design/icons';
import { useHistory } from 'react-router';
import { useQuery } from 'react-query';
import {
BrokenPage,
Expand All @@ -10,8 +9,10 @@ import {
IdentifierUseCodes,
getResourcesFromBundle,
parseFhirHumanName,
viewDetailsQuery,
useSearchParams,
} from '@opensrp/react-utils';
import { careTeamResourceType, URL_CARE_TEAM } from '../../constants';
import { careTeamResourceType } from '../../constants';
import { useTranslation } from '../../mls';
import { renderObjectAsKeyvalue } from '@opensrp/react-utils';
import { get, groupBy, keyBy } from 'lodash';
Expand Down Expand Up @@ -70,7 +71,7 @@ function categorizeIncludedResources(resources: Resource[], careTeamId: string)
const ViewDetails = (props: ViewDetailsProps) => {
const { careTeamId, fhirBaseURL } = props;
const { t } = useTranslation();
const history = useHistory();
const { removeParam } = useSearchParams();

// fetch this careTeam and include all its referenced resources.
const { data, isLoading, error } = useQuery({
Expand Down Expand Up @@ -143,19 +144,19 @@ const ViewDetails = (props: ViewDetailsProps) => {
))}
</ul>
) : (
<Alert description={t('No managing organizaions found')} type="warning"></Alert>
<Alert description={t('No managing organizations found')} type="warning"></Alert>
),
};

return (
<Col className="view-details-content">
<div className="flex-right">
<Button
data-test-id="cancel"
data-testid="cancel"
icon={<CloseOutlined />}
shape="circle"
type="text"
onClick={() => history.push(URL_CARE_TEAM)}
onClick={() => removeParam(viewDetailsQuery)}
/>
</div>
{error && !data ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ exports[`1157 - view details errors out for careTeam 3500 6`] = `
<div
class="ant-alert-description"
>
No managing organizaions found
No managing organizations found
</div>
</div>
</div>
Expand Down Expand Up @@ -434,7 +434,7 @@ exports[`Closes on clicking cancel (X) 7`] = `
<div
class="ant-alert-description"
>
No managing organizaions found
No managing organizations found
</div>
</div>
</div>
Expand Down Expand Up @@ -674,7 +674,7 @@ exports[`works correctly 7`] = `
<div
class="ant-alert-description"
>
No managing organizaions found
No managing organizations found
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { createBrowserHistory } from 'history';
import { createTestQueryClient } from '../../ListView/tests/utils';
import nock from 'nock';
import { cleanup, fireEvent, render, waitForElementToBeRemoved } from '@testing-library/react';
import { careTeamResourceType } from '../../../constants';
import { createMemoryHistory } from 'history';
import { URL_CARE_TEAM, careTeamResourceType } from '../../../constants';

const history = createBrowserHistory();

Expand Down Expand Up @@ -86,6 +87,9 @@ test('works correctly', async () => {
});

test('Closes on clicking cancel (X) ', async () => {
const history = createMemoryHistory();
history.push(URL_CARE_TEAM);

const localProps = {
...props,
careTeamId: '142534',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const ViewDetails = (props: ViewDetailsProps) => {
renderObjectAsKeyvalue(practitionerKeyValues)
) : (
<Alert
description={t('Organiation does not have any assigned practitioners')}
description={t('Organization does not have any assigned practitioners')}
type="warning"
></Alert>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/locales/eusm/fhir-care-team/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"Name": "",
"Name is Required": "",
"No": "",
"No managing organizaions found": "No managing organizaions found",
"No managing organizations found": "No managing organizations found",
"Participants": "Participants",
"Practitioner Participant": "Practitioner Participant",
"Save": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/locales/eusm/fhir-care-team/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"Name": "",
"Name is Required": "",
"No": "",
"No managing organizaions found": "No managing organizaions found",
"No managing organizations found": "No managing organizations found",
"Participants": "Participants",
"Practitioner Participant": "Practitioner Participant",
"Save": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/locales/eusm/fhir-team-management/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"Inactive": "",
"Must be a valid string": "",
"Name": "",
"Organiation does not have any assigned practitioners": "Organiation does not have any assigned practitioners",
"Organization does not have any assigned practitioners": "Organization does not have any assigned practitioners",
"Organization list": "",
"Organization updated successfully": "",
"Practitioner assignments updated successfully": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/locales/eusm/fhir-team-management/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"Inactive": "",
"Must be a valid string": "",
"Name": "",
"Organiation does not have any assigned practitioners": "Organiation does not have any assigned practitioners",
"Organization does not have any assigned practitioners": "Organization does not have any assigned practitioners",
"Organization list": "",
"Organization updated successfully": "",
"Practitioner assignments updated successfully": "",
Expand Down
Loading

0 comments on commit ab58a5a

Please sign in to comment.