Skip to content

Commit

Permalink
WIP dashboard card enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Feb 13, 2025
1 parent dd9cace commit 66911f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Trans } from '@lingui/macro';
import { Resource } from 'fhir/r4b';
import { ParametersParameter, Patient, Resource } from 'fhir/r4b';
import { Link, useLocation } from 'react-router-dom';

import { DashboardCard, DashboardCardTable } from 'src/components/DashboardCard';
import { OverviewCard } from 'src/containers/PatientDetails/PatientOverviewDynamic/components/StandardCard/types';
import { QuestionnaireActionType, HeaderQuestionnaireAction } from 'src/uberComponents/ResourceListPage/actions';

interface Props<T extends Resource> {
export interface StandardCardProps<T extends Resource> {
card: OverviewCard<T>;
patient: Patient;
reload: () => void;
to?: string;
action?: QuestionnaireActionType,
getLaunchContext?: () => Array<ParametersParameter>
}

export function StandardCard<T extends Resource>({ card }: Props<T>) {
export function StandardCard<T extends Resource>({ patient, card, reload, to, action, getLaunchContext }: StandardCardProps<T>) {
const location = useLocation();

return (
Expand All @@ -19,14 +25,26 @@ export function StandardCard<T extends Resource>({ card }: Props<T>) {
key={`cards-${card.key}`}
empty={!card.data.length}
extra={
card.total && card.total > 7 ? (
<Link to={`${location.pathname}/resources/${card.key}`}>
<b>
<Trans>See all</Trans>
{` (${card.total})`}
</b>
</Link>
) : null
<>
{(card.total && card.total > 7) || (typeof to !== 'undefined') ? (
<Link to={to ?? `${location.pathname}/resources/${card.key}`}>
<b>
<Trans>See all</Trans>
{` (${card.total})`}
</b>
</Link>
) : null}
{action ? <HeaderQuestionnaireAction
action={action}
reload={reload}
defaultLaunchContext={
[
{name: "Patient", resource: patient},
...(getLaunchContext ? getLaunchContext() : []),
]}
/>
: null}
</>
}
>
<DashboardCardTable title={card.title} data={card.data} columns={card.columns} getKey={card.getKey} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provenance, Resource } from 'fhir/r4b';
import { Bundle, Resource } from 'fhir/r4b';

export interface OverviewCard<T extends Resource | Resource[]> {
title: string;
Expand All @@ -16,5 +16,5 @@ export interface OverviewCard<T extends Resource | Resource[]> {
}

export type PrepareFunction<T extends Resource> =
| ((resources: T[], provenances: Provenance[], total: number, to?: string) => OverviewCard<T>)
| ((resources: T[], bundle: Bundle<T>, total: number, to?: string) => OverviewCard<T>)
| ((resources: T[]) => OverviewCard<T[]>);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Patient, FhirResource, Resource } from 'fhir/r4b';
import { Patient, FhirResource, Resource, Bundle } from 'fhir/r4b';

Check failure on line 1 in src/containers/PatientDetails/PatientOverviewDynamic/containers/StandardCardContainerFabric/hooks.ts

View workflow job for this annotation

GitHub Actions / Tests

'FhirResource' is declared but its value is never read.

Check failure on line 1 in src/containers/PatientDetails/PatientOverviewDynamic/containers/StandardCardContainerFabric/hooks.ts

View workflow job for this annotation

GitHub Actions / Tests

'Bundle' is declared but its value is never read.

import { extractBundleResources, useService } from '@beda.software/fhir-react';
import { mapSuccess, resolveMap } from '@beda.software/remote-data';
Expand All @@ -16,19 +16,18 @@ export function useStandardCard<T extends Resource>(
prepareFunction: PrepareFunction<T>,
to?: string,
) {
const [response] = useService(async () => {
const [response, manager] = useService(async () => {
return mapSuccess(
await resolveMap({
resourceBundle: getFHIRResources<FhirResource>(query.resourceType, query.search(patient)),
resourceBundle: getFHIRResources<T>(query.resourceType, query.search(patient)),
}),
({ resourceBundle }) => {
const resources = extractBundleResources(resourceBundle);
const resource = resources[query.resourceType];
const provenance = resources.Provenance;

const card: OverviewCard<T> | OverviewCard<T[]> = prepareFunction(
resource as T[],
provenance,
resourceBundle,
resource.length,
to,
);
Expand All @@ -37,5 +36,5 @@ export function useStandardCard<T extends Resource>(
);
}, []);

return { response };
return { response, manager };
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RenderRemoteData } from '@beda.software/fhir-react';

import { ContainerProps } from 'src/components/Dashboard/types';
import { Spinner } from 'src/components/Spinner';
import { StandardCard } from 'src/containers/PatientDetails/PatientOverviewDynamic/components/StandardCard';
import { StandardCard, StandardCardProps } from 'src/containers/PatientDetails/PatientOverviewDynamic/components/StandardCard';
import {
OverviewCard,
PrepareFunction,
Expand All @@ -13,28 +13,32 @@ import { useStandardCard } from 'src/containers/PatientDetails/PatientOverviewDy

interface StandardCardContainerWrapperProps<T extends Resource> extends ContainerProps {
prepareFunction: PrepareFunction<T>;
cardProps: Partial<StandardCardProps<T>>;
}

function StandardCardContainerWrapper<T extends Resource>(props: StandardCardContainerWrapperProps<T>) {
const { patient, widgetInfo, prepareFunction } = props;
const { patient, widgetInfo, prepareFunction, cardProps } = props;

const { response } = useStandardCard(patient, widgetInfo.query!, prepareFunction);
const { response, manager } = useStandardCard(patient, widgetInfo.query!, prepareFunction);

return (
<RenderRemoteData remoteData={response} renderLoading={Spinner}>
{({ card }) => <StandardCard card={card as OverviewCard<T>} />}
{({ card }) => <StandardCard card={card as OverviewCard<T>} patient={patient} reload={manager.reload} {...cardProps}/>}
</RenderRemoteData>
);
}

export function StandardCardContainerFabric<T extends Resource>(prepareFunction: PrepareFunction<T>) {
export function StandardCardContainerFabric<T extends Resource>(
prepareFunction: PrepareFunction<T>,
cardProps:Partial<StandardCardProps<T>>={},
) {
return function StandardCardContainer(props: ContainerProps) {
const { widgetInfo } = props;

if (!widgetInfo.query) {
return <div>Error: no query parameter for the widget.</div>;
}

return <StandardCardContainerWrapper<T> {...props} prepareFunction={prepareFunction} />;
return <StandardCardContainerWrapper<T> {...props} prepareFunction={prepareFunction} cardProps={cardProps} />;
};
}

0 comments on commit 66911f4

Please sign in to comment.