Skip to content

Commit

Permalink
Improved upload feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Dec 9, 2023
1 parent d55c04b commit f4a32b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
26 changes: 19 additions & 7 deletions frontend/src/CollectionView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const CollectionView = () => {
const [disableMintAll, setDisableMintAll] = useState(null);
const [budget, setBudget] = useState(0);
const [modalOpen, setModalOpen] = useState(false);
const [uploadResults, setUploadResults] = useState(null);
const [uploadWarnings, setUploadWarnings] = useState(null);

useEffect(() => {
const fetchCollection = async () => {
Expand Down Expand Up @@ -66,33 +68,39 @@ const CollectionView = () => {
try {
const response = await axios.post(`/api/v1/collections/${collection.xid}/upload`, formData);
const data = response.data;
let uploadResults = ''
let uploadWarnings = '';

if (data.filesUploaded) {
const mb = data.bytesUploaded / 1000000;
alert(`You were debited ${data.creditsDebited} credits to upload ${data.filesUploaded} files (${mb.toFixed(2)} MB)`);
uploadResults = `You were debited ${data.creditsDebited} credits to upload ${data.filesUploaded} files (${mb.toFixed(2)} MB)`;
setRefreshKey((prevKey) => prevKey + 1);
}

if (data.filesSkipped) {
if (data.filesSkipped === 1) {
alert(`1 file was skipped due to insufficient credits.`);
uploadWarnings = `1 file was skipped due to insufficient credits. `;
}
else {
alert(`${data.filesSkipped} files were skipped due to insufficient credits.`);
uploadWarnings = `${data.filesSkipped} files were skipped due to insufficient credits. `;
}
}

if (data.filesErrored) {
if (data.filesErrored === 1) {
alert(`1 file was skipped due to error reading image.`);
uploadWarnings += `1 file was skipped due to error reading image.`;
}
else {
alert(`${data.filesErrored} files were skipped due to error reading image.`);
uploadWarnings += `${data.filesErrored} files were skipped due to error reading image.`;
}
}

setUploadResults(uploadResults);
setUploadWarnings(uploadWarnings);
} catch (error) {
console.error('Error uploading images:', error);
alert('Error uploading images');
setUploadResults('Error uploading images');
setUploadWarnings('');
}
};

Expand Down Expand Up @@ -201,7 +209,11 @@ const CollectionView = () => {
{
isDragActive ?
<p>Drop the images here ...</p> :
<p>Copy/Paste or Drag 'n' Drop some images here, or click to select files</p>
<div>
<p>Copy/Paste or Drag 'n' Drop some images here, or click to select files</p>
<p>{uploadResults}</p>
<p>{uploadWarnings}</p>
</div>
}
</div>
);
Expand Down
23 changes: 11 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,20 +1203,19 @@ app.post('/api/v1/collections/:xid/upload', ensureAuthenticated, upload.array('i
const collectionId = req.params.xid;
const upload = await xidb.createAssets(req.user.xid, req.files, collectionId);

if (!upload.filesUploaded) {
return res.status(500).json({ message: 'Error processing images' });
}
if (upload.filesUploaded) {
const txn = {
'type': 'upload',
'xid': collectionId,
'files': upload.filesUploaded,
'bytes': upload.bytesUploaded,
'credits': upload.creditsDebited,
};

const txn = {
'type': 'upload',
'xid': collectionId,
'files': upload.filesUploaded,
'bytes': upload.bytesUploaded,
'credits': upload.creditsDebited,
};
agent.saveTxnLog(req.user.xid, txn);
await archiver.commitChanges({ type: 'upload', agent: req.user.xid, asset: collectionId, files: upload.filesUploaded, bytes: upload.bytesUploaded });
}

agent.saveTxnLog(req.user.xid, txn);
await archiver.commitChanges({ type: 'upload', agent: req.user.xid, asset: collectionId, files: upload.filesUploaded, bytes: upload.bytesUploaded });
res.status(200).json(upload);
} catch (error) {
console.error('Error processing files:', error);
Expand Down

0 comments on commit f4a32b2

Please sign in to comment.