Skip to content

Commit

Permalink
Dataset uploads are not updated appropriately (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidQuartz authored Apr 13, 2022
1 parent 5baff2d commit 83224cd
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 22 deletions.
23 changes: 2 additions & 21 deletions geonode_mapstore_client/client/js/routes/UploadDataset.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = [
{
Expand Down Expand Up @@ -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 (
<UploadList
onSuccess={(successfulUploads) => setPendingUploads((prevUploads) => parseUploadResponse([...successfulUploads, ...prevUploads]))}
>
<ProcessingUploadList
uploads={pendingUploads}
onChange={(uploads) => setPendingUploads((prevUploads) => processUploadResponse([...uploads, ...prevUploads]))}
onChange={(uploads) => setPendingUploads((prevUploads) => processUploadResponse([...prevUploads, ...uploads]))}
onDelete={(uploads) => setPendingUploads(parseUploadResponse(uploads))}
refreshTime={refreshTime}
/>
Expand Down
20 changes: 20 additions & 0 deletions geonode_mapstore_client/client/js/utils/ResourceUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
toMapStoreMapConfig,
parseStyleName,
canCopyResource,
excludeDeletedResources
excludeDeletedResources,
processUploadResponse,
parseUploadResponse
} from '../ResourceUtils';

describe('Test Resource Utils', () => {
Expand Down Expand Up @@ -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
}
]);
});
});

0 comments on commit 83224cd

Please sign in to comment.