Skip to content

Commit

Permalink
Add support for Card suggestions, including FHIR Tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
plarocque4 committed Feb 22, 2024
1 parent ccb9b54 commit 45a5a6f
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion src/views/Patient/MedReqDropDown/cdsHooksCards/cdsHooksCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Button, Card, CardActions, CardContent, Grid, Typography } from '@mui/m

import axios from 'axios';
import Client from 'fhirclient/lib/Client';
import { FhirResource, Task } from 'fhir/r4';

Check warning on line 6 in src/views/Patient/MedReqDropDown/cdsHooksCards/cdsHooksCard.tsx

View workflow job for this annotation

GitHub Actions / Check tsc, lint, and prettier

'FhirResource' is defined but never used

Check warning on line 6 in src/views/Patient/MedReqDropDown/cdsHooksCards/cdsHooksCard.tsx

View workflow job for this annotation

GitHub Actions / Check tsc, lint, and prettier

'Task' is defined but never used

Check warning on line 6 in src/views/Patient/MedReqDropDown/cdsHooksCards/cdsHooksCard.tsx

View workflow job for this annotation

GitHub Actions / Check tsc, lint, and prettier

'FhirResource' is defined but never used

Check warning on line 6 in src/views/Patient/MedReqDropDown/cdsHooksCards/cdsHooksCard.tsx

View workflow job for this annotation

GitHub Actions / Check tsc, lint, and prettier

'Task' is defined but never used

import { Card as HooksCard, Link } from '../../../../cds-hooks/resources/HookTypes';
import { Card as HooksCard, Link, Suggestion, Action } from '../../../../cds-hooks/resources/HookTypes';
import { SmartApp } from '../../../Questionnaire/SmartApp';
import { AppContext, getAppContext } from '../../../Questionnaire/questionnaireUtil';
import PictureAsPdfIcon from '@mui/icons-material/PictureAsPdf';
Expand All @@ -28,13 +29,17 @@ interface CdsHooksCardProps {

const CdsHooksCard = (props: CdsHooksCardProps) => {
const [links, setLinks] = useState<Link[]>([]);
const [suggestions, setSuggestions] = useState<Suggestion[]>([]);
useEffect(() => {
modifySmartLaunchURLs(props.card).then(updatedLinks => {
setLinks(updatedLinks);
console.log(
'CdsHooksCard::useEffect: updated all of the smart links for: ' + props.card?.summary
);
});
if (props?.card?.suggestions) {
setSuggestions(props.card?.suggestions);
}
}, [props.card]);

function retrieveLaunchContext(client: Client, link: Link) {
Expand Down Expand Up @@ -99,6 +104,52 @@ const CdsHooksCard = (props: CdsHooksCardProps) => {
});
}

const buttonClickSuggestion = (suggestion: Suggestion) => {
console.log('CdsHooksCard::buttonClickSuggestion: ' + suggestion.label);
let uri = '';
suggestion?.actions?.forEach((action: Action) => {
if (action.type.toUpperCase() === 'DELETE') {
uri = action.resource.resourceType + '/' + action.resource.id;
console.log('completing suggested action DELETE: ' + uri);
props.client.delete(uri).then(result => {
console.log('suggested action DELETE result:');
console.log(result);
});
} else if (action.type.toUpperCase() === 'CREATE') {
uri = action.resource.resourceType;
console.log('completing suggested action CREATE: ' + uri);

props.client.request({
url: action.resource.resourceType,
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(action.resource)
}).then(result => {
console.log('suggested action CREATE result:');
console.log(result);
});
} else if (action.type.toUpperCase() === 'UPDATE') {
uri = action.resource.resourceType + '/' + action.resource.id;
console.log('completing suggested action UPDATE: ' + uri);
props.client.request({
url: action.resource.resourceType + '/' + action.resource.id,
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(action.resource)
}).then(result => {
console.log('suggested action UPDATE result:');
console.log(result);
});
} else {
console.log('WARNING: unknown action: ' + action.type);
}
});
};

const buttonClickAction = (link: Link) => {
console.log('CdsHooksCard::buttonClickAction(' + link.type + '): ' + link.label);
if (link.type === 'absolute') {
Expand Down Expand Up @@ -195,6 +246,19 @@ const CdsHooksCard = (props: CdsHooksCardProps) => {
})}
</Grid>
</CardActions>
<CardActions>
<Grid container spacing={1}>
{suggestions.map((suggestion: Suggestion) => {
return (
<Grid item key={suggestion?.label}>
<Button variant="contained" onClick={() => buttonClickSuggestion(suggestion)}>
{suggestion?.label}
</Button>
</Grid>
);
})}
</Grid>
</CardActions>
</Card>
</Grid>
);
Expand Down

0 comments on commit 45a5a6f

Please sign in to comment.