Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/pluggableWidgets/file-uploader-web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- We fixed an issue where validation errors could not be dismissed and persisted after uploading a valid file.

## [2.4.2] - 2026-04-23

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactElement, useCallback } from "react";
import { FileUploaderContainerProps } from "../../typings/FileUploaderProps";
import { ActionButton, FileActionButton } from "./ActionButton";

Check warning on line 3 in packages/pluggableWidgets/file-uploader-web/src/components/ActionsBar.tsx

View workflow job for this annotation

GitHub Actions / Run code quality check

`./ActionButton` import should occur before import of `../../typings/FileUploaderProps`
import { IconInternal } from "@mendix/widget-plugin-component-kit/IconInternal";

Check warning on line 4 in packages/pluggableWidgets/file-uploader-web/src/components/ActionsBar.tsx

View workflow job for this annotation

GitHub Actions / Run code quality check

`@mendix/widget-plugin-component-kit/IconInternal` import should occur before import of `../../typings/FileUploaderProps`
import { FileStore } from "../stores/FileStore";
import { useTranslationsStore } from "../utils/useTranslationsStore";
Expand All @@ -11,6 +11,10 @@
}

export const ActionsBar = ({ actions, store }: ButtonsBarProps): ReactElement | null => {
if (store.fileStatus === "validationError") {
return <DismissActionsBar store={store} />;
}

if (!actions) {
return <DefaultActionsBar store={store} />;
}
Expand Down Expand Up @@ -70,6 +74,25 @@
);
}

function DismissActionsBar({ store }: ButtonsBarProps): ReactElement {
const translations = useTranslationsStore();

const onDismiss = useCallback(() => {
store.dismiss();
}, [store]);

return (
<div className={"entry-details-actions"}>
<ActionButton
icon={<span className={"remove-icon"} aria-hidden />}
title={translations.get("removeButtonTextMessage")}
action={onDismiss}
isDisabled={false}
/>
</div>
);
}

function onDownloadClick(fileUrl: string | undefined): void {
if (!fileUrl) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";
import { FileUploaderContainerProps } from "../../../typings/FileUploaderProps";
import { FileStore } from "../../stores/FileStore";
import { TranslationsStoreProvider } from "../../utils/useTranslationsStore";
import { ActionsBar } from "../ActionsBar";

jest.mock("../../utils/mx-data", () => ({
fetchDocumentUrl: jest.fn(),
fetchImageThumbnail: jest.fn(),
fetchMxObject: jest.fn(),
removeObject: jest.fn(),
saveFile: jest.fn(),
fileHasContents: jest.fn()
}));

function makeFakeProps(): FileUploaderContainerProps {
const dv = (v: string): { value: string; status: string } => ({ value: v, status: "available" });
return {
name: "fileUploader1",
uploadMode: "files",
maxFileSize: 10,
maxFilesPerUpload: { value: { toNumber: () => 5 } },
readOnlyMode: false,
objectCreationTimeout: 30,
allowedFileFormats: "",
removeButtonTextMessage: dv("Remove"),
downloadButtonTextMessage: dv("Download"),
unavailableCreateActionMessage: dv("Unavailable"),
uploadFailureTooManyFilesMessage: dv("Too many"),
uploadFailureInvalidFileFormatMessage: dv("Invalid format"),
uploadFailureFileIsTooBigMessage: dv("Too big")
} as unknown as FileUploaderContainerProps;
}

function makeValidationErrorStore(): { store: FileStore; dismiss: jest.Mock } {
const dismiss = jest.fn();
const rootStore = { dismissFile: dismiss, _uploadMode: "files", isReadOnly: false } as any;
const store = FileStore.newFileWithError(new File([], "bad.txt"), "bad format", rootStore);
return { store, dismiss };
}

function renderWithTranslations(store: FileStore): void {
const props = makeFakeProps();
render(
<TranslationsStoreProvider props={props}>
<ActionsBar store={store} />
</TranslationsStoreProvider>
);
}

describe("DismissActionsBar", () => {
it("renders dismiss button for validationError file", () => {
const { store } = makeValidationErrorStore();
renderWithTranslations(store);
expect(screen.getByRole("button")).toBeInTheDocument();
});

it("calls store.dismiss() when button clicked", () => {
const { store } = makeValidationErrorStore();
const dismissSpy = jest.spyOn(store, "dismiss");
renderWithTranslations(store);
fireEvent.click(screen.getByRole("button"));
expect(dismissSpy).toHaveBeenCalledTimes(1);
});

it("does not render DismissActionsBar for non-validationError file", () => {
const rootStore = { dismissFile: jest.fn(), _uploadMode: "files", isReadOnly: false } as any;
const store = FileStore.newFile(new File([], "ok.txt"), rootStore);
(store as any).fileStatus = "done";
renderWithTranslations(store);
// DefaultActionsBar renders 2 buttons (download + remove)
expect(screen.queryAllByRole("button")).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class FileStore {
imagePreviewUrl: computed,
upload: action,
fetchMxObject: action,
markMissing: action
markMissing: action,
dismiss: action
});
}

Expand All @@ -76,6 +77,10 @@ export class FileStore {
this.errorDescription = errorMessage;
}

dismiss(): void {
this._rootStore.dismissFile(this);
}

canExecute(listAction: ListActionValue): boolean {
if (!this._objectItem) {
return false;
Expand Down Expand Up @@ -123,6 +128,7 @@ export class FileStore {
runInAction(() => {
this.fileStatus = "done";
this._rootStore.objectCreationHelper.reportUploadSuccess(this._objectItem!);
this._rootStore.dismissValidationErrors();
});
} catch (_e: unknown) {
runInAction(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class FileUploaderStore {
makeObservable(this, {
updateProps: action,
processDrop: action,
dismissValidationErrors: action,
dismissFile: action,
setMessage: action,
processExistingFileItem: action,
files: observable,
Expand Down Expand Up @@ -142,6 +144,14 @@ export class FileUploaderStore {
this.errorMessage = msg;
}

dismissValidationErrors(): void {
this.files = this.files.filter(file => file.fileStatus !== "validationError");
}

dismissFile(file: FileStore): void {
this.files = this.files.filter(f => f !== file);
}

processDrop(acceptedFiles: File[], fileRejections: FileRejection[]): void {
if (!this.objectCreationHelper.canCreateFiles) {
console.error(
Expand All @@ -158,6 +168,7 @@ export class FileUploaderStore {
return;
}

this.dismissValidationErrors();
this.setMessage();

for (const file of fileRejections) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FileStore } from "../FileStore";
import { FileUploaderStore } from "../FileUploaderStore";

jest.mock("../../utils/mx-data", () => ({
fetchDocumentUrl: jest.fn(),
fetchImageThumbnail: jest.fn(),
fetchMxObject: jest.fn(),
removeObject: jest.fn(),
saveFile: jest.fn(),
fileHasContents: jest.fn()
}));

function makeRootStore(): { dismissFile: jest.Mock } {
return {
dismissFile: jest.fn(),
_uploadMode: "files",
isReadOnly: false
} as unknown as FileUploaderStore & { dismissFile: jest.Mock };
}

describe("FileStore.dismiss()", () => {
it("calls dismissFile on root store with itself", () => {
const rootStore = makeRootStore();
const store = FileStore.newFileWithError(new File([], "test.txt"), "bad format", rootStore as any);
store.dismiss();
expect(rootStore.dismissFile).toHaveBeenCalledTimes(1);
expect(rootStore.dismissFile).toHaveBeenCalledWith(store);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { FileStore } from "../FileStore";
import { FileUploaderStore } from "../FileUploaderStore";
import { TranslationsStore } from "../TranslationsStore";

jest.mock("../../utils/mx-data", () => ({
fetchDocumentUrl: jest.fn(),
fetchImageThumbnail: jest.fn(),
fetchMxObject: jest.fn(),
removeObject: jest.fn(),
saveFile: jest.fn(),
fileHasContents: jest.fn()
}));

function makeStore(): FileUploaderStore {
const translations = { get: jest.fn(() => "msg"), updateProps: jest.fn() } as unknown as TranslationsStore;
const store = Object.create(FileUploaderStore.prototype) as FileUploaderStore;
store.files = [];
return Object.assign(store, {
translations,
objectCreationHelper: { canCreateFiles: true, enable: jest.fn(), updateProps: jest.fn(), request: jest.fn() },
updateProcessor: { processUpdate: jest.fn() },
isReadOnly: false,
_uploadMode: "files" as const,
_maxFileSizeMiB: 10,
_maxFileSize: 10 * 1024 * 1024,
acceptedFileTypes: [],
existingItemsLoaded: false
});
}

function makeValidationErrorFile(store: FileUploaderStore): FileStore {
return FileStore.newFileWithError(new File([], "bad.txt"), "bad format", store);
}

function makeDoneFile(store: FileUploaderStore): FileStore {
const f = FileStore.newFile(new File([], "good.txt"), store);
(f as any).fileStatus = "done";
return f;
}

describe("FileUploaderStore.dismissValidationErrors()", () => {
it("removes only validationError files", () => {
const store = makeStore();
const validationFile = makeValidationErrorFile(store);
const doneFile = makeDoneFile(store);
store.files = [validationFile, doneFile];

store.dismissValidationErrors();

expect(store.files).toHaveLength(1);
expect(store.files[0]).toBe(doneFile);
});

it("leaves files untouched when none have validationError status", () => {
const store = makeStore();
const doneFile = makeDoneFile(store);
store.files = [doneFile];

store.dismissValidationErrors();

expect(store.files).toHaveLength(1);
});

it("empties files when all have validationError status", () => {
const store = makeStore();
store.files = [makeValidationErrorFile(store), makeValidationErrorFile(store)];

store.dismissValidationErrors();

expect(store.files).toHaveLength(0);
});
});

describe("FileUploaderStore.dismissFile()", () => {
it("removes the specific file from the list", () => {
const store = makeStore();
const fileA = makeValidationErrorFile(store);
const fileB = makeValidationErrorFile(store);
store.files = [fileA, fileB];

store.dismissFile(fileA);

expect(store.files).toHaveLength(1);
expect(store.files[0]).toBe(fileB);
});

it("does not remove other files", () => {
const store = makeStore();
const fileA = makeValidationErrorFile(store);
const fileB = makeDoneFile(store);
store.files = [fileA, fileB];

store.dismissFile(fileA);

expect(store.files[0]).toBe(fileB);
});

it("is a no-op when file is not in the list", () => {
const store = makeStore();
const fileA = makeValidationErrorFile(store);
const fileB = makeValidationErrorFile(store);
store.files = [fileA];

store.dismissFile(fileB);

expect(store.files).toHaveLength(1);
expect(store.files[0]).toBe(fileA);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ Place your custom CSS here
}

&.invalid {
opacity: 0.7;
.entry-details-preview,
.entry-details-main {
opacity: 0.5;
}
.download-icon {
visibility: hidden;
}
.entry-details-actions {
display: none;
}
}
}
}
Expand Down
Loading