IO-3464 S3 Document Editor

Signed-off-by: Allan Carr <allan@imexsystems.ca>
This commit is contained in:
Allan Carr
2025-12-12 19:39:02 -08:00
parent 6ea1c291e6
commit 56d50b855b
3 changed files with 140 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
//import "tui-image-editor/dist/tui-image-editor.css";
import axios from "axios";
import { Result } from "antd";
import * as markerjs2 from "markerjs2";
import { useCallback, useEffect, useRef, useState } from "react";
@@ -6,8 +7,7 @@ import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop, selectCurrentUser } from "../../redux/user/user.selectors";
import { handleUpload } from "../documents-upload/documents-upload.utility";
import { GenerateSrcUrl } from "../jobs-documents-gallery/job-documents.utility";
import { handleUpload } from "../documents-upload-imgproxy/documents-upload-imgproxy.utility.js";
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
@@ -23,6 +23,9 @@ export function DocumentEditorComponent({ currentUser, bodyshop, document }) {
const imgRef = useRef(null);
const [loading, setLoading] = useState(false);
const [uploaded, setuploaded] = useState(false);
const [imageUrl, setImageUrl] = useState(null);
const [imageLoaded, setImageLoaded] = useState(false);
const [imageLoading, setImageLoading] = useState(true);
const markerArea = useRef(null);
const { t } = useTranslation();
const notification = useNotification();
@@ -55,7 +58,7 @@ export function DocumentEditorComponent({ currentUser, bodyshop, document }) {
);
useEffect(() => {
if (imgRef.current !== null) {
if (imgRef.current !== null && imageLoaded && !markerArea.current) {
// create a marker.js MarkerArea
markerArea.current = new markerjs2.MarkerArea(imgRef.current);
@@ -78,7 +81,47 @@ export function DocumentEditorComponent({ currentUser, bodyshop, document }) {
//markerArea.current.settings.displayMode = "inline";
markerArea.current.show();
}
}, [triggerUpload]);
}, [triggerUpload, imageLoaded]);
useEffect(() => {
// When the document changes, fetch the image via axios so auth and base URL are applied
let isCancelled = false;
const loadImage = async () => {
if (!document || !document.id) return;
setImageLoaded(false);
setImageLoading(true);
try {
const response = await axios.post(
"/media/imgproxy/original",
{ documentId: document.id },
{ responseType: "blob" }
);
if (isCancelled) return;
const blobUrl = URL.createObjectURL(response.data);
setImageUrl((prevUrl) => {
if (prevUrl) URL.revokeObjectURL(prevUrl);
return blobUrl;
});
} catch (error) {
console.error("Failed to fetch original image blob", error);
} finally {
if (!isCancelled) {
setImageLoading(false);
}
}
};
loadImage();
return () => {
isCancelled = true;
};
}, [document]);
async function b64toBlob(url) {
const res = await fetch(url);
@@ -87,16 +130,21 @@ export function DocumentEditorComponent({ currentUser, bodyshop, document }) {
return (
<div>
{!loading && !uploaded && (
{!loading && !uploaded && imageUrl && (
<img
ref={imgRef}
src={GenerateSrcUrl(document)}
src={imageUrl}
alt="sample"
crossOrigin="anonymous"
onLoad={() => setImageLoaded(true)}
onError={(error) => {
console.error("Failed to load original image", error);
}}
style={{ maxWidth: "90vw", maxHeight: "90vh" }}
/>
)}
{loading && <LoadingSpinner message={t("documents.labels.uploading")} />}
{(loading || imageLoading || !imageLoaded) && !uploaded && (
<LoadingSpinner message={t("documents.labels.uploading")} />
)}
{uploaded && <Result status="success" title={t("documents.successes.edituploaded")} />}
</div>
);