From 4a938efc805d0e28704ea5d3a2059381fb354481 Mon Sep 17 00:00:00 2001 From: NSUWAL123 Date: Thu, 14 Dec 2023 13:20:56 +0545 Subject: [PATCH] fix (projectInfo): submissionJsonDownload - api fetch technuique changed for project-submission json download --- src/frontend/src/api/task.ts | 84 ++++++++++++++++++++++++++ src/frontend/src/views/ProjectInfo.tsx | 12 +++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/frontend/src/api/task.ts b/src/frontend/src/api/task.ts index 787d8b3f06..19b5158fe6 100644 --- a/src/frontend/src/api/task.ts +++ b/src/frontend/src/api/task.ts @@ -68,6 +68,90 @@ export const getDownloadProjectSubmission: Function = (url: string) => { }; }; +export const getDownloadProjectSubmissionJson: Function = (url: string) => { + return async (dispatch) => { + dispatch( + CoreModules.TaskActions.GetDownloadProjectSubmissionLoading({ + type: 'json', + loading: true, + }), + ); + + const getProjectSubmission = async (url: string) => { + try { + const response = await CoreModules.axios.post(url); + dispatch( + CommonActions.SetSnackBar({ + open: true, + message: response.data.Message, + variant: 'success', + duration: 3000, + }), + ); + + const checkStatus = async () => { + let statusResponse; + do { + const submissionResponse = await CoreModules.axios.post( + `${url}&background_task_id=${response.data.task_id}`, + ); + statusResponse = submissionResponse.data; + if (statusResponse.status === 'PENDING') { + await new Promise((resolve) => setTimeout(resolve, 2000)); + } + } while (statusResponse.status === 'PENDING'); + return statusResponse; + }; + const finalStatus = await checkStatus(); + if (finalStatus.status === 'FAILED') { + dispatch( + CommonActions.SetSnackBar({ + open: true, + message: finalStatus.message, + variant: 'error', + duration: 3000, + }), + ); + return; + } + var a = document.createElement('a'); + a.href = finalStatus; + a.download = 'Submissions'; + a.click(); + dispatch( + CoreModules.TaskActions.GetDownloadProjectSubmissionLoading({ + type: 'json', + loading: false, + }), + ); + } catch (error) { + dispatch( + CommonActions.SetSnackBar({ + open: true, + message: 'Something went wrong.', + variant: 'error', + duration: 3000, + }), + ); + dispatch( + CoreModules.TaskActions.GetDownloadProjectSubmissionLoading({ + type: 'json', + loading: false, + }), + ); + } finally { + dispatch( + CoreModules.TaskActions.GetDownloadProjectSubmissionLoading({ + type: 'json', + loading: false, + }), + ); + } + }; + await getProjectSubmission(url); + }; +}; + export const fetchConvertToOsmDetails: Function = (url: string) => { return async (dispatch) => { dispatch(CoreModules.TaskActions.FetchConvertToOsmLoading(true)); diff --git a/src/frontend/src/views/ProjectInfo.tsx b/src/frontend/src/views/ProjectInfo.tsx index d1f1c51ff3..d87ea19d51 100644 --- a/src/frontend/src/views/ProjectInfo.tsx +++ b/src/frontend/src/views/ProjectInfo.tsx @@ -5,7 +5,13 @@ import ProjectInfomap from '../components/ProjectInfo/ProjectInfomap'; import environment from '../environment'; import { ProjectActions } from '../store/slices/ProjectSlice'; -import { ConvertXMLToJOSM, fetchConvertToOsmDetails, fetchInfoTask, getDownloadProjectSubmission } from '../api/task'; +import { + ConvertXMLToJOSM, + fetchConvertToOsmDetails, + fetchInfoTask, + getDownloadProjectSubmission, + getDownloadProjectSubmissionJson, +} from '../api/task'; import AssetModules from '../shared/AssetModules'; import { ProjectById } from '../api/Project'; import ProjectInfoCountCard from '../components/ProjectInfo/ProjectInfoCountCard'; @@ -49,8 +55,8 @@ const ProjectInfo = () => { ); } else if (downloadType === 'json') { dispatch( - getDownloadProjectSubmission( - `${import.meta.env.VITE_API_URL}/submission/download?project_id=${decodedId}&export_json=true`, + getDownloadProjectSubmissionJson( + `${import.meta.env.VITE_API_URL}/submission/download-submission?project_id=${decodedId}`, ), ); }