mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
fix pdf preview with background worker (#9591)
This commit is contained in:
parent
fc42b6d7a1
commit
cee679d6e3
@ -1,6 +1,7 @@
|
||||
import { Trans } from '@lingui/react/macro';
|
||||
import { forwardRef, useImperativeHandle, useState } from 'react';
|
||||
|
||||
import { ApiEndpoints, apiUrl } from '@lib/index';
|
||||
import { api } from '../../../../App';
|
||||
import type { PreviewAreaComponent } from '../TemplateEditor';
|
||||
|
||||
@ -50,8 +51,37 @@ export const PdfPreviewComponent: PreviewAreaComponent = forwardRef(
|
||||
throw new Error(preview.data);
|
||||
}
|
||||
|
||||
if (preview?.data?.output) {
|
||||
preview = await api.get(preview.data.output, {
|
||||
let outputUrl = preview?.data?.output;
|
||||
if (preview.data && !preview.data.complete) {
|
||||
outputUrl = await new Promise((res, rej) => {
|
||||
let cnt = 0;
|
||||
const interval = setInterval(() => {
|
||||
api
|
||||
.get(apiUrl(ApiEndpoints.data_output, preview.data.pk))
|
||||
.then((response) => {
|
||||
if (response.data.error) {
|
||||
clearInterval(interval);
|
||||
rej(response.data.error);
|
||||
}
|
||||
|
||||
if (response.data.complete) {
|
||||
clearInterval(interval);
|
||||
res(response.data.output);
|
||||
}
|
||||
|
||||
// timeout after 1 minute
|
||||
if (cnt > 2 * 60) {
|
||||
clearInterval(interval);
|
||||
rej('Timeout');
|
||||
}
|
||||
cnt++;
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
}
|
||||
|
||||
if (outputUrl) {
|
||||
preview = await api.get(outputUrl, {
|
||||
responseType: 'blob'
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user