2022-08-09 23:24:44 +08:00
|
|
|
import Presentations from '/imports/api/presentations';
|
2022-08-10 22:57:53 +08:00
|
|
|
import React, { useState } from 'react';
|
2022-08-09 23:24:44 +08:00
|
|
|
import { useTracker } from 'meteor/react-meteor-data';
|
|
|
|
import Icon from '/imports/ui/components/common/icon/component';
|
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
|
|
|
import Styled from '/imports/ui/components/presentation/presentation-uploader/styles';
|
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
import { defineMessages } from 'react-intl';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import { UploadingPresentations } from '/imports/api/presentations';
|
2022-09-17 04:36:23 +08:00
|
|
|
|
|
|
|
const TIMEOUT_CLOSE_TOAST = 1; //second
|
2022-08-09 23:24:44 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
|
|
|
|
item: {
|
|
|
|
id: 'app.presentationUploder.item',
|
|
|
|
description: 'single item label',
|
|
|
|
},
|
|
|
|
itemPlural: {
|
|
|
|
id: 'app.presentationUploder.itemPlural',
|
|
|
|
description: 'plural item label',
|
|
|
|
},
|
|
|
|
uploading: {
|
|
|
|
id: 'app.presentationUploder.uploading',
|
|
|
|
description: 'uploading label for toast notification',
|
|
|
|
},
|
|
|
|
uploadStatus: {
|
|
|
|
id: 'app.presentationUploder.uploadStatus',
|
|
|
|
description: 'upload status for toast notification',
|
|
|
|
},
|
|
|
|
completed: {
|
|
|
|
id: 'app.presentationUploder.completed',
|
|
|
|
description: 'uploads complete label for toast notification',
|
|
|
|
},
|
|
|
|
GENERATING_THUMBNAIL: {
|
|
|
|
id: 'app.presentationUploder.conversion.generatingThumbnail',
|
|
|
|
description: 'indicatess that it is generating thumbnails',
|
|
|
|
},
|
|
|
|
GENERATING_SVGIMAGES: {
|
|
|
|
id: 'app.presentationUploder.conversion.generatingSvg',
|
|
|
|
description: 'warns that it is generating svg images',
|
|
|
|
},
|
|
|
|
GENERATED_SLIDE: {
|
|
|
|
id: 'app.presentationUploder.conversion.generatedSlides',
|
|
|
|
description: 'warns that were slides generated',
|
|
|
|
},
|
2022-09-16 23:52:41 +08:00
|
|
|
413: {
|
|
|
|
id: 'app.presentationUploder.upload.413',
|
|
|
|
description: 'error that file exceed the size limit',
|
|
|
|
},
|
2022-10-06 22:24:50 +08:00
|
|
|
CONVERSION_TIMEOUT: {
|
|
|
|
id:'app.presentationUploder.conversion.conversionTimeout',
|
|
|
|
description: 'warns the user that the presentation timed out in the back-end in specific page of the document',
|
|
|
|
},
|
2022-09-16 23:52:41 +08:00
|
|
|
FILE_TOO_LARGE: {
|
|
|
|
id: 'app.presentationUploder.upload.413',
|
|
|
|
description: 'error that file exceed the size limit',
|
|
|
|
},
|
2022-08-09 23:24:44 +08:00
|
|
|
PAGE_COUNT_EXCEEDED: {
|
|
|
|
id: 'app.presentationUploder.conversion.pageCountExceeded',
|
|
|
|
description: 'warns the user that the conversion failed because of the page count',
|
|
|
|
},
|
|
|
|
PDF_HAS_BIG_PAGE: {
|
|
|
|
id: 'app.presentationUploder.conversion.pdfHasBigPage',
|
|
|
|
description: 'warns the user that the conversion failed because of the pdf page siz that exceeds the allowed limit',
|
|
|
|
},
|
|
|
|
OFFICE_DOC_CONVERSION_INVALID: {
|
|
|
|
id: 'app.presentationUploder.conversion.officeDocConversionInvalid',
|
|
|
|
description: '',
|
|
|
|
},
|
|
|
|
OFFICE_DOC_CONVERSION_FAILED: {
|
|
|
|
id: 'app.presentationUploder.conversion.officeDocConversionFailed',
|
|
|
|
description: 'warns the user that the conversion failed because of wrong office file',
|
|
|
|
},
|
|
|
|
UNSUPPORTED_DOCUMENT: {
|
|
|
|
id: 'app.presentationUploder.conversion.unsupportedDocument',
|
|
|
|
description: 'warns the user that the file extension is not supported',
|
|
|
|
},
|
|
|
|
fileToUpload: {
|
|
|
|
id: 'app.presentationUploder.fileToUpload',
|
|
|
|
description: 'message used in the file selected for upload',
|
|
|
|
},
|
|
|
|
uploadProcess: {
|
|
|
|
id: 'app.presentationUploder.upload.progress',
|
|
|
|
description: 'message that indicates the percentage of the upload',
|
|
|
|
},
|
|
|
|
badConnectionError: {
|
|
|
|
id: 'app.presentationUploder.connectionClosedError',
|
|
|
|
description: 'message indicating that the connection was closed',
|
|
|
|
},
|
|
|
|
conversionProcessingSlides: {
|
|
|
|
id: 'app.presentationUploder.conversion.conversionProcessingSlides',
|
|
|
|
description: 'indicates how many slides were converted',
|
|
|
|
},
|
|
|
|
genericError: {
|
|
|
|
id: 'app.presentationUploder.genericError',
|
|
|
|
description: 'generic error while uploading/converting',
|
|
|
|
},
|
|
|
|
genericConversionStatus: {
|
|
|
|
id: 'app.presentationUploder.conversion.genericConversionStatus',
|
|
|
|
description: 'indicates that file is being converted',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function renderPresentationItemStatus(item, intl) {
|
2022-09-16 23:52:41 +08:00
|
|
|
if ((("progress" in item) && item.progress === 0) || (("upload" in item) && item.upload.progress === 0 && !item.upload.error)) {
|
2022-08-09 23:24:44 +08:00
|
|
|
return intl.formatMessage(intlMessages.fileToUpload);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (("progress" in item) && item.progress < 100 && !("conversion" in item)) {
|
|
|
|
return intl.formatMessage(intlMessages.uploadProcess, {
|
|
|
|
0: Math.floor(item.progress).toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const constraint = {};
|
|
|
|
|
|
|
|
if (("upload" in item) && (item.upload.done && item.upload.error)) {
|
2022-09-16 23:52:41 +08:00
|
|
|
if (item.conversion.status === 'FILE_TOO_LARGE' || item.upload.status !== 413) {
|
2022-08-09 23:24:44 +08:00
|
|
|
constraint['0'] = ((item.conversion.maxFileSize) / 1000 / 1000).toFixed(2);
|
2022-09-16 23:52:41 +08:00
|
|
|
} else {
|
|
|
|
if (item.progress < 100) {
|
|
|
|
const errorMessage = intlMessages.badConnectionError;
|
|
|
|
return intl.formatMessage(errorMessage);
|
|
|
|
}
|
2022-08-09 23:24:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const errorMessage = intlMessages[item.upload.status] || intlMessages.genericError;
|
|
|
|
return intl.formatMessage(errorMessage, constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (("conversion" in item) && (!item.conversion.done && item.conversion.error)) {
|
|
|
|
const errorMessage = intlMessages[item.conversion.status] || intlMessages.genericConversionStatus;
|
|
|
|
|
|
|
|
switch (item.conversion.status) {
|
2022-10-06 22:24:50 +08:00
|
|
|
case 'CONVERSION_TIMEOUT':
|
|
|
|
constraint['0'] = item.conversion.numberPageError;
|
|
|
|
constraint['1'] = item.conversion.maxNumberOfAttempts;
|
|
|
|
break;
|
2022-09-16 23:52:41 +08:00
|
|
|
case 'FILE_TOO_LARGE':
|
|
|
|
constraint['0'] = ((item.conversion.maxFileSize) / 1000 / 1000).toFixed(2);
|
|
|
|
break;
|
2022-08-09 23:24:44 +08:00
|
|
|
case 'PAGE_COUNT_EXCEEDED':
|
|
|
|
constraint['0'] = item.conversion.maxNumberPages;
|
|
|
|
break;
|
|
|
|
case 'PDF_HAS_BIG_PAGE':
|
|
|
|
constraint['0'] = (item.conversion.bigPageSize / 1000 / 1000).toFixed(2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return intl.formatMessage(errorMessage, constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((("conversion" in item) && (!item.conversion.done && !item.conversion.error)) || (("progress" in item) && item.progress == 100)) {
|
|
|
|
let conversionStatusMessage
|
|
|
|
if ("conversion" in item) {
|
|
|
|
if (item.conversion.pagesCompleted < item.conversion.numPages) {
|
|
|
|
return intl.formatMessage(intlMessages.conversionProcessingSlides, {
|
|
|
|
0: item.conversion.pagesCompleted,
|
|
|
|
1: item.conversion.numPages,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
conversionStatusMessage = intlMessages[item.conversion.status]
|
|
|
|
|| intlMessages.genericConversionStatus;
|
|
|
|
} else {
|
|
|
|
conversionStatusMessage = intlMessages.genericConversionStatus;
|
|
|
|
}
|
|
|
|
return intl.formatMessage(conversionStatusMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderToastItem(item, intl) {
|
|
|
|
|
|
|
|
const isUploading = ("progress" in item) && item.progress <= 100;
|
|
|
|
const isConverting = ("conversion" in item) && !item.conversion.done;
|
|
|
|
const hasError = ((("conversion" in item) && item.conversion.error) || (("upload" in item) && item.upload.error));
|
|
|
|
const isProcessing = (isUploading || isConverting) && !hasError;
|
|
|
|
|
|
|
|
let icon = isProcessing ? 'blank' : 'check';
|
|
|
|
if (hasError) icon = 'circle_close';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.UploadRow
|
2022-11-11 21:16:21 +08:00
|
|
|
key={item.id || item.temporaryPresentationId }
|
2022-08-09 23:24:44 +08:00
|
|
|
onClick={() => {
|
|
|
|
if (hasError || isProcessing) Session.set('showUploadPresentationView', true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Styled.FileLine>
|
|
|
|
<span>
|
|
|
|
<Icon iconName="file" />
|
|
|
|
</span>
|
|
|
|
<Styled.ToastFileName>
|
|
|
|
<span>{item.filename || item.name}</span>
|
|
|
|
</Styled.ToastFileName>
|
|
|
|
<Styled.StatusIcon>
|
|
|
|
<Styled.ToastItemIcon
|
|
|
|
done={!isProcessing && !hasError}
|
|
|
|
error={hasError}
|
|
|
|
loading={ isProcessing }
|
|
|
|
iconName={icon}
|
|
|
|
/>
|
|
|
|
</Styled.StatusIcon>
|
|
|
|
</Styled.FileLine>
|
|
|
|
<Styled.StatusInfo>
|
|
|
|
<Styled.StatusInfoSpan data-test="presentationStatusInfo" styles={hasError ? 'error' : 'info'}>
|
|
|
|
{renderPresentationItemStatus(item, intl)}
|
|
|
|
</Styled.StatusInfoSpan>
|
|
|
|
</Styled.StatusInfo>
|
|
|
|
</Styled.UploadRow>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderToastList = (presentations, intl) => {
|
|
|
|
|
|
|
|
let converted = 0;
|
|
|
|
|
|
|
|
let presentationsSorted = presentations
|
|
|
|
.sort((a, b) => a.uploadTimestamp - b.uploadTimestamp)
|
|
|
|
.sort((a, b) => {
|
|
|
|
const presADone = a.conversion ? a.conversion.done : false;
|
|
|
|
const presBDone = b.conversion ? b.conversion.done : false;
|
|
|
|
|
|
|
|
return presADone - presBDone
|
|
|
|
});
|
|
|
|
|
|
|
|
presentationsSorted
|
|
|
|
.forEach((p) => {
|
|
|
|
const presDone = p.conversion ? p.conversion.done : false;
|
|
|
|
if (presDone) converted += 1;
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
|
|
|
|
let toastHeading = '';
|
|
|
|
const itemLabel = presentationsSorted.length > 1
|
|
|
|
? intl.formatMessage(intlMessages.itemPlural)
|
|
|
|
: intl.formatMessage(intlMessages.item);
|
|
|
|
|
|
|
|
if (converted === 0) {
|
|
|
|
toastHeading = intl.formatMessage(intlMessages.uploading, {
|
|
|
|
0: presentationsSorted.length,
|
|
|
|
1: itemLabel,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (converted > 0 && converted !== presentationsSorted.length) {
|
|
|
|
toastHeading = intl.formatMessage(intlMessages.uploadStatus, {
|
|
|
|
0: converted,
|
|
|
|
1: presentationsSorted.length,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (converted === presentationsSorted.length) {
|
|
|
|
toastHeading = intl.formatMessage(intlMessages.completed, {
|
|
|
|
0: converted,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-09-27 21:05:27 +08:00
|
|
|
<Styled.ToastWrapper data-test="presentationUploadProgressToast" >
|
2022-08-09 23:24:44 +08:00
|
|
|
<Styled.UploadToastHeader>
|
|
|
|
<Styled.UploadIcon iconName="upload" />
|
|
|
|
<Styled.UploadToastTitle>{toastHeading}</Styled.UploadToastTitle>
|
|
|
|
</Styled.UploadToastHeader>
|
|
|
|
<Styled.InnerToast>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
{presentationsSorted.map((item) => renderToastItem(item, intl))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Styled.InnerToast>
|
|
|
|
</Styled.ToastWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleDismissToast(toastId) {
|
|
|
|
return toast.dismiss(toastId);
|
|
|
|
}
|
|
|
|
|
2022-08-10 22:57:53 +08:00
|
|
|
const alreadyRenderedPresList = []
|
2022-09-23 23:24:39 +08:00
|
|
|
export const PresentationUploaderToast = ({ intl }) => {
|
2022-08-09 23:24:44 +08:00
|
|
|
|
|
|
|
useTracker(() => {
|
2022-09-16 23:52:41 +08:00
|
|
|
|
2022-08-09 23:24:44 +08:00
|
|
|
const presentationsRenderedFalseAndConversionFalse = Presentations.find({ $or: [{renderedInToast: false}, {"conversion.done": false}] }).fetch();
|
|
|
|
const convertingPresentations = presentationsRenderedFalseAndConversionFalse.filter(p => !p.renderedInToast )
|
|
|
|
let tmpIdconvertingPresentations = presentationsRenderedFalseAndConversionFalse.filter(p => !p.conversion.done)
|
2022-10-05 03:25:54 +08:00
|
|
|
.map(p => { return {temporaryPresentationId: p.temporaryPresentationId, id: p.id}; })
|
|
|
|
UploadingPresentations.find({}).fetch().filter(p => tmpIdconvertingPresentations.findIndex(pres => pres.temporaryPresentationId === p.temporaryPresentationId || pres.id === p.id) !== -1)
|
2022-09-03 02:24:29 +08:00
|
|
|
.map(p => {
|
2022-10-05 03:25:54 +08:00
|
|
|
return UploadingPresentations.remove({$or: [{temporaryPresentationId: p.temporaryPresentationId }, {id: p.id}]})});
|
2022-08-09 23:24:44 +08:00
|
|
|
const uploadingPresentations = UploadingPresentations.find().fetch();
|
|
|
|
let presentationsToConvert = convertingPresentations.concat(uploadingPresentations);
|
|
|
|
|
2022-09-17 04:36:23 +08:00
|
|
|
// Updating or populating the "state" presentation list
|
|
|
|
presentationsToConvert.map(p => {
|
|
|
|
return {
|
2022-09-22 04:41:31 +08:00
|
|
|
filename: p.name || p.filename,
|
2022-09-17 04:36:23 +08:00
|
|
|
temporaryPresentationId: p.temporaryPresentationId,
|
|
|
|
presentationId: p.id,
|
|
|
|
hasError: p.conversion?.error || p.upload?.error,
|
2022-09-22 04:41:31 +08:00
|
|
|
lastModifiedUploader: p.lastModifiedUploader,
|
2022-09-17 04:36:23 +08:00
|
|
|
}
|
2022-09-22 04:41:31 +08:00
|
|
|
}).forEach(p => {
|
|
|
|
const docIndexAlreadyInList = alreadyRenderedPresList.findIndex(pres => {
|
|
|
|
return (pres.temporaryPresentationId === p.temporaryPresentationId || pres.presentationId === p.presentationId
|
|
|
|
|| (pres.lastModifiedUploader !== undefined && !pres.lastModifiedUploader && pres.filename === p.filename))});
|
2022-09-17 04:36:23 +08:00
|
|
|
if (docIndexAlreadyInList === -1) {
|
2022-08-10 22:57:53 +08:00
|
|
|
alreadyRenderedPresList.push({
|
2022-09-22 04:41:31 +08:00
|
|
|
filename: p.filename,
|
|
|
|
temporaryPresentationId: p.temporaryPresentationId,
|
|
|
|
presentationId: p.presentationId,
|
2022-08-10 22:57:53 +08:00
|
|
|
rendered: false,
|
2022-09-22 04:41:31 +08:00
|
|
|
lastModifiedUploader: p.lastModifiedUploader,
|
|
|
|
hasError: p.hasError,
|
2022-08-10 22:57:53 +08:00
|
|
|
});
|
2022-09-17 04:36:23 +08:00
|
|
|
} else {
|
2022-09-22 04:41:31 +08:00
|
|
|
alreadyRenderedPresList[docIndexAlreadyInList].temporaryPresentationId = p.temporaryPresentationId;
|
|
|
|
alreadyRenderedPresList[docIndexAlreadyInList].presentationId = p.presentationId;
|
|
|
|
alreadyRenderedPresList[docIndexAlreadyInList].lastModifiedUploader = p.lastModifiedUploader;
|
|
|
|
alreadyRenderedPresList[docIndexAlreadyInList].hasError = p.hasError;
|
2022-08-10 22:57:53 +08:00
|
|
|
}
|
|
|
|
})
|
2022-08-09 23:24:44 +08:00
|
|
|
let activeToast = Session.get("presentationUploaderToastId");
|
|
|
|
const showToast = presentationsToConvert.length > 0;
|
|
|
|
if (showToast && !activeToast) {
|
|
|
|
activeToast = toast.info(() => renderToastList(presentationsToConvert, intl), {
|
|
|
|
hideProgressBar: true,
|
|
|
|
autoClose: false,
|
|
|
|
newestOnTop: true,
|
|
|
|
closeOnClick: true,
|
2022-09-23 23:24:39 +08:00
|
|
|
className: "presentationUploaderToast toastClass",
|
2022-08-09 23:24:44 +08:00
|
|
|
onClose: () => {
|
|
|
|
presentationsToConvert = [];
|
2022-09-17 04:36:23 +08:00
|
|
|
if (alreadyRenderedPresList.every((pres) => pres.rendered)) {
|
2022-09-23 23:24:39 +08:00
|
|
|
makeCall('setPresentationRenderedInToast').then(() => {
|
|
|
|
Session.set("presentationUploaderToastId", null);
|
|
|
|
});
|
2022-09-17 04:36:23 +08:00
|
|
|
alreadyRenderedPresList.length = 0;
|
|
|
|
}
|
2022-08-09 23:24:44 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
Session.set("presentationUploaderToastId", activeToast);
|
|
|
|
} else if (!showToast && activeToast) {
|
|
|
|
handleDismissToast(activeToast);
|
|
|
|
Session.set("presentationUploaderToastId", null);
|
|
|
|
} else {
|
|
|
|
toast.update(activeToast, {
|
|
|
|
render: renderToastList(presentationsToConvert, intl),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-03 02:24:29 +08:00
|
|
|
let temporaryPresentationIdListToSetAsRendered = presentationsToConvert.filter(p =>
|
2022-08-09 23:24:44 +08:00
|
|
|
("conversion" in p && (p.conversion.done || p.conversion.error)))
|
|
|
|
|
2022-09-03 02:24:29 +08:00
|
|
|
temporaryPresentationIdListToSetAsRendered = temporaryPresentationIdListToSetAsRendered.map(p => {
|
2022-09-16 23:52:41 +08:00
|
|
|
index = alreadyRenderedPresList.findIndex(pres => (pres.temporaryPresentationId === p.temporaryPresentationId || pres.presentationId === p.id));
|
2022-08-10 22:57:53 +08:00
|
|
|
if (index !== -1) {
|
|
|
|
alreadyRenderedPresList[index].rendered = true;
|
|
|
|
}
|
2022-09-03 02:24:29 +08:00
|
|
|
return p.temporaryPresentationId
|
2022-08-10 22:57:53 +08:00
|
|
|
});
|
2022-08-09 23:24:44 +08:00
|
|
|
|
2022-09-17 04:36:23 +08:00
|
|
|
if (alreadyRenderedPresList.every((pres) => pres.rendered && !pres.hasError)) {
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
makeCall('setPresentationRenderedInToast');
|
|
|
|
alreadyRenderedPresList.length = 0;
|
|
|
|
}, TIMEOUT_CLOSE_TOAST * 1000);
|
2022-08-10 22:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}, [])
|
2022-08-09 23:24:44 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
handleDismissToast,
|
|
|
|
renderPresentationItemStatus,
|
2022-09-03 02:24:29 +08:00
|
|
|
}
|