Skip to content

Commit

Permalink
progress bar setup, api utils, and cancel button logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmage20 committed Oct 24, 2024
1 parent 9869398 commit 2933e7f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/reader/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def documents
document.to_hash.tap do |object|
object[:opened_by_current_user] = read_documents_hash[document.id] || false
object[:tags] = tags_by_doc_id[document.id].to_a
object[:file_size] = document.file_size
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def serve
default_path
end

def file_size
File.size(default_path) || 0
end

def file_name
vbms_document_id.to_s
end
Expand Down
1 change: 1 addition & 0 deletions client/app/readerprototype/DocumentViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const DocumentViewer = (props) => {
setIsDocumentLoadError={setIsDocumentLoadError}
setNumPages={setNumPages}
zoomLevel={props.zoomLevel}
onrequestCancel={() => props.history.push(props.documentPathBase)}
/>
</div>
<ReaderFooter
Expand Down
28 changes: 28 additions & 0 deletions client/app/readerprototype/components/PdfDocument.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const PdfDocument = ({
rotateDeg,
setIsDocumentLoadError,
setNumPages,
onrequestCancel,
zoomLevel }) => {
const [pdfDoc, setPdfDoc] = useState(null);
const [pdfPages, setPdfPages] = useState([]);
Expand All @@ -28,6 +29,7 @@ const PdfDocument = ({
const [allPagesRendered, setAllPagesRendered] = useState(false);
const [metricsLogged, setMetricsLogged] = useState(false);
const metricsLoggedRef = useRef(metricsLogged);
const requestRef = useRef(null);

const containerStyle = {
width: '100%',
Expand Down Expand Up @@ -97,6 +99,15 @@ const PdfDocument = ({
setMetricsLogged(true);
};

const handleCancelRequest = () => {
if (requestRef.current) {
// Abort the download
requestRef.current.abort();
// Redirect
onrequestCancel();
}
};

useEffect(() => {
const getDocData = async () => {
pdfMetrics.current.renderedPageCount = 0;
Expand All @@ -110,10 +121,25 @@ const PdfDocument = ({
withCredentials: true,
timeout: true,
responseType: 'arraybuffer',
onProgress: ({ loaded }) => {
let percentage = 0;

if (doc.file_size > 0) {
percentage = (loaded / doc.file_size) * 100;
}

console.log(`### Downloaded ${percentage.toFixed(0)}%\n`);

},
cancellableRequest: ({ request }) => {
requestRef.current = request;
}
};

pdfMetrics.current.getStartTime = new Date().getTime();
const byteArr = await ApiUtil.get(doc.content_url, requestOptions).then((response) => {
requestRef.current = null;

return response.body;
});

Expand Down Expand Up @@ -202,12 +228,14 @@ PdfDocument.propTypes = {
filename: PropTypes.string,
id: PropTypes.number,
type: PropTypes.string,
file_size: PropTypes.number,
}),
isDocumentLoadError: PropTypes.bool,
rotateDeg: PropTypes.string,
setIsDocumentLoadError: PropTypes.func,
setNumPages: PropTypes.func,
zoomLevel: PropTypes.number,
onrequestCancel: PropTypes.func,
};

export default PdfDocument;
18 changes: 17 additions & 1 deletion client/app/util/ApiUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,23 @@ const httpMethods = {
set(getHeadersObject(options.headers)).
query(options.query).
timeout(timeoutSettings).
on('error', (err) => errorHandling(url, err, 'GET', options));
on('error', (err) => errorHandling(url, err, 'GET', options)).
on('abort', () => console.log('## User Cancelled, should I log this?\n'));

if (typeof options.onProgress === 'function') {
promise.on('progress', (event) => {
const loaded = event.loaded;

options.onProgress({ loaded });
});
}

if (typeof options.cancellableRequest === 'function') {
const pendingRequest = promise;

options.cancellableRequest({ request: pendingRequest });

}

if (options.responseType) {
promise.responseType(options.responseType);
Expand Down

0 comments on commit 2933e7f

Please sign in to comment.