Skip to content

Commit

Permalink
Adopt Upload API error codes (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidQuartz authored Mar 21, 2022
1 parent 140c608 commit abb3937
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
26 changes: 21 additions & 5 deletions geonode_mapstore_client/client/js/routes/UploadDataset.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ function UploadList({
function ProcessingUploadList({
uploads: pendingUploads,
onChange,
refreshTime = 3000
refreshTime = 3000,
onDelete
}) {

const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -342,7 +343,7 @@ function ProcessingUploadList({
.finally(() => {
if (isMounted.current) {
setDeletedIds((ids) => [...ids, id]);
onChange(pendingUploads.filter(upload => upload.id !== id));
onDelete(pendingUploads.filter(upload => upload.id !== id));
}
});
}
Expand Down Expand Up @@ -379,8 +380,22 @@ function UploadDataset({

const [pendingUploads, setPendingUploads] = useState([]);

function parseUploadResponse(response) {
return orderBy(uniqBy([...response], 'id'), 'create_date', 'desc');

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 (
Expand All @@ -389,7 +404,8 @@ function UploadDataset({
>
<ProcessingUploadList
uploads={pendingUploads}
onChange={(uploads) => setPendingUploads(parseUploadResponse(uploads))}
onChange={(uploads) => setPendingUploads((prevUploads) => processUploadResponse([...uploads, ...prevUploads]))}
onDelete={(uploads) => setPendingUploads(parseUploadResponse(uploads))}
refreshTime={refreshTime}
/>
</UploadList>
Expand Down
22 changes: 2 additions & 20 deletions geonode_mapstore_client/client/js/routes/upload/UploadCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import tooltip from '@mapstore/framework/components/misc/enhancers/tooltip';
import Message from '@mapstore/framework/components/I18N/Message';
import moment from 'moment';
import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils';
import { getUploadErrorMessageFromCode } from '@js/utils/ErrorUtils';

function ErrorMessage(props) {
return (
Expand Down Expand Up @@ -42,25 +43,6 @@ function UploadCard({
const maxAllowedBytes = type !== 'document' ? datasetMaxUploadSize : documentMaxUploadSize;
const maxAllowedSize = Math.floor(maxAllowedBytes / (1024 * 1024));

const exceedingSizeError = (err) => {
let message = err?.message?.match('File size') || err?.errors?.[0]?.match('smaller files') || '';
let fileExceeds = false;
if (message) {
fileExceeds = true;
}
return fileExceeds;
};

const errorToolTip = ({ code }) => {
switch (code) {
case "upload_parallelism_limit_exceeded": {
return "gnviewer.parallelLimitError";
}
default:
return "gnviewer.invalidUploadMessageErrorTooltip";
}
};

return (
<div className="gn-upload-card">
<div className="gn-upload-card-header">
Expand Down Expand Up @@ -107,7 +89,7 @@ function UploadCard({
</Button>
: null}
{state === 'INVALID'
? exceedingSizeError(error) ? <ErrorMessageWithTooltip tooltip={<Message msgId="gnviewer.fileExceeds" msgParams={{limit: maxAllowedSize }} />} /> : <ErrorMessageWithTooltip tooltipId={<Message msgId={errorToolTip(error)} msgParams={{ limit: maxParallelUploads }} />} />
? <ErrorMessageWithTooltip tooltipId={<Message msgId={`gnviewer.${getUploadErrorMessageFromCode(error?.code)}`} msgParams={{ limit: getUploadErrorMessageFromCode(error) === 'fileExceeds' ? maxAllowedSize : maxParallelUploads }} />} />
: null}
</div>
</div>
Expand Down
21 changes: 21 additions & 0 deletions geonode_mapstore_client/client/js/utils/ErrorUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

export const getUploadErrorMessageFromCode = (code) => {
switch (code) {
case 'upload_parallelism_limit_exceeded': {
return 'parallelLimitError';
}
case 'total_upload_size_exceeded': {
return 'fileExceeds';
}
case 'upload_exception':
default:
return 'invalidUploadMessageErrorTooltip';
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import expect from 'expect';
import { getUploadErrorMessageFromCode } from '../ErrorUtils';

describe('Test error utilities', () => {
it('should test getUploadErrorMessageFromCode', () => {
expect(getUploadErrorMessageFromCode('upload_parallelism_limit_exceeded')).toEqual('parallelLimitError');
expect(getUploadErrorMessageFromCode('total_upload_size_exceeded')).toEqual('fileExceeds');
expect(getUploadErrorMessageFromCode('upload_exception')).toEqual('invalidUploadMessageErrorTooltip');
expect(getUploadErrorMessageFromCode()).toEqual('invalidUploadMessageErrorTooltip');
});
});

0 comments on commit abb3937

Please sign in to comment.