Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding file attachment support #128

Merged
merged 17 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"react-scroll": "^1.8.2",
"react-test-renderer": "^16.9.0",
"react-waypoint": "^10.3.0",
"react-webcam": "^7.1.1",
"semver": "^7.3.5",
"yup": "^0.29.1"
},
Expand Down
55 changes: 53 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isUuid } from '../utils/boolean-utils';

export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) {
const url = !!encounterUuid ? `/ws/rest/v1/encounter/${encounterUuid}?v=full` : `/ws/rest/v1/encounter?v=full`;

return openmrsFetch(url, {
headers: {
'Content-Type': 'application/json',
Expand All @@ -17,8 +18,43 @@ export function saveEncounter(abortController: AbortController, payload, encount
});
}

export function saveAttachment(patientUuid, field, conceptUuid, date, encounterUUID, abortController) {
const url = '/ws/rest/v1/attachment';

const content = field?.value.value;
const cameraUploadType = typeof content === 'string' && content?.split(';')[0].split(':')[1].split('/')[1];

const formData = new FormData();
const fileCaption = field.id;

formData.append('fileCaption', fileCaption);
formData.append('patient', patientUuid);

if (typeof content === 'object') {
formData.append('file', content);
} else {
formData.append('file', new File([''], `camera-upload.${cameraUploadType}`), `camera-upload.${cameraUploadType}`);
formData.append('base64Content', content);
}
formData.append('encounter', encounterUUID);
formData.append('obsDatetime', date);

return openmrsFetch(url, {
method: 'POST',
signal: abortController.signal,
body: formData,
});
}

export function getAttachmentByUuid(patientUuid: string, encounterUuid: string, abortController: AbortController) {
const attachmentUrl = '/ws/rest/v1/attachment';
return openmrsFetch(`${attachmentUrl}?patient=${patientUuid}&encounter=${encounterUuid}`, {
signal: abortController.signal,
}).then((response) => response.data);
}

export function getConcept(conceptUuid: string, v: string): Observable<any> {
return openmrsObservableFetch(`/ws/rest/v1/concept/${conceptUuid}?v=${v}`).pipe(map(response => response['data']));
return openmrsObservableFetch(`/ws/rest/v1/concept/${conceptUuid}?v=${v}`).pipe(map((response) => response['data']));
}

export function getLocationsByTag(tag: string): Observable<{ uuid: string; display: string }[]> {
Expand All @@ -32,7 +68,7 @@ export async function getPreviousEncounter(patientUuid: string, encounterType: s
let response = await openmrsFetch(`/ws/fhir2/R4/Encounter?${query}`);
if (response.data.entry.length) {
const latestEncounter = response.data.entry[0].resource.id;
response = await openmrsFetch(`/ws/rest/v1/encounter/${latestEncounter}?v=${encounterRepresentation}`)
response = await openmrsFetch(`/ws/rest/v1/encounter/${latestEncounter}?v=${encounterRepresentation}`);
return response.data;
}
return null;
Expand Down Expand Up @@ -101,3 +137,18 @@ export async function fetchClobData(form: OpenmrsForm): Promise<any | null> {

return clobDataResponse;
}

function dataURItoFile(dataURI: string) {
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

// write the bytes of the string to a typed array
const buffer = new Uint8Array(byteString.length);

for (let i = 0; i < byteString.length; i++) {
buffer[i] = byteString.charCodeAt(i);
}

const blob = new Blob([buffer], { type: mimeString });
return blob;
}
30 changes: 28 additions & 2 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export interface SubmissionHandler {
*
* @returns the `initialValue`
*/
getInitialValue: (encounter: OpenmrsEncounter, field: OHRIFormField, allFormFields?: Array<OHRIFormField>) => {};
getInitialValue: (
encounter: OpenmrsEncounter,
field: OHRIFormField,
allFormFields?: Array<OHRIFormField>,
context?: EncounterContext,
) => {};

/**
* Handles field submission.
Expand Down Expand Up @@ -164,6 +169,8 @@ export interface OHRIFormQuestionOptions {
};
isDateTime?: { labelTrue: boolean; labelFalse: boolean };
usePreviousValueDisabled?: boolean;
allowedFileTypes?: Array<string>;
allowMultiple?: boolean;
datasource?: { name: string; config?: Record<string, any> };
isSearchable?: boolean;
}
Expand All @@ -185,7 +192,8 @@ export type RenderType =
| 'encounter-location'
| 'textarea'
| 'toggle'
| 'fixed-value';
| 'fixed-value'
| 'file';

export interface PostSubmissionAction {
applyAction(
Expand Down Expand Up @@ -270,3 +278,21 @@ export interface DataSourceParameters {
name: string;
config?: Record<string, any>;
}

export interface AttachmentResponse {
bytesContentFamily: string;
bytesMimeType: string;
comment: string;
dateTime: string;
uuid: string;
}

export interface Attachment {
id: string;
src: string;
title: string;
description: string;
dateTime: string;
bytesMimeType: string;
bytesContentFamily: string;
}
Loading
Loading