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

DT-340: Warn the user if they are leaving a story edit page with unsaved changes #334

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions app/assets/javascripts/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,57 @@ document.addEventListener("DOMContentLoaded", () => {
debounceTimer = window.setTimeout(updateMarkdown, 300);
});
});

const form = document.querySelector(".edit_story");
const backButton = document.getElementById("back");
const logo = document.getElementById("logo");
let isDirty = false;

if (form) {
// Mark the form as dirty when any input changes
form.addEventListener("input", function () {
isDirty = true;
addBeforeUnloadEventListener(isDirty);
});

// Reset isDirty on form submission
form.addEventListener("submit", function () {
isDirty = false;
addBeforeUnloadEventListener(isDirty);
});
}

// Attach a click event to the custom back button
[backButton, logo].forEach(element => {
element.addEventListener("click", function (event) {
if (isDirty) {
const confirmLeave = confirm("You have unsaved changes. Are you sure you want to go back?");
if (!confirmLeave) {
// Prevent navigation if the user chooses not to leave
event.preventDefault();
} else {
// Optionally, reset isDirty if leaving
isDirty = false;
addBeforeUnloadEventListener(isDirty)
}
}
})
});
});

function addBeforeUnloadEventListener(isDirty) {
if (isDirty) {
window.addEventListener("beforeunload", warnUserifUnsavedEdits);
} else {
window.removeEventListener("beforeunload", warnUserifUnsavedEdits);
}
}

function warnUserifUnsavedEdits(event) {
event.preventDefault();
event.returnValue = '';
}

function updateStatusButton(color, status) {
const button = document.querySelector(".story-title .dropdown-wrapper > button");
button.className = `button ${color}`;
Expand Down
11 changes: 11 additions & 0 deletions spec/features/stories_manage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
expect(page).to have_content "Story updated!"
end

it "alerts me when I try to navigate away from the page without saving my edits", js: true do
visit project_path(id: project.id)
click_button "More actions"
click_link "Edit"
fill_in "story[title]", with: "As a user, I want to edit stories"
click_link "Back"
alert_text = page.driver.browser.switch_to.alert.text

expect(alert_text).to eq "You have unsaved changes. Are you sure you want to go back?"
end

it "allows me to delete a story" do
visit project_path(id: project.id)

Expand Down
Loading