Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-5074 - Confirm before page unload #2886

Merged
merged 4 commits into from
Nov 2, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ describe(FileUpload.name, () => {

expect(wrapper.text()).toContain(testSlot);
});

it("should not show notification on page unload", async () => {
setup();

const beforeUnloadEvent = new Event("beforeunload");
const preventDefaultSpy = jest.fn();

beforeUnloadEvent.preventDefault = preventDefaultSpy;
window.dispatchEvent(beforeUnloadEvent);

expect(preventDefaultSpy).not.toHaveBeenCalled();
});
});

describe("when file gets picked", () => {
Expand Down Expand Up @@ -82,6 +94,23 @@ describe(FileUpload.name, () => {
const progressLinear = wrapper.find("v-progress-linear-stub");
expect(progressLinear.exists()).toBe(true);
});

it("should show notification on page unload", async () => {
const { wrapper } = setup();

const filePicker = wrapper.findComponent(FilePicker);
expect(filePicker.exists()).toBe(true);

filePicker.vm.$emit("update:file", { fileName: "Test.jpg" });

const beforeUnloadEvent = new Event("beforeunload");
const preventDefaultSpy = jest.fn();

beforeUnloadEvent.preventDefault = preventDefaultSpy;
window.dispatchEvent(beforeUnloadEvent);

expect(preventDefaultSpy).toHaveBeenCalled();
});
});
});

Expand Down
19 changes: 18 additions & 1 deletion src/components/feature-board-file-element/upload/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<script lang="ts">
import { useSharedLastCreatedElement } from "@util-board";
import { defineComponent, ref, watch } from "vue";
import { defineComponent, onBeforeUnmount, onMounted, ref, watch } from "vue";
import FilePicker from "./file-picker/FilePicker.vue";

export default defineComponent({
Expand All @@ -42,6 +42,23 @@ export default defineComponent({
}
});

const handleBeforeUnload = (event: BeforeUnloadEvent) => {
if (fileWasPicked.value) {
// Opens confirmation dialog in firefox
event.preventDefault();
// Opens confirmation dialog in chrome
event.returnValue = "";
}
};

onMounted(() => {
window.addEventListener("beforeunload", handleBeforeUnload);
});

onBeforeUnmount(() => {
window.removeEventListener("beforeunload", handleBeforeUnload);
});

const onFileSelect = async (file: File) => {
fileWasPicked.value = true;
emit("upload:file", file);
Expand Down
Loading