From 83224cd1125c916d8d4c2bca85332a56c2ea3f52 Mon Sep 17 00:00:00 2001 From: David Quartey <42542676+DavidQuartz@users.noreply.github.com> Date: Wed, 13 Apr 2022 13:42:55 +0000 Subject: [PATCH] Dataset uploads are not updated appropriately (#914) --- .../client/js/routes/UploadDataset.jsx | 23 +-- .../client/js/utils/ResourceUtils.js | 20 +++ .../js/utils/__tests__/ResourceUtils-test.js | 141 +++++++++++++++++- 3 files changed, 162 insertions(+), 22 deletions(-) diff --git a/geonode_mapstore_client/client/js/routes/UploadDataset.jsx b/geonode_mapstore_client/client/js/routes/UploadDataset.jsx index d0d250ecc2..a74206609c 100644 --- a/geonode_mapstore_client/client/js/routes/UploadDataset.jsx +++ b/geonode_mapstore_client/client/js/routes/UploadDataset.jsx @@ -8,8 +8,6 @@ import React, { useState, useEffect, useRef, useCallback } from 'react'; import PropTypes from 'prop-types'; -import uniqBy from 'lodash/uniqBy'; -import orderBy from 'lodash/orderBy'; import omit from 'lodash/omit'; import pick from 'lodash/pick'; import merge from 'lodash/merge'; @@ -26,6 +24,7 @@ import axios from '@mapstore/framework/libs/ajax'; import UploadListContainer from '@js/routes/upload/UploadListContainer'; import UploadContainer from '@js/routes/upload/UploadContainer'; import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils'; +import { parseUploadResponse, processUploadResponse } from '@js/utils/ResourceUtils'; const supportedDatasetTypes = [ { @@ -380,31 +379,13 @@ function UploadDataset({ const [pendingUploads, setPendingUploads] = useState([]); - - function parseUploadResponse(upload) { - return orderBy(uniqBy([...upload], 'id'), 'create_date', 'desc'); - } - - function processUploadResponse(response) { - const newResponse = response.reduce((acc, currentResponse) => { - const duplicate = acc.find((upload) => upload.id === currentResponse.id); - if (duplicate) { - const merger = merge(duplicate, currentResponse); - const newAcc = acc.filter((upload) => upload.id !== duplicate.id); - return [...newAcc, merger]; - } - return [...acc, currentResponse]; - }, []); - return parseUploadResponse(newResponse); - } - return ( setPendingUploads((prevUploads) => parseUploadResponse([...successfulUploads, ...prevUploads]))} > setPendingUploads((prevUploads) => processUploadResponse([...uploads, ...prevUploads]))} + onChange={(uploads) => setPendingUploads((prevUploads) => processUploadResponse([...prevUploads, ...uploads]))} onDelete={(uploads) => setPendingUploads(parseUploadResponse(uploads))} refreshTime={refreshTime} /> diff --git a/geonode_mapstore_client/client/js/utils/ResourceUtils.js b/geonode_mapstore_client/client/js/utils/ResourceUtils.js index dd4cd5abbc..4add9b8794 100644 --- a/geonode_mapstore_client/client/js/utils/ResourceUtils.js +++ b/geonode_mapstore_client/client/js/utils/ResourceUtils.js @@ -14,6 +14,7 @@ import { parseDevHostname } from '@js/utils/APIUtils'; import { ProcessTypes, ProcessStatus } from '@js/utils/ResourceServiceUtils'; import { bboxToPolygon } from '@js/utils/CoordinatesUtils'; import uniqBy from 'lodash/uniqBy'; +import orderBy from 'lodash/orderBy'; import isString from 'lodash/isString'; import isObject from 'lodash/isObject'; import { excludeGoogleBackground, extractTileMatrixFromSources } from '@mapstore/framework/utils/LayersUtils'; @@ -594,3 +595,22 @@ export const excludeDeletedResources = (suppliedResources) => { return !isDeleted && resource; }); }; + +export const parseUploadResponse = (upload) => { + return orderBy(uniqBy([...upload], 'id'), 'create_date', 'desc'); +}; + +export const processUploadResponse = (response) => { + const newResponse = response.reduce((acc, currentResponse) => { + const duplicate = acc.find((upload) => upload.id === currentResponse.id); + if (duplicate) { + const newAcc = acc.filter((upload) => upload.id !== duplicate.id); + return [currentResponse, ...newAcc]; + } + return [currentResponse, ...acc]; + }, []); + + const uploads = parseUploadResponse(newResponse); + + return uploads; +}; diff --git a/geonode_mapstore_client/client/js/utils/__tests__/ResourceUtils-test.js b/geonode_mapstore_client/client/js/utils/__tests__/ResourceUtils-test.js index 9930544e7e..ae3eaca582 100644 --- a/geonode_mapstore_client/client/js/utils/__tests__/ResourceUtils-test.js +++ b/geonode_mapstore_client/client/js/utils/__tests__/ResourceUtils-test.js @@ -19,7 +19,9 @@ import { toMapStoreMapConfig, parseStyleName, canCopyResource, - excludeDeletedResources + excludeDeletedResources, + processUploadResponse, + parseUploadResponse } from '../ResourceUtils'; describe('Test Resource Utils', () => { @@ -431,4 +433,141 @@ describe('Test Resource Utils', () => { expect(excludeDeletedResources(resources)).toEqual([{ name: 'test-2' }]); }); + + it('should test processUploadResponse', () => { + const prev = [{ + id: 1, + name: 'test1', + create_date: '2022-04-13T11:24:55.444578Z', + state: 'PENDING', + progress: 0, + complete: false + }, + { + id: 2, + name: 'test2', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'PENDING', + progress: 0, + complete: false + }, + { + id: 3, + name: 'test3', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'PENDING', + progress: 20, + complete: false + }]; + const current = [{ + id: 1, + name: 'test1', + create_date: '2022-04-13T11:24:55.444578Z', + state: 'RUNNING', + progress: 100, + complete: true + }, + { + id: 2, + name: 'test2', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'PENDING', + progress: 40, + complete: false, + resume_url: 'test/upload/delete/439' + }, + { + id: 3, + name: 'test3', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + }, + { + id: 4, + name: 'test4', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + }]; + + expect(processUploadResponse([...prev, ...current])).toEqual([ + { + id: 1, + name: 'test1', + create_date: '2022-04-13T11:24:55.444578Z', + state: 'RUNNING', + progress: 100, + complete: true + }, + { + id: 4, + name: 'test4', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + }, + { + id: 3, + name: 'test3', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + }, + { + id: 2, + name: 'test2', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'PENDING', + progress: 40, + complete: false, + resume_url: 'test/upload/delete/439' + } + ]); + }); + + it('should test parseUploadResponse', () => { + const uploads = [ + { + id: 3, + name: 'test3', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + }, + { + id: 2, + name: 'test2', + create_date: '2022-04-13T12:24:54.042291Z', + state: 'PENDING', + progress: 40, + complete: false, + resume_url: 'test/upload/delete/439' + }]; + + expect(parseUploadResponse(uploads)).toEqual([ + { + id: 2, + name: 'test2', + create_date: '2022-04-13T12:24:54.042291Z', + state: 'PENDING', + progress: 40, + complete: false, + resume_url: 'test/upload/delete/439' + }, + { + id: 3, + name: 'test3', + create_date: '2022-04-13T11:24:54.042291Z', + state: 'COMPLETE', + progress: 100, + complete: true + } + ]); + }); });