Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add check for unsaved data before changing status

Mir Bhatiaa36e7f0c01be
diff --git a/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts b/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts
new file mode 100644
index 00000000000..7622564a09c
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts
@@ -0,0 +1,93 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import { ApplicationController } from 'stimulus-use';
+import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
+
+export default class extends ApplicationController {
+ private turboRequests:TurboRequestsService;
+ private boundBeforeUnloadHandler = this.beforeUnloadHandler.bind(this);
+
+ static values = { unsavedChangesConfirmationMessage: String };
+
+ declare unsavedChangesConfirmationMessageValue:string;
+
+ async connect():Promise<void> {
+ window.addEventListener('beforeunload', this.boundBeforeUnloadHandler);
+
+ const context = await window.OpenProject.getPluginContext();
+ this.turboRequests = context.services.turboRequests;
+ }
+
+ disconnect():void {
+ window.removeEventListener('beforeunload', this.boundBeforeUnloadHandler);
+ }
+
+ handleClick(event:Event):void {
+ event.preventDefault();
+
+ const target = event.currentTarget as HTMLElement;
+ const url = target.dataset.href;
+
+ if (!url) return;
+
+ if (this.hasUnsavedChanges()) {
+ // eslint-disable-next-line no-alert
+ if (window.confirm(this.unsavedChangesConfirmationMessageValue)) {
+ this.sendRequest(url);
+ }
+ } else {
+ this.sendRequest(url);
+ }
+ }
+
+ private hasUnsavedChanges():boolean {
+ const textInputs = Array.from(document.querySelectorAll('input[type="text"], input[type="number"]'));
+ const allTextSaved = textInputs.every((input) => (input as HTMLInputElement).value.trim().length === 0);
+
+ return !allTextSaved || window.OpenProject.pageWasEdited;
+ }
+
+ private beforeUnloadHandler(event:BeforeUnloadEvent):void {
+ if (this.hasUnsavedChanges()) {
+ event.preventDefault();
+ }
+ }
+
+ private sendRequest(url:string):void {
+ void this.turboRequests.request(url, {
+ method: 'PUT',
+ headers: {
+ 'X-CSRF-Token': (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement).content,
+ Accept: 'text/vnd.turbo-stream.html',
+ },
+ });
+ }
+}
diff --git a/modules/meeting/app/components/meetings/show_component.html.erb b/modules/meeting/app/components/meetings/show_component.html.erb
index e12c5f570a0..a4d9f57c5e9 100644
--- a/modules/meeting/app/components/meetings/show_component.html.erb
+++ b/modules/meeting/app/components/meetings/show_component.html.erb
@@ -1,12 +1,12 @@
<%=
- flex_layout(data: { turbo: true }) do |show_page|
+ flex_layout(data: show_page_data_attributes) do |show_page|
show_page.with_row do
render(Meetings::HeaderComponent.new(meeting: @meeting))
end
show_page.with_row do
render(Primer::Alpha::Layout.new(stacking_breakpoint: :md)) do |content|
- content.with_main(classes: "dragula-container", data: wrapper_data_attributes) do
+ content.with_main(classes: "dragula-container", data: main_content_data_attributes) do
flex_layout do |agenda|
agenda.with_row do
render(MeetingAgendaItems::ListComponent.new(meeting: @meeting))
diff --git a/modules/meeting/app/components/meetings/show_component.rb b/modules/meeting/app/components/meetings/show_component.rb
index 325f219bd1c..2c313ac0371 100644
--- a/modules/meeting/app/components/meetings/show_component.rb
+++ b/modules/meeting/app/components/meetings/show_component.rb
@@ -41,10 +41,22 @@ module Meetings
private
- def wrapper_data_attributes
+ def show_page_data_attributes
+ {
+ turbo: true,
+ controller: "meetings--check-unsaved",
+ "meetings--check-unsaved-unsaved-changes-confirmation-message-value": unsaved_changes_confirmation_message
+ }
+ end
+
+ def main_content_data_attributes
{
controller: "meetings--drag-and-drop meetings--add-params"
}
end
+
+ def unsaved_changes_confirmation_message
+ I18n.t("activities.work_packages.activity_tab.unsaved_changes_confirmation_message")
+ end
end
end
diff --git a/modules/meeting/app/components/meetings/side_panel/state_component.html.erb b/modules/meeting/app/components/meetings/side_panel/state_component.html.erb
index 84d9a0fe2ef..090116b69bd 100644
--- a/modules/meeting/app/components/meetings/side_panel/state_component.html.erb
+++ b/modules/meeting/app/components/meetings/side_panel/state_component.html.erb
@@ -17,10 +17,10 @@
if edit_enabled?
section.with_footer_button(
tag: :a,
- href: change_state_project_meeting_path(@project, @meeting, state: "in_progress"),
+ href: href("in_progress"),
classes: "hide-when-print",
- data: { "turbo-stream": true, "turbo-method": "put" },
- test_selector: "close-meeting-button"
+ test_selector: "close-meeting-button",
+ data: button_data_attributes(href("in_progress"))
) do |button|
button.with_leading_visual_icon(icon: :play)
t("label_meeting_in_progress_action")
@@ -41,9 +41,9 @@
if edit_enabled?
section.with_footer_button(
tag: :a,
- href: change_state_project_meeting_path(@project, @meeting, state: "open"),
+ href: href("open"),
classes: "hide-when-print",
- data: { "turbo-stream": true, "turbo-method": "put" }
+ data: button_data_attributes(href("open"))
) do |button|
button.with_leading_visual_icon(icon: :unlock)
t("label_meeting_reopen_action")
@@ -64,10 +64,10 @@
if edit_enabled?
section.with_footer_button(
tag: :a,
- href: change_state_project_meeting_path(@project, @meeting, state: "closed"),
+ href: href("closed"),
classes: "hide-when-print",
- data: { "turbo-stream": true, "turbo-method": "put" },
- test_selector: "close-meeting-button"
+ test_selector: "close-meeting-button",
+ data: button_data_attributes(href("closed"))
) do |button|
button.with_leading_visual_icon(icon: :unlock)
t("label_meeting_close_action")
diff --git a/modules/meeting/app/components/meetings/side_panel/state_component.rb b/modules/meeting/app/components/meetings/side_panel/state_component.rb
index e853915a7ab..cd043ae23c0 100644
--- a/modules/meeting/app/components/meetings/side_panel/state_component.rb
+++ b/modules/meeting/app/components/meetings/side_panel/state_component.rb
@@ -51,5 +51,16 @@ module Meetings
def status_button
render(Meetings::SidePanel::StatusButtonComponent.new(meeting: @meeting))
end
+
+ def href(state)
+ change_state_project_meeting_path(@project, @meeting, state: state)
+ end
+
+ def button_data_attributes(href)
+ {
+ action: "click->meetings--check-unsaved#handleClick",
+ href: href
+ }
+ end
end
end
diff --git a/modules/meeting/app/components/meetings/side_panel/status_button_component.rb b/modules/meeting/app/components/meetings/side_panel/status_button_component.rb
index e1599fdcc16..0935598e2e9 100644
--- a/modules/meeting/app/components/meetings/side_panel/status_button_component.rb
+++ b/modules/meeting/app/components/meetings/side_panel/status_button_component.rb
@@ -82,10 +82,10 @@ module Meetings
color_namespace: :meeting_status,
icon: :"issue-opened",
tag: :a,
+ href: href("open"),
description: t("text_meeting_open_dropdown_description"),
- href: change_state_project_meeting_path(@project, @meeting, state: "open"),
content_arguments: {
- data: { "turbo-stream": true, "turbo-method": "put" }
+ data: data_attributes(href("open"))
})
end
@@ -95,10 +95,10 @@ module Meetings
color_namespace: :meeting_status,
icon: :play,
tag: :a,
+ href: href("in_progress"),
description: t("text_meeting_in_progress_dropdown_description"),
- href: change_state_project_meeting_path(@project, @meeting, state: "in_progress"),
content_arguments: {
- data: { "turbo-stream": true, "turbo-method": "put" }
+ data: data_attributes(href("in_progress"))
})
end
@@ -108,11 +108,22 @@ module Meetings
color_namespace: :meeting_status,
icon: :"issue-closed",
tag: :a,
+ href: href("closed"),
description: t("text_meeting_closed_dropdown_description"),
- href: change_state_project_meeting_path(@project, @meeting, state: "closed"),
content_arguments: {
- data: { "turbo-stream": true, "turbo-method": "put" }
+ data: data_attributes(href("closed"))
})
end
+
+ def href(state)
+ change_state_project_meeting_path(@project, @meeting, state: state)
+ end
+
+ def data_attributes(href)
+ {
+ action: "click->meetings--check-unsaved#handleClick",
+ href: href
+ }
+ end
end
end