mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-04 17:12:06 +00:00
fix(work-orders): make extra document opening reliable
This commit is contained in:
parent
e761e0dae5
commit
ba69706fcf
2 changed files with 52 additions and 13 deletions
|
|
@ -50,16 +50,23 @@ type SlideOverExtrasTabProps = {
|
||||||
onCategorize?: (id: string, category: UploadableMediaCategory) => void;
|
onCategorize?: (id: string, category: UploadableMediaCategory) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
let lastExtraDocObjectUrl: string | null = null;
|
|
||||||
|
|
||||||
async function openExtraDocContent(workOrderId: string | number, mediaId: string): Promise<void> {
|
async function openExtraDocContent(workOrderId: string | number, mediaId: string): Promise<void> {
|
||||||
const blob = await workOrdersApi.getMediaContent(workOrderId, mediaId);
|
const tab = window.open("about:blank", "_blank");
|
||||||
if (lastExtraDocObjectUrl) {
|
if (!tab) {
|
||||||
URL.revokeObjectURL(lastExtraDocObjectUrl);
|
toast.error("Unable to open this document. Please allow pop-ups and try again.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const blob = await workOrdersApi.getMediaContent(workOrderId, mediaId);
|
||||||
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
|
tab.opener = null;
|
||||||
|
tab.location.href = objectUrl;
|
||||||
|
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 60_000);
|
||||||
|
} catch {
|
||||||
|
tab.close();
|
||||||
|
toast.error("Unable to open this document.");
|
||||||
}
|
}
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
|
||||||
lastExtraDocObjectUrl = objectUrl;
|
|
||||||
window.open(objectUrl, "_blank", "noopener,noreferrer");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ExtraDocRow({
|
function ExtraDocRow({
|
||||||
|
|
@ -98,9 +105,7 @@ function ExtraDocRow({
|
||||||
if (workOrderId == null) {
|
if (workOrderId == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void openExtraDocContent(workOrderId, file.id).catch(() => {
|
void openExtraDocContent(workOrderId, file.id);
|
||||||
toast.error("Unable to open this document.");
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
className="min-w-0 flex-1 truncate text-left hover:underline disabled:cursor-not-allowed disabled:opacity-50"
|
className="min-w-0 flex-1 truncate text-left hover:underline disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
style={{ fontSize: 12, color: "var(--foreground)" }}
|
style={{ fontSize: 12, color: "var(--foreground)" }}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { fireEvent, screen } from "@testing-library/react";
|
import { fireEvent, screen } from "@testing-library/react";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
import { SlideOverExtrasTab } from "@/app/(protected)/workorders/_components/detail/tabs/slide-over-extras-tab";
|
import { SlideOverExtrasTab } from "@/app/(protected)/workorders/_components/detail/tabs/slide-over-extras-tab";
|
||||||
import { workOrdersApi } from "@/domain/work-orders/api/work-orders-api";
|
import { workOrdersApi } from "@/domain/work-orders/api/work-orders-api";
|
||||||
import type { MediaFile } from "@/domain/work-orders/types/work-order-media";
|
import type { MediaFile } from "@/domain/work-orders/types/work-order-media";
|
||||||
|
|
@ -82,7 +83,12 @@ describe("SlideOverExtrasTab Aveta", () => {
|
||||||
const getMediaContent = vi.spyOn(workOrdersApi, "getMediaContent").mockResolvedValue(blob);
|
const getMediaContent = vi.spyOn(workOrdersApi, "getMediaContent").mockResolvedValue(blob);
|
||||||
const createObjectURL = vi.fn(() => "blob:authorized");
|
const createObjectURL = vi.fn(() => "blob:authorized");
|
||||||
vi.stubGlobal("URL", { ...URL, createObjectURL, revokeObjectURL: vi.fn() });
|
vi.stubGlobal("URL", { ...URL, createObjectURL, revokeObjectURL: vi.fn() });
|
||||||
const open = vi.fn();
|
const tab = {
|
||||||
|
opener: window,
|
||||||
|
location: { href: "about:blank" },
|
||||||
|
close: vi.fn(),
|
||||||
|
};
|
||||||
|
const open = vi.fn(() => tab);
|
||||||
vi.stubGlobal("open", open);
|
vi.stubGlobal("open", open);
|
||||||
|
|
||||||
renderWithProviders(
|
renderWithProviders(
|
||||||
|
|
@ -103,7 +109,35 @@ describe("SlideOverExtrasTab Aveta", () => {
|
||||||
expect(getMediaContent).toHaveBeenCalledWith(10, "m1");
|
expect(getMediaContent).toHaveBeenCalledWith(10, "m1");
|
||||||
});
|
});
|
||||||
expect(createObjectURL).toHaveBeenCalledWith(blob);
|
expect(createObjectURL).toHaveBeenCalledWith(blob);
|
||||||
expect(open).toHaveBeenCalledWith("blob:authorized", "_blank", "noopener,noreferrer");
|
expect(open).toHaveBeenCalledWith("about:blank", "_blank");
|
||||||
|
expect(tab.opener).toBeNull();
|
||||||
|
expect(tab.location.href).toBe("blob:authorized");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows an error when the browser blocks the document tab", async () => {
|
||||||
|
const getMediaContent = vi.spyOn(workOrdersApi, "getMediaContent");
|
||||||
|
const toastError = vi.spyOn(toast, "error").mockImplementation(vi.fn());
|
||||||
|
vi.stubGlobal(
|
||||||
|
"open",
|
||||||
|
vi.fn(() => null),
|
||||||
|
);
|
||||||
|
|
||||||
|
renderWithProviders(
|
||||||
|
<SlideOverExtrasTab
|
||||||
|
uploads={[photo]}
|
||||||
|
workOrderId={10}
|
||||||
|
isReadOnly
|
||||||
|
onUpload={vi.fn()}
|
||||||
|
onRemove={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "safety-form.pdf" }));
|
||||||
|
|
||||||
|
expect(toastError).toHaveBeenCalledWith(
|
||||||
|
"Unable to open this document. Please allow pop-ups and try again.",
|
||||||
|
);
|
||||||
|
expect(getMediaContent).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows Extra Docs accept copy with PDF DOC and 50MB", () => {
|
it("shows Extra Docs accept copy with PDF DOC and 50MB", () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue