Skip to content

Commit

Permalink
U4X-373:Add patient flags functionality (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mwanje committed Feb 6, 2024
1 parent 74d27a8 commit b11722f
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/esm-patient-flags-app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const rootConfig = require('../../jest.config.js');

const packageConfig = {
...rootConfig,
collectCoverage: false,
};

module.exports = packageConfig;
51 changes: 51 additions & 0 deletions packages/esm-patient-flags-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@ugandaemr/esm-patient-flags-app",
"version": "5.1.0",
"description": "Patient flags microfrontend for UgandaEMR",
"browser": "dist/esm-patient-flags-app.js",
"main": "src/index.ts",
"source": true,
"license": "MPL-2.0",
"scripts": {
"start": "openmrs develop",
"serve": "webpack serve --mode=development",
"debug": "npm run serve",
"build": "webpack --mode production",
"analyze": "webpack --mode=production --env.analyze=true",
"lint": "eslint src --ext ts,tsx",
"typescript": "tsc",
"extract-translations": "i18next 'src/**/*.component.tsx' 'src/index.ts' --config ../../tools/i18next-parser.config.js",
"test": "cross-env TZ=UTC jest --config jest.config.js --verbose false --passWithNoTests",
"test:watch": "cross-env TZ=UTC jest --watch --config jest.config.js",
"coverage": "yarn test --coverage"
},
"browserslist": [
"extends browserslist-config-openmrs"
],
"keywords": [
"openmrs"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/METS-Programme/esm-ugandaemr-core#readme"
},
"bugs": {
"url": "https://github.com/METS-Programme/esm-ugandaemr-core/issues"
},
"dependencies": {
"@carbon/react": "^1.42.1",
"lodash-es": "^4.17.15"
},
"peerDependencies": {
"@openmrs/esm-framework": "5.x",
"react": "^18.1.0",
"react-i18next": "11.x",
"react-router-dom": "6.x"
},
"devDependencies": {
"webpack": "^5.74.0"
}
}
14 changes: 14 additions & 0 deletions packages/esm-patient-flags-app/src/config-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Type } from '@openmrs/esm-framework';

export const configSchema = {
casualGreeting: {
_type: Type.Boolean,
_default: false,
_description: 'Whether to use a casual greeting (or a formal one).',
},
};

export type Config = {
casualGreeting: boolean;
whoToGreet: Array<string>;
};
4 changes: 4 additions & 0 deletions packages/esm-patient-flags-app/src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.css';
declare module '*.scss';
declare module '@carbon/react';
declare type SideNavProps = object;
21 changes: 21 additions & 0 deletions packages/esm-patient-flags-app/src/hooks/usePatientFlags.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { openmrsFetch } from '@openmrs/esm-framework';
import useSWR from 'swr';

export interface Result {
uuid: string;
display: string;
links: Link[];
}

export interface Link {
rel: string;
uri: string;
resourceAlias: string;
}

export const usePatientFlags = (patientUuid: string) => {
const patientFlagsUrl = `/ws/rest/v1/patientflags/patientflag?patient=${patientUuid}`;
const { data, error, isLoading } = useSWR<{ data: { results: Array<Result> } }, Error>(patientFlagsUrl, openmrsFetch);
const patientFlags = typeof data?.data === 'string' ? [] : data?.data?.results ?? [];
return { patientFlags, isLoading, error };
};
17 changes: 17 additions & 0 deletions packages/esm-patient-flags-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfigSchema, getSyncLifecycle } from '@openmrs/esm-framework';
import { configSchema } from './config-schema';
import patientFlagsComponent from './patient-flags/patient-flags.component';

const moduleName = '@ugandaemr/esm-patient-flags-app';

const options = {
featureName: 'patient-flags',
moduleName,
};

export const importTranslation = require.context('../translations', false, /.json$/, 'lazy');
export const patientFlag = getSyncLifecycle(patientFlagsComponent, options);

export function startupApp() {
defineConfigSchema(moduleName, configSchema);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Tag } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { usePatientFlags } from '../hooks/usePatientFlags';
import styles from './patient-flags.scss';

interface PatientFlagsProps {
patientUuid: string;
}

const PatientFlags: React.FC<PatientFlagsProps> = ({ patientUuid }) => {
const { t } = useTranslation();
const { patientFlags, error } = usePatientFlags(patientUuid);

if (error) {
return <span>{t('errorPatientFlags', 'Error loading patient flags')}</span>;
}

return (
<div className={styles.flagContainer}>
{patientFlags.map((patientFlag) => (
<Tag className={styles.tag} key={patientFlag} type="magenta">
{patientFlag?.display}
</Tag>
))}
</div>
);
};

export default PatientFlags;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@use '@carbon/colors';
@use '@carbon/layout';

.flagContainer {
margin: layout.$spacing-05;
}

.tag {
cursor: pointer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import PatientFlags from './patient-flags.component';
import { usePatientFlags } from '../hooks/usePatientFlags';

const mockUsePatientFlags = usePatientFlags as jest.Mock;

jest.mock('../hooks/usePatientFlags', () => {
const originalModule = jest.requireActual('../hooks/usePatientFlags');
return { ...originalModule, usePatientFlags: jest.fn() };
});

describe('<PatientFlags/>', () => {
afterEach(() => {
jest.resetAllMocks();
});

test('should display patient flags', () => {
mockUsePatientFlags.mockReturnValue({ isLoading: false, patientFlags: ['hiv', 'cancer'], error: null });
render(<PatientFlags patientUuid="some-patient-uuid" />);
expect(screen.getByText(/^hiv$/i)).toBeInTheDocument();
expect(screen.getByText(/^cancer$/i)).toBeInTheDocument();
});

test("should display error message when there's an error", () => {
mockUsePatientFlags.mockReturnValue({ isLoading: false, patientFlags: [], error: 'some-error' });
render(<PatientFlags patientUuid="some-patient-uuid" />);
expect(screen.getByText(/Error loading patient flags/i)).toBeInTheDocument();
});
});
17 changes: 17 additions & 0 deletions packages/esm-patient-flags-app/src/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.openmrs.org/routes.schema.json",
"backendDependencies": {
"webservices.rest": "^2.24.0"
},
"pages": [],
"extensions": [
{
"name": "patient-flag",
"slot": "top-of-all-patient-dashboards-slot",
"component": "patientFlag",
"order": 0,
"online": true,
"offline": false
}
]
}
2 changes: 2 additions & 0 deletions packages/esm-patient-flags-app/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
5 changes: 5 additions & 0 deletions packages/esm-patient-flags-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"],
"exclude": ["src/**/*.test.tsx", "src/**/*.outdated.tsx"]
}
1 change: 1 addition & 0 deletions packages/esm-patient-flags-app/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('openmrs/default-webpack-config');
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6782,6 +6782,21 @@ __metadata:
languageName: unknown
linkType: soft

"@ugandaemr/esm-patient-flags-app@workspace:packages/esm-patient-flags-app":
version: 0.0.0-use.local
resolution: "@ugandaemr/esm-patient-flags-app@workspace:packages/esm-patient-flags-app"
dependencies:
"@carbon/react": ^1.42.1
lodash-es: ^4.17.15
webpack: ^5.74.0
peerDependencies:
"@openmrs/esm-framework": 5.x
react: ^18.1.0
react-i18next: 11.x
react-router-dom: 6.x
languageName: unknown
linkType: soft

"@ugandaemr/esm-patient-queues-app@workspace:packages/esm-patient-queues-app":
version: 0.0.0-use.local
resolution: "@ugandaemr/esm-patient-queues-app@workspace:packages/esm-patient-queues-app"
Expand Down

0 comments on commit b11722f

Please sign in to comment.