From 22f2a6865eb69a63df045cd7329c81ca7b4d261a Mon Sep 17 00:00:00 2001
From: Peter Muriuki
Date: Mon, 11 Dec 2023 13:56:39 +0300
Subject: [PATCH] Refactor mission data download
---
.../src/components/MissionData/index.tsx | 35 +++++++++++++++--
packages/opensrp-plans/src/helpers/utils.tsx | 38 ++++++++++++++++++-
2 files changed, 67 insertions(+), 6 deletions(-)
diff --git a/packages/opensrp-plans/src/components/MissionData/index.tsx b/packages/opensrp-plans/src/components/MissionData/index.tsx
index 7c21475a8..375d5d6a9 100644
--- a/packages/opensrp-plans/src/components/MissionData/index.tsx
+++ b/packages/opensrp-plans/src/components/MissionData/index.tsx
@@ -11,11 +11,12 @@ import { useTranslation } from '../../mls';
import { loadTasksIndicators, TaskCount, TaskParams } from '../../helpers/dataLoaders';
import { CommonProps, defaultCommonProps } from '@opensrp/plan-form';
import { useHandleBrokenPage } from '@opensrp/react-utils';
-import { BuildDownloadUrl } from '../../helpers/utils';
import {
OPENSRP_BUSINESS_STATUS_HAS_PROBLEM,
OPENSRP_TASK_STATUS_COMPLETED,
} from '../../constants';
+import { sendErrorNotification } from '@opensrp/notifications';
+import { downloadMissionData } from '../../helpers/utils';
const { Title, Text } = Typography;
@@ -98,9 +99,7 @@ const MissionData = (props: MissionDataProps) => {
{t('Number of flagged products')}:
{flaggedProducts}
-
-
-
+
) : null;
@@ -109,3 +108,31 @@ const MissionData = (props: MissionDataProps) => {
MissionData.defaultProps = defaultProps;
export { MissionData };
+
+// Download Mission button.
+
+interface DownloadMissionDataProps {
+ baseUrl: string;
+ plan: PlanDefinition;
+}
+
+export const DownloadMissionData = (props: DownloadMissionDataProps) => {
+ const { baseUrl, plan } = props;
+ const { t } = useTranslation();
+ const [downloading, setDownloading] = useState(false);
+ const handleClick = () => {
+ setDownloading(true);
+ downloadMissionData(baseUrl, plan)
+ .catch(() => {
+ sendErrorNotification(t('Mission data download failed.'));
+ })
+ .finally(() => {
+ setDownloading(false);
+ });
+ };
+ return (
+
+ );
+};
diff --git a/packages/opensrp-plans/src/helpers/utils.tsx b/packages/opensrp-plans/src/helpers/utils.tsx
index 03cc1aadb..b203d41aa 100644
--- a/packages/opensrp-plans/src/helpers/utils.tsx
+++ b/packages/opensrp-plans/src/helpers/utils.tsx
@@ -9,6 +9,9 @@ import {
SERVICE_POINT_CHECK_CODE,
RECORD_GPS_CODE,
} from '@opensrp/plan-form-core';
+import { HTTPMethod } from '@opensrp/server-service';
+import { getFileNameFromCDHHeader, downloadFile } from '@opensrp/react-utils';
+import { OpenSRPService } from './dataLoaders';
/**
* helper to retrieve the plan Type from a plan definition object
@@ -62,7 +65,38 @@ export const PlanLoading = () => {
return ;
};
-export const BuildDownloadUrl = (baseURL: string, planId: string) => {
+const getFetchOptions = (_: AbortSignal, accessToken: string, method: HTTPMethod): RequestInit => {
+ return {
+ headers: {
+ authorization: `Bearer ${accessToken}`,
+ },
+ method,
+ };
+};
+
+/**
+ * download a misisons data.
+ *
+ * @param baseURL - opensrp server base url
+ * @param plan - plan whose mission data we fetching.
+ */
+export const downloadMissionData = async (baseURL: string, plan: PlanDefinition) => {
+ const { title, identifier } = plan;
+ const fileTitle = title.replaceAll(/\\s/g, ' ').split(' ').join('_');
const eventType = `${FLAG_PROBLEM_CODE},${SERVICE_POINT_CHECK_CODE},${LOOKS_GOOD_CODE},${RECORD_GPS_CODE}`;
- return `${baseURL}${OPENSRP_TASK_EXPORT_DATA}?eventTypes=${eventType}&planIdentifier=${planId}`;
+ const exportPath = `${OPENSRP_TASK_EXPORT_DATA}?eventTypes=${eventType}&planIdentifier=${identifier}`;
+
+ const serve = new OpenSRPService(exportPath, baseURL, getFetchOptions);
+ const response = await serve.download();
+
+ // get filename from content-disposition header
+ const contentDispositionHeader = response.headers.get('content-disposition');
+ const fileName = contentDispositionHeader
+ ? getFileNameFromCDHHeader(contentDispositionHeader)
+ : `${fileTitle}_${Date.now()}.zip`;
+
+ // get blob data from response
+ const blob = await response.blob();
+
+ downloadFile(blob, fileName);
};