Skip to content

Commit 443c01a

Browse files
committed
Rework backlogs Ajax requests using Request.JS
1 parent f1e1a63 commit 443c01a

File tree

6 files changed

+58
-70
lines changed

6 files changed

+58
-70
lines changed

frontend/src/stimulus/controllers/dynamic/backlogs/common.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ if (window.RB === null || window.RB === undefined) {
3939
let Factory;
4040
let Dialog;
4141
let UserPreferences;
42-
let ajax;
4342

4443
object = {
4544
// Douglas Crockford's technique for object extension
@@ -118,38 +117,6 @@ if (window.RB === null || window.RB === undefined) {
118117
},
119118
});
120119

121-
ajax = (function () {
122-
let ajaxQueue:any;
123-
let ajaxOngoing:any;
124-
let processAjaxQueue:any;
125-
126-
ajaxQueue = [];
127-
ajaxOngoing = false;
128-
129-
processAjaxQueue = function () {
130-
const options = ajaxQueue.shift();
131-
132-
if (options !== null && options !== undefined) {
133-
ajaxOngoing = true;
134-
$.ajax(options);
135-
}
136-
};
137-
138-
// Process outstanding entries in the ajax queue whenever a ajax request
139-
// finishes.
140-
$(document).ajaxComplete((event, xhr, settings) => {
141-
ajaxOngoing = false;
142-
processAjaxQueue();
143-
});
144-
145-
return function (options:any) {
146-
ajaxQueue.push(options);
147-
if (!ajaxOngoing) {
148-
processAjaxQueue();
149-
}
150-
};
151-
}());
152-
153120
// Abstract the user preference from the rest of the RB objects
154121
// so that we can change the underlying implementation as needed
155122
UserPreferences = object.create({
@@ -170,6 +137,4 @@ if (window.RB === null || window.RB === undefined) {
170137
RB.Dialog = Dialog;
171138
// @ts-expect-error TS(2304): Cannot find name 'RB'.
172139
RB.UserPreferences = UserPreferences;
173-
// @ts-expect-error TS(2304): Cannot find name 'RB'.
174-
RB.ajax = ajax;
175140
}(jQuery));

frontend/src/stimulus/controllers/dynamic/backlogs/impediment.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ RB.Impediment = (function ($) {
5454
let j;
5555
let prev;
5656
let statusID;
57-
let data;
57+
58+
let method;
5859
let url;
60+
let data;
5961

6062
j = this.$;
6163
prev = this.$.prev();
@@ -72,14 +74,16 @@ RB.Impediment = (function ($) {
7274
if (this.isNew()) {
7375
// @ts-expect-error TS(2304): Cannot find name 'RB'.
7476
url = RB.urlFor('create_impediment', { sprint_id: RB.constants.sprint_id });
77+
method = 'post';
7578
} else {
7679
// @ts-expect-error TS(2304): Cannot find name 'RB'.
7780
url = RB.urlFor('update_impediment', { id: this.getID(), sprint_id: RB.constants.sprint_id });
78-
data += '&_method=put';
81+
method = 'put';
7982
}
8083

8184
return {
8285
url,
86+
method,
8387
data,
8488
};
8589
},

frontend/src/stimulus/controllers/dynamic/backlogs/model.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// See COPYRIGHT and LICENSE files for more details.
2727
//++
2828

29+
import { FetchRequest, FetchResponse } from '@rails/request.js';
30+
2931
/***************************************
3032
MODEL
3133
Common methods for sprint, work_package,
@@ -42,11 +44,11 @@ RB.Model = (function ($) {
4244
this.el = el;
4345
},
4446

45-
afterCreate(data:any, textStatus:any, xhr:any) {
47+
afterCreate(data:string, response:FetchResponse) {
4648
// Do nothing. Child objects may optionally override this
4749
},
4850

49-
afterSave(data:any, textStatus:any, xhr:any) {
51+
afterSave(data:string, response:FetchResponse) {
5052
let isNew;
5153
let result;
5254

@@ -59,13 +61,13 @@ RB.Model = (function ($) {
5961

6062
if (isNew) {
6163
this.$.attr('id', result.$.attr('id'));
62-
this.afterCreate(data, textStatus, xhr);
64+
this.afterCreate(data, response);
6365
} else {
64-
this.afterUpdate(data, textStatus, xhr);
66+
this.afterUpdate(data, response);
6567
}
6668
},
6769

68-
afterUpdate(data:any, textStatus:any, xhr:any) {
70+
afterUpdate(data:string, response:FetchResponse) {
6971
// Do nothing. Child objects may optionally override this
7072
},
7173

@@ -285,11 +287,11 @@ RB.Model = (function ($) {
285287
this.$.removeClass('editing');
286288
},
287289

288-
error(xhr:any, textStatus:any, error:any) {
290+
error(responseHtml:string, error:unknown) {
289291
this.markError();
290292
// @ts-expect-error TS(2304): Cannot find name 'RB'.
291-
RB.Dialog.msg($(xhr.responseText).find('.errors').html());
292-
this.processError(xhr, textStatus, error);
293+
RB.Dialog.msg($(responseHtml).find('.errors').html());
294+
this.processError(responseHtml, error);
293295
},
294296

295297
getEditor() {
@@ -375,7 +377,7 @@ RB.Model = (function ($) {
375377
this.$.removeClass('closed');
376378
},
377379

378-
processError(x:any, t:any, e:any) {
380+
processError(responseHtml:string, error:unknown) {
379381
// Override as needed
380382
},
381383

@@ -411,9 +413,8 @@ RB.Model = (function ($) {
411413

412414
saveEdits() {
413415
const j = this.$;
414-
const self = this;
415-
const editors = j.find('.editor');
416-
let saveDir;
416+
const self = this;
417+
const editors = j.find('.editor');
417418

418419
// Copy the values from the fields to the proper html elements
419420
editors.each(function (this:any, index:any) {
@@ -440,25 +441,32 @@ RB.Model = (function ($) {
440441
self.markIfClosed();
441442

442443
// Get the save directives.
443-
saveDir = self.saveDirectives();
444+
const { method, url, data } = self.saveDirectives();
444445

445446
self.beforeSave();
446-
447447
self.unmarkError();
448448
self.markSaving();
449-
// @ts-expect-error TS(2304): Cannot find name 'RB'.
450-
RB.ajax({
451-
type: 'POST',
452-
url: saveDir.url,
453-
data: saveDir.data,
454-
success(d:any, t:any, x:any) {
455-
self.afterSave(d, t, x);
456-
},
457-
error(x:any, t:any, e:any) {
458-
self.error(x, t, e);
459-
},
460-
});
461-
self.endEdit();
449+
450+
(async () => {
451+
try {
452+
const request = new FetchRequest(method, url, {
453+
body: data,
454+
contentType: 'multipart/form-data',
455+
responseKind: 'html',
456+
});
457+
const response = await request.perform();
458+
const html = await response.html;
459+
if (!response.ok) {
460+
self.error(html, null);
461+
} else {
462+
self.afterSave(html, response);
463+
}
464+
} catch (error) {
465+
self.error('Network error', error);
466+
} finally {
467+
self.endEdit();
468+
}
469+
})();
462470
},
463471

464472
unmarkError() {

frontend/src/stimulus/controllers/dynamic/backlogs/sprint.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ RB.Sprint = (function ($) {
6363
saveDirectives() {
6464
const wrapper = this.$;
6565
const editor = wrapper.find('.editor');
66-
const data = `${editor.serialize()}&_method=put`;
66+
const data = editor.serialize();
6767
// @ts-expect-error TS(2304): Cannot find name 'RB'.
6868
const url = RB.urlFor('update_sprint', { id: this.getID() });
6969

7070
return {
7171
url,
72+
method: 'put',
7273
data,
7374
};
7475
},

frontend/src/stimulus/controllers/dynamic/backlogs/story.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// See COPYRIGHT and LICENSE files for more details.
2727
//++
2828

29+
import { FetchResponse } from '@rails/request.js';
30+
2931
/**************************************
3032
STORY
3133
***************************************/
@@ -49,11 +51,11 @@ RB.Story = (function ($) {
4951
this.refreshStory();
5052
},
5153

52-
afterCreate(data:any, textStatus:any, xhr:any) {
54+
afterCreate(data:string, response:FetchResponse) {
5355
this.refreshStory();
5456
},
5557

56-
afterUpdate(data:any, textStatus:any, xhr:any) {
58+
afterUpdate(data:string, response:FetchResponse) {
5759
this.refreshStory();
5860
},
5961

@@ -97,7 +99,9 @@ RB.Story = (function ($) {
9799
let url;
98100
let prev;
99101
let sprintId;
102+
100103
let data;
104+
let method;
101105

102106
prev = this.$.prev();
103107
sprintId = this.$.parents('.backlog').data('this').isSprintBacklog()
@@ -120,14 +124,16 @@ RB.Story = (function ($) {
120124
if (this.isNew()) {
121125
// @ts-expect-error TS(2304): Cannot find name 'RB'.
122126
url = RB.urlFor('create_story', { sprint_id: sprintId });
127+
method = 'post';
123128
} else {
124129
// @ts-expect-error TS(2304): Cannot find name 'RB'.
125130
url = RB.urlFor('update_story', { id: this.getID(), sprint_id: sprintId });
126-
data += '&_method=put';
131+
method = 'put';
127132
}
128133

129134
return {
130135
url,
136+
method,
131137
data,
132138
};
133139
},

frontend/src/stimulus/controllers/dynamic/backlogs/task.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ RB.Task = (function ($) {
7777
saveDirectives() {
7878
let prev;
7979
let cellId;
80-
let data;
80+
8181
let url;
82+
let method;
83+
let data;
8284

8385
prev = this.$.prev();
8486
cellId = this.$.parent('td').first().attr('id').split('_');
@@ -92,14 +94,16 @@ RB.Task = (function ($) {
9294
if (this.isNew()) {
9395
// @ts-expect-error TS(2304): Cannot find name 'RB'.
9496
url = RB.urlFor('create_task', { sprint_id: RB.constants.sprint_id });
97+
method = 'post';
9598
} else {
9699
// @ts-expect-error TS(2304): Cannot find name 'RB'.
97100
url = RB.urlFor('update_task', { id: this.getID(), sprint_id: RB.constants.sprint_id });
98-
data += '&_method=put';
101+
method = 'put';
99102
}
100103

101104
return {
102105
url,
106+
method,
103107
data,
104108
};
105109
},

0 commit comments

Comments
 (0)