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

Replace custom activities tab onSubmit action with default turbo:submit

Kabiru Mwenja587f9ee6bfa3
diff --git a/app/components/work_packages/activities_tab/index_component.html.erb b/app/components/work_packages/activities_tab/index_component.html.erb
index 5eba6927ac0..fcacece9d01 100644
--- a/app/components/work_packages/activities_tab/index_component.html.erb
+++ b/app/components/work_packages/activities_tab/index_component.html.erb
@@ -42,7 +42,7 @@
data: add_comment_wrapper_data_attributes
) do
render(
- WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:)
+ WorkPackages::ActivitiesTab::Journals::NewComponent.new(work_package:, filter:, last_server_timestamp:)
)
end
end
diff --git a/app/components/work_packages/activities_tab/journals/new_component.html.erb b/app/components/work_packages/activities_tab/journals/new_component.html.erb
index 14f016249f8..99deb177e13 100644
--- a/app/components/work_packages/activities_tab/journals/new_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/new_component.html.erb
@@ -42,14 +42,13 @@
data: { "work-packages--activities-tab--index-target": "formRow" }
) do
primer_form_with(
- id: "work-package-journal-form-element", # required for specs
+ id: "work-package-journal-form-element",
model: journal,
method: :post,
data: {
turbo: true,
turbo_stream: true,
"work-packages--activities-tab--index-target": "form",
- action: "submit->work-packages--activities-tab--index#onSubmit",
test_selector: "op-work-package-journal-form-element"
},
url: work_package_activities_path(work_package_id: work_package.id)
@@ -59,7 +58,12 @@
classes: "work-packages-activities-tab-journals-new-component--ck-editor-column",
mb: 3
) do
- render(WorkPackages::ActivitiesTab::Journals::NotesForm.new(f))
+ render(
+ Primer::Forms::FormList.new(
+ WorkPackages::ActivitiesTab::Journals::HiddenForm.new(f, filter:, last_server_timestamp:),
+ WorkPackages::ActivitiesTab::Journals::NotesForm.new(f)
+ )
+ )
end
form_container.with_row(flex_layout: true, justify_content: adding_internal_comment_allowed? ? :space_between : :flex_end) do |form_container_row|
diff --git a/app/components/work_packages/activities_tab/journals/new_component.rb b/app/components/work_packages/activities_tab/journals/new_component.rb
index a0e796f92d5..903c1662757 100644
--- a/app/components/work_packages/activities_tab/journals/new_component.rb
+++ b/app/components/work_packages/activities_tab/journals/new_component.rb
@@ -35,17 +35,19 @@ module WorkPackages
include OpTurbo::Streamable
include WorkPackages::ActivitiesTab::StimulusControllers
- def initialize(work_package:, journal: nil, form_hidden_initially: true)
+ def initialize(work_package:, filter:, last_server_timestamp:, journal: nil, form_hidden_initially: true)
super
@work_package = work_package
+ @filter = filter
+ @last_server_timestamp = last_server_timestamp
@journal = journal
@form_hidden_initially = form_hidden_initially
end
private
- attr_reader :work_package, :form_hidden_initially
+ attr_reader :work_package, :filter, :last_server_timestamp, :form_hidden_initially
def journal
@journal || Journal.new(journable: work_package)
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index 38c9c6cc656..ea6b2a35321 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -216,7 +216,7 @@ class WorkPackages::ActivitiesTabController < ApplicationController
end
def set_filter
- @filter = params[:filter]&.to_sym || :all
+ @filter = (params[:filter] || params.dig(:journal, :filter))&.to_sym || :all
end
def journal_sorting
@@ -255,7 +255,10 @@ class WorkPackages::ActivitiesTabController < ApplicationController
end
def perform_update_streams_from_last_update_timestamp
- if params[:last_update_timestamp].present? && (last_updated_at = Time.zone.parse(params[:last_update_timestamp]))
+ last_update_timestamp = params[:last_update_timestamp] || params.dig(:journal, :last_update_timestamp)
+
+ if last_update_timestamp.present?
+ last_updated_at = Time.zone.parse(last_update_timestamp)
generate_time_based_update_streams(last_updated_at)
generate_work_package_journals_emoji_reactions_update_streams
else
diff --git a/app/forms/work_packages/activities_tab/journals/hidden_form.rb b/app/forms/work_packages/activities_tab/journals/hidden_form.rb
new file mode 100644
index 00000000000..448fa38e457
--- /dev/null
+++ b/app/forms/work_packages/activities_tab/journals/hidden_form.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+#
+#-- 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.
+#++
+
+module WorkPackages::ActivitiesTab::Journals
+ class HiddenForm < ApplicationForm
+ form do |journals_form|
+ journals_form.hidden(name: :last_update_timestamp, value: @last_server_timestamp)
+ journals_form.hidden(name: :filter, value: @filter)
+ end
+
+ def initialize(last_server_timestamp:, filter: :all)
+ super()
+ @last_server_timestamp = last_server_timestamp
+ @filter = filter
+ end
+ end
+end
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
index b07a2134f00..01fef83c9bd 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
@@ -1,33 +1,3 @@
-/*
- * -- copyright
- * OpenProject is an open source project management software.
- * Copyright (C) 2023 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 { Controller } from '@hotwired/stimulus';
import {
ICKEditorInstance,
@@ -89,14 +59,14 @@ export default class IndexController extends Controller {
private handleWorkPackageUpdateBound:EventListener;
private handleVisibilityChangeBound:EventListener;
private rescueEditorContentBound:EventListener;
+ private handleTurboSubmitStartBound:EventListener;
+ private handleTurboSubmitEndBound:EventListener;
- private onSubmitBound:EventListener;
private onEscapeEditorBound:EventListener;
private adjustMarginBound:EventListener;
private onBlurEditorBound:EventListener;
private onFocusEditorBound:EventListener;
- private saveInProgress:boolean;
private updateInProgress:boolean;
private turboRequests:TurboRequestsService;
@@ -113,7 +83,7 @@ export default class IndexController extends Controller {
this.handleInitialScroll();
this.populateRescuedEditorContent();
this.markAsConnected();
- this.safeUpdateWorkPackageFormsWithStateChecks(); // required if switching back to the activities tab from another tab
+ this.safeUpdateWorkPackageFormsWithStateChecks();
this.setLatestKnownChangesetUpdatedAt();
this.startPolling();
@@ -149,10 +119,16 @@ export default class IndexController extends Controller {
this.handleVisibilityChangeBound = () => { void this.handleVisibilityChange(); };
this.rescueEditorContentBound = () => { void this.rescueEditorContent(); };
+ this.handleTurboSubmitStartBound = (event:Event) => { void this.handleTurboSubmitStart(event); };
+ this.handleTurboSubmitEndBound = (event:Event) => { void this.handleTurboSubmitEnd(event); };
+
document.addEventListener('work-package-updated', this.handleWorkPackageUpdateBound);
document.addEventListener('work-package-notifications-updated', this.handleWorkPackageUpdateBound);
document.addEventListener('visibilitychange', this.handleVisibilityChangeBound);
document.addEventListener('beforeunload', this.rescueEditorContentBound);
+
+ this.element.addEventListener('turbo:submit-start', this.handleTurboSubmitStartBound);
+ this.element.addEventListener('turbo:submit-end', this.handleTurboSubmitEndBound);
}
private removeEventListeners() {
@@ -160,6 +136,9 @@ export default class IndexController extends Controller {
document.removeEventListener('work-package-notifications-updated', this.handleWorkPackageUpdateBound);
document.removeEventListener('visibilitychange', this.handleVisibilityChangeBound);
document.removeEventListener('beforeunload', this.rescueEditorContentBound);
+
+ this.element.removeEventListener('turbo:submit-start', this.handleTurboSubmitStartBound);
+ this.element.removeEventListener('turbo:submit-end', this.handleTurboSubmitEndBound);
}
private handleVisibilityChange() {
@@ -547,7 +526,6 @@ export default class IndexController extends Controller {
}
private addEventListenersToCkEditorInstance() {
- this.onSubmitBound = () => { void this.onSubmit(); };
this.onEscapeEditorBound = () => { void this.closeEditor(); };
this.adjustMarginBound = () => { void this.adjustJournalContainerMargin(); };
this.onBlurEditorBound = () => { void this.onBlurEditor(); };
@@ -560,7 +538,6 @@ export default class IndexController extends Controller {
const editorElement = this.getCkEditorElement();
if (editorElement) {
- editorElement.addEventListener('saveRequested', this.onSubmitBound);
editorElement.addEventListener('editorEscape', this.onEscapeEditorBound);
editorElement.addEventListener('editorKeyup', this.adjustMarginBound);
editorElement.addEventListener('editorBlur', this.onBlurEditorBound);
@@ -571,7 +548,6 @@ export default class IndexController extends Controller {
private removeEventListenersFromCkEditorInstance() {
const editorElement = this.getCkEditorElement();
if (editorElement) {
- editorElement.removeEventListener('saveRequested', this.onSubmitBound);
editorElement.removeEventListener('editorEscape', this.onEscapeEditorBound);
editorElement.removeEventListener('editorKeyup', this.adjustMarginBound);
editorElement.removeEventListener('editorBlur', this.onBlurEditorBound);
@@ -704,52 +680,45 @@ export default class IndexController extends Controller {
this.adjustJournalContainerMargin();
}
- private closeForm() {
- this.hideEditor();
- this.formTarget.reset();
- this.notifyFormClose();
- }
-
private isEditorEmpty():boolean {
const ckEditorInstance = this.getCkEditorInstance();
return !!(ckEditorInstance?.getData({ trim: false }).length === 0);
}
- async onSubmit(event:Event | null = null) {
- event?.preventDefault();
+ private handleTurboSubmitStart(_event:Event) {
+ this.setCKEditorReadonlyMode(true);
+ }
- if (this.saveInProgress === true) return;
+ private handleTurboSubmitEnd(event:Event) {
+ const formSubmitResponse = (event as CustomEvent<{ fetchResponse:{ succeeded:boolean; response:{ headers:Headers } } }>).detail.fetchResponse;
- this.setFormSubmitInProgress(true);
+ this.setCKEditorReadonlyMode(false);
- const formData = this.prepareFormData();
- void this.submitForm(formData)
- .then(({ html, headers }) => {
- this.handleSuccessfulSubmission(html, headers);
- this.formTarget.reset();
- })
- .catch((error) => {
- console.error('Error saving activity:', error);
- })
- .finally(() => {
- this.setFormSubmitInProgress(false);
- this.notifyFormClose();
- });
- }
+ if (formSubmitResponse.succeeded) {
+ // extract server timestamp from response headers in order to be in sync with the server
+ this.setLastServerTimestampViaHeaders(formSubmitResponse.response.headers);
- private notifyFormClose() {
- this.dispatch('onSubmit-end');
- }
+ if (!this.journalsContainerTarget) return;
- private setFormSubmitInProgress(inProgress:boolean) {
- this.saveInProgress = inProgress;
+ this.clearEditor();
+ this.closeForm();
+ this.resetJournalsContainerMargins();
- if (this.hasFormSubmitButtonTarget) {
- this.formSubmitButtonTarget.disabled = inProgress;
+ setTimeout(() => {
+ if (this.isMobile() && !this.isWithinNotificationCenter()) {
+ // wait for the keyboard to be fully down before scrolling further
+ // timeout amount tested on mobile devices for best possible user experience
+ this.scrollInputContainerIntoView(800);
+ } else {
+ this.scrollJournalContainer(
+ this.sortingValue === 'asc',
+ true,
+ );
+ }
+ this.handleStemVisibility();
+ }, 10);
}
-
- this.setCKEditorReadonlyMode(inProgress);
}
private setCKEditorReadonlyMode(disabled:boolean) {
@@ -765,55 +734,6 @@ export default class IndexController extends Controller {
}
}
- private prepareFormData():FormData {
- const ckEditorInstance = this.getCkEditorInstance();
- const data = ckEditorInstance ? ckEditorInstance.getData({ trim: false }) : '';
-
- const formData = new FormData(this.formTarget);
- formData.append('last_update_timestamp', this.lastServerTimestampValue);
- formData.append('filter', this.filterValue);
- formData.append('journal[notes]', data);
-
- return formData;
- }
-
- private async submitForm(formData:FormData):Promise<{ html:string, headers:Headers }> {
- return this.turboRequests.request(this.formTarget.action, {
- method: 'POST',
- body: formData,
- headers: {
- 'X-CSRF-Token': (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement).content,
- },
- }, true);
- }
-
- private handleSuccessfulSubmission(html:string, headers:Headers) {
- // extract server timestamp from response headers in order to be in sync with the server
- this.setLastServerTimestampViaHeaders(headers);
-
- if (!this.journalsContainerTarget) return;
-
- this.clearEditor();
- this.hideEditor();
- this.resetJournalsContainerMargins();
-
- setTimeout(() => {
- if (this.isMobile() && !this.isWithinNotificationCenter()) {
- // wait for the keyboard to be fully down before scrolling further
- // timeout amount tested on mobile devices for best possible user experience
- this.scrollInputContainerIntoView(800);
- } else {
- this.scrollJournalContainer(
- this.sortingValue === 'asc',
- true,
- );
- }
- this.handleStemVisibility();
- }, 10);
-
- this.setFormSubmitInProgress(false);
- }
-
private resetJournalsContainerMargins():void {
if (!this.journalsContainerTarget) return;
@@ -827,6 +747,16 @@ export default class IndexController extends Controller {
}
}
+ private closeForm() {
+ this.hideEditor();
+ this.formTarget.reset();
+ this.notifyFormClose();
+ }
+
+ private notifyFormClose() {
+ this.dispatch('onSubmit-end');
+ }
+
// Towards the code below:
// Ideally the stem rendering would be correctly rendered for all UI states from the server
// but as we push single elements into the DOM via turbo-streams, the server-side rendered collection state gets stale quickly