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

Bug/58890 new comment on wp requires page reload to appear (#17140)

jjabari-op79caf0088fc3
diff --git a/app/components/work_packages/activities_tab/index_component.rb b/app/components/work_packages/activities_tab/index_component.rb
index acad2fe5006..92f4cdad2b6 100644
--- a/app/components/work_packages/activities_tab/index_component.rb
+++ b/app/components/work_packages/activities_tab/index_component.rb
@@ -35,16 +35,17 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(work_package:, filter: :all)
+ def initialize(work_package:, last_server_timestamp:, filter: :all)
super
@work_package = work_package
@filter = filter
+ @last_server_timestamp = last_server_timestamp
end
private
- attr_reader :work_package, :filter
+ attr_reader :work_package, :filter, :last_server_timestamp
def wrapper_data_attributes
{
@@ -59,7 +60,8 @@ module WorkPackages
"work-packages--activities-tab--index-user-id-value": User.current.id,
"work-packages--activities-tab--index-work-package-id-value": work_package.id,
"work-packages--activities-tab--index-polling-interval-in-ms-value": polling_interval,
- "work-packages--activities-tab--index-notification-center-path-name-value": notifications_path
+ "work-packages--activities-tab--index-notification-center-path-name-value": notifications_path,
+ "work-packages--activities-tab--index-last-server-timestamp-value": last_server_timestamp
}
end
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index ab1b16aafb7..8aec07f8f80 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -42,13 +42,16 @@ class WorkPackages::ActivitiesTabController < ApplicationController
render(
WorkPackages::ActivitiesTab::IndexComponent.new(
work_package: @work_package,
- filter: @filter
+ filter: @filter,
+ last_server_timestamp: get_current_server_timestamp
),
layout: false
)
end
def update_streams
+ set_last_server_timestamp_to_headers
+
perform_update_streams_from_last_update_timestamp
respond_with_turbo_streams
@@ -115,6 +118,7 @@ class WorkPackages::ActivitiesTabController < ApplicationController
call = create_journal_service_call
if call.success? && call.result
+ set_last_server_timestamp_to_headers
handle_successful_create_call(call)
else
handle_failed_create_call(call) # errors should be rendered in the form
@@ -284,7 +288,8 @@ class WorkPackages::ActivitiesTabController < ApplicationController
replace_via_turbo_stream(
component: WorkPackages::ActivitiesTab::IndexComponent.new(
work_package: @work_package,
- filter: @filter
+ filter: @filter,
+ last_server_timestamp: get_current_server_timestamp
)
)
end
@@ -427,4 +432,14 @@ class WorkPackages::ActivitiesTabController < ApplicationController
def allowed_to_edit?(journal)
journal.editable_by?(User.current)
end
+
+ def get_current_server_timestamp
+ # single source of truth for the server timestamp format
+ Time.current.iso8601(3)
+ end
+
+ def set_last_server_timestamp_to_headers
+ # Add server timestamp to response in order to let the client be in sync with the server
+ response.headers["X-Server-Timestamp"] = get_current_server_timestamp
+ end
end
diff --git a/frontend/src/app/core/turbo/turbo-requests.service.ts b/frontend/src/app/core/turbo/turbo-requests.service.ts
index f5dfb7364c0..23bae83a6c9 100644
--- a/frontend/src/app/core/turbo/turbo-requests.service.ts
+++ b/frontend/src/app/core/turbo/turbo-requests.service.ts
@@ -10,24 +10,28 @@ export class TurboRequestsService {
}
- public request(url:string, init:RequestInit = {}):Promise<unknown> {
+ public request(url:string, init:RequestInit = {}):Promise<{ html:string, headers:Headers }> {
return fetch(url, init)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
-
- return response.text();
+ return response.text().then((html) => ({
+ html,
+ headers: response.headers,
+ }));
})
- .then((html) => {
- renderStreamMessage(html);
-
- return html; // enable further processing wherever this is called
- })
- .catch((error) => this.toast.addError(error as string));
+ .then((result) => {
+ renderStreamMessage(result.html);
+ return result;
+ })
+ .catch((error) => {
+ this.toast.addError(error as string);
+ throw error;
+ });
}
- public requestStream(url:string):Promise<unknown> {
+ public requestStream(url:string):Promise<{ html:string, headers:Headers }> {
return this.request(url, {
method: 'GET',
headers: { Accept: 'text/vnd.turbo-stream.html' },
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 80c8305a316..38252596273 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
@@ -20,6 +20,7 @@ export default class IndexController extends Controller {
userId: Number,
workPackageId: Number,
notificationCenterPathName: String,
+ lastServerTimestamp: String,
};
static targets = ['journalsContainer', 'buttonRow', 'formRow', 'form', 'reactionButton'];
@@ -32,7 +33,7 @@ export default class IndexController extends Controller {
declare updateStreamsUrlValue:string;
declare sortingValue:string;
- declare lastUpdateTimestamp:string;
+ declare lastServerTimestampValue:string;
declare intervallId:number;
declare pollingIntervalInMsValue:number;
declare notificationCenterPathNameValue:string;
@@ -62,7 +63,6 @@ export default class IndexController extends Controller {
this.apiV3Service = context.services.apiV3Service;
this.setLocalStorageKey();
- this.setLastUpdateTimestamp();
this.setupEventListeners();
this.handleInitialScroll();
this.startPolling();
@@ -166,8 +166,8 @@ export default class IndexController extends Controller {
const journalsContainerAtBottom = this.isJournalsContainerScrolledToBottom();
void this.performUpdateStreamsRequest(this.prepareUpdateStreamsUrl())
- .then((html) => {
- this.handleUpdateStreamsResponse(html as string, journalsContainerAtBottom);
+ .then(({ html, headers }) => {
+ this.handleUpdateStreamsResponse(html, headers, journalsContainerAtBottom);
}).catch((error) => {
console.error('Error updating activities list:', error);
}).finally(() => {
@@ -183,11 +183,11 @@ export default class IndexController extends Controller {
const url = new URL(this.updateStreamsUrlValue);
url.searchParams.set('sortBy', this.sortingValue);
url.searchParams.set('filter', this.filterValue);
- url.searchParams.set('last_update_timestamp', this.lastUpdateTimestamp);
+ url.searchParams.set('last_update_timestamp', this.lastServerTimestampValue);
return url.toString();
}
- private performUpdateStreamsRequest(url:string):Promise<unknown> {
+ private performUpdateStreamsRequest(url:string):Promise<{ html:string, headers:Headers }> {
return this.turboRequests.request(url, {
method: 'GET',
headers: {
@@ -196,8 +196,10 @@ export default class IndexController extends Controller {
});
}
- private handleUpdateStreamsResponse(html:string, journalsContainerAtBottom:boolean) {
- this.setLastUpdateTimestamp();
+ private handleUpdateStreamsResponse(html:string, lastResponseHeaders:Headers, journalsContainerAtBottom:boolean) {
+ // extract server timestamp from response headers in order to be in sync with the server
+ this.setLastServerTimestampViaHeaders(lastResponseHeaders);
+
this.checkForAndHandleWorkPackageUpdate(html);
this.checkForNewNotifications(html);
this.performAutoScrolling(html, journalsContainerAtBottom);
@@ -515,8 +517,8 @@ export default class IndexController extends Controller {
const formData = this.prepareFormData();
void this.submitForm(formData)
- .then(() => {
- this.handleSuccessfulSubmission();
+ .then(({ html, headers }) => {
+ this.handleSuccessfulSubmission(html, headers);
})
.catch((error) => {
console.error('Error saving activity:', error);
@@ -531,14 +533,14 @@ export default class IndexController extends Controller {
const data = ckEditorInstance ? ckEditorInstance.getData({ trim: false }) : '';
const formData = new FormData(this.formTarget);
- formData.append('last_update_timestamp', this.lastUpdateTimestamp);
+ 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<unknown> {
+ private async submitForm(formData:FormData):Promise<{ html:string, headers:Headers }> {
return this.turboRequests.request(this.formTarget.action, {
method: 'POST',
body: formData,
@@ -548,8 +550,9 @@ export default class IndexController extends Controller {
});
}
- private handleSuccessfulSubmission() {
- this.setLastUpdateTimestamp();
+ 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;
@@ -588,7 +591,9 @@ export default class IndexController extends Controller {
this.journalsContainerTarget.classList.add('work-packages-activities-tab-index-component--journals-container_with-input-compensation');
}
- setLastUpdateTimestamp() {
- this.lastUpdateTimestamp = new Date().toISOString();
+ private setLastServerTimestampViaHeaders(headers:Headers) {
+ if (headers.has('X-Server-Timestamp')) {
+ this.lastServerTimestampValue = headers.get('X-Server-Timestamp') as string;
+ }
}
}