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

Rework backlogs Ajax requests using Request.JS

Alexander Brandon Coles443c01af83cc
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/common.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/common.ts
index 950d76bf277..12937fef187 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/common.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/common.ts
@@ -39,7 +39,6 @@ if (window.RB === null || window.RB === undefined) {
let Factory;
let Dialog;
let UserPreferences;
- let ajax;
object = {
// Douglas Crockford's technique for object extension
@@ -118,38 +117,6 @@ if (window.RB === null || window.RB === undefined) {
},
});
- ajax = (function () {
- let ajaxQueue:any;
- let ajaxOngoing:any;
- let processAjaxQueue:any;
-
- ajaxQueue = [];
- ajaxOngoing = false;
-
- processAjaxQueue = function () {
- const options = ajaxQueue.shift();
-
- if (options !== null && options !== undefined) {
- ajaxOngoing = true;
- $.ajax(options);
- }
- };
-
- // Process outstanding entries in the ajax queue whenever a ajax request
- // finishes.
- $(document).ajaxComplete((event, xhr, settings) => {
- ajaxOngoing = false;
- processAjaxQueue();
- });
-
- return function (options:any) {
- ajaxQueue.push(options);
- if (!ajaxOngoing) {
- processAjaxQueue();
- }
- };
- }());
-
// Abstract the user preference from the rest of the RB objects
// so that we can change the underlying implementation as needed
UserPreferences = object.create({
@@ -170,6 +137,4 @@ if (window.RB === null || window.RB === undefined) {
RB.Dialog = Dialog;
// @ts-expect-error TS(2304): Cannot find name 'RB'.
RB.UserPreferences = UserPreferences;
- // @ts-expect-error TS(2304): Cannot find name 'RB'.
- RB.ajax = ajax;
}(jQuery));
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/impediment.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/impediment.ts
index 648c8af2803..9393425dafb 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/impediment.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/impediment.ts
@@ -54,8 +54,10 @@ RB.Impediment = (function ($) {
let j;
let prev;
let statusID;
- let data;
+
+ let method;
let url;
+ let data;
j = this.$;
prev = this.$.prev();
@@ -72,14 +74,16 @@ RB.Impediment = (function ($) {
if (this.isNew()) {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('create_impediment', { sprint_id: RB.constants.sprint_id });
+ method = 'post';
} else {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('update_impediment', { id: this.getID(), sprint_id: RB.constants.sprint_id });
- data += '&_method=put';
+ method = 'put';
}
return {
url,
+ method,
data,
};
},
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/model.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/model.ts
index 8ba9cdf6be7..3373ffb0116 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/model.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/model.ts
@@ -26,6 +26,8 @@
// See COPYRIGHT and LICENSE files for more details.
//++
+import { FetchRequest, FetchResponse } from '@rails/request.js';
+
/***************************************
MODEL
Common methods for sprint, work_package,
@@ -42,11 +44,11 @@ RB.Model = (function ($) {
this.el = el;
},
- afterCreate(data:any, textStatus:any, xhr:any) {
+ afterCreate(data:string, response:FetchResponse) {
// Do nothing. Child objects may optionally override this
},
- afterSave(data:any, textStatus:any, xhr:any) {
+ afterSave(data:string, response:FetchResponse) {
let isNew;
let result;
@@ -59,13 +61,13 @@ RB.Model = (function ($) {
if (isNew) {
this.$.attr('id', result.$.attr('id'));
- this.afterCreate(data, textStatus, xhr);
+ this.afterCreate(data, response);
} else {
- this.afterUpdate(data, textStatus, xhr);
+ this.afterUpdate(data, response);
}
},
- afterUpdate(data:any, textStatus:any, xhr:any) {
+ afterUpdate(data:string, response:FetchResponse) {
// Do nothing. Child objects may optionally override this
},
@@ -285,11 +287,11 @@ RB.Model = (function ($) {
this.$.removeClass('editing');
},
- error(xhr:any, textStatus:any, error:any) {
+ error(responseHtml:string, error:unknown) {
this.markError();
// @ts-expect-error TS(2304): Cannot find name 'RB'.
- RB.Dialog.msg($(xhr.responseText).find('.errors').html());
- this.processError(xhr, textStatus, error);
+ RB.Dialog.msg($(responseHtml).find('.errors').html());
+ this.processError(responseHtml, error);
},
getEditor() {
@@ -375,7 +377,7 @@ RB.Model = (function ($) {
this.$.removeClass('closed');
},
- processError(x:any, t:any, e:any) {
+ processError(responseHtml:string, error:unknown) {
// Override as needed
},
@@ -411,9 +413,8 @@ RB.Model = (function ($) {
saveEdits() {
const j = this.$;
- const self = this;
- const editors = j.find('.editor');
- let saveDir;
+ const self = this;
+ const editors = j.find('.editor');
// Copy the values from the fields to the proper html elements
editors.each(function (this:any, index:any) {
@@ -440,25 +441,32 @@ RB.Model = (function ($) {
self.markIfClosed();
// Get the save directives.
- saveDir = self.saveDirectives();
+ const { method, url, data } = self.saveDirectives();
self.beforeSave();
-
self.unmarkError();
self.markSaving();
- // @ts-expect-error TS(2304): Cannot find name 'RB'.
- RB.ajax({
- type: 'POST',
- url: saveDir.url,
- data: saveDir.data,
- success(d:any, t:any, x:any) {
- self.afterSave(d, t, x);
- },
- error(x:any, t:any, e:any) {
- self.error(x, t, e);
- },
- });
- self.endEdit();
+
+ (async () => {
+ try {
+ const request = new FetchRequest(method, url, {
+ body: data,
+ contentType: 'multipart/form-data',
+ responseKind: 'html',
+ });
+ const response = await request.perform();
+ const html = await response.html;
+ if (!response.ok) {
+ self.error(html, null);
+ } else {
+ self.afterSave(html, response);
+ }
+ } catch (error) {
+ self.error('Network error', error);
+ } finally {
+ self.endEdit();
+ }
+ })();
},
unmarkError() {
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/sprint.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/sprint.ts
index cfa97609630..f77f08fdb90 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/sprint.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/sprint.ts
@@ -63,12 +63,13 @@ RB.Sprint = (function ($) {
saveDirectives() {
const wrapper = this.$;
const editor = wrapper.find('.editor');
- const data = `${editor.serialize()}&_method=put`;
+ const data = editor.serialize();
// @ts-expect-error TS(2304): Cannot find name 'RB'.
const url = RB.urlFor('update_sprint', { id: this.getID() });
return {
url,
+ method: 'put',
data,
};
},
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/story.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/story.ts
index ced6773010a..a9f945adacf 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/story.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/story.ts
@@ -26,6 +26,8 @@
// See COPYRIGHT and LICENSE files for more details.
//++
+import { FetchResponse } from '@rails/request.js';
+
/**************************************
STORY
***************************************/
@@ -49,11 +51,11 @@ RB.Story = (function ($) {
this.refreshStory();
},
- afterCreate(data:any, textStatus:any, xhr:any) {
+ afterCreate(data:string, response:FetchResponse) {
this.refreshStory();
},
- afterUpdate(data:any, textStatus:any, xhr:any) {
+ afterUpdate(data:string, response:FetchResponse) {
this.refreshStory();
},
@@ -97,7 +99,9 @@ RB.Story = (function ($) {
let url;
let prev;
let sprintId;
+
let data;
+ let method;
prev = this.$.prev();
sprintId = this.$.parents('.backlog').data('this').isSprintBacklog()
@@ -120,14 +124,16 @@ RB.Story = (function ($) {
if (this.isNew()) {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('create_story', { sprint_id: sprintId });
+ method = 'post';
} else {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('update_story', { id: this.getID(), sprint_id: sprintId });
- data += '&_method=put';
+ method = 'put';
}
return {
url,
+ method,
data,
};
},
diff --git a/frontend/src/stimulus/controllers/dynamic/backlogs/task.ts b/frontend/src/stimulus/controllers/dynamic/backlogs/task.ts
index 2c1c3112640..970e4006dc7 100644
--- a/frontend/src/stimulus/controllers/dynamic/backlogs/task.ts
+++ b/frontend/src/stimulus/controllers/dynamic/backlogs/task.ts
@@ -77,8 +77,10 @@ RB.Task = (function ($) {
saveDirectives() {
let prev;
let cellId;
- let data;
+
let url;
+ let method;
+ let data;
prev = this.$.prev();
cellId = this.$.parent('td').first().attr('id').split('_');
@@ -92,14 +94,16 @@ RB.Task = (function ($) {
if (this.isNew()) {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('create_task', { sprint_id: RB.constants.sprint_id });
+ method = 'post';
} else {
// @ts-expect-error TS(2304): Cannot find name 'RB'.
url = RB.urlFor('update_task', { id: this.getID(), sprint_id: RB.constants.sprint_id });
- data += '&_method=put';
+ method = 'put';
}
return {
url,
+ method,
data,
};
},