Skip to content

Commit ea3d7d8

Browse files
migrate.js: Use fetch() api instead of jquery ajax
1 parent 3121f1c commit ea3d7d8

1 file changed

Lines changed: 71 additions & 39 deletions

File tree

public/js/migrate.js

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -259,27 +259,37 @@
259259
});
260260

261261
if (containerUrls.length) {
262-
let req = $.ajax({
263-
context : this,
264-
type : 'post',
265-
url : this.icinga.config.baseUrl + '/icingadb/migrate/monitoring-url',
266-
headers : { 'Accept': 'application/json' },
267-
contentType : 'application/json',
268-
data : JSON.stringify(containerUrls)
269-
});
262+
fetch(
263+
this.icinga.config.baseUrl + '/icingadb/migrate/monitoring-url',
264+
{
265+
method : 'POST',
266+
headers :
267+
{
268+
'Accept': 'application/json',
269+
'Content-Type' : 'application/json',
270+
'X-Requested-With': 'XMLHttpRequest'
271+
},
272+
body : JSON.stringify(containerUrls),
273+
}
274+
)
275+
.then(response => {
276+
if (! response.ok) {
277+
throw new Error('Network response was not OK');
278+
}
270279

271-
req.urls = urls;
272-
req.urlIndexToContainerId = containerIds;
273-
req.done(this.processUrlMigrationResults);
274-
req.always(() => this.changeUrlMigrationReadyState(true));
280+
return response.json();
281+
})
282+
.then(data => this.processUrlMigrationResults(data, urls, containerIds))
283+
.catch(error => console.error('Request failed:', error))
284+
.finally(() => this.changeBackendSupportReadyState(true));
275285
} else {
276286
// All urls have already been migrated once, show popup immediately
277287
this.addSuggestions(urls);
278288
this.changeUrlMigrationReadyState(true);
279289
}
280290
}
281291

282-
processUrlMigrationResults(data, textStatus, req) {
292+
processUrlMigrationResults(data, urls, urlIndexToContainerId) {
283293
let _this = this;
284294
let result, containerId;
285295

@@ -295,11 +305,11 @@
295305
}
296306

297307
result.forEach((migratedUrl, i) => {
298-
containerId = req.urlIndexToContainerId[i];
299-
this.knownMigrations[req.urls[containerId]] = migratedUrl;
308+
containerId = urlIndexToContainerId[i];
309+
this.knownMigrations[urls[containerId]] = migratedUrl;
300310
});
301311

302-
this.addSuggestions(req.urls);
312+
this.addSuggestions(urls);
303313
}
304314

305315
prepareBackendCheckboxForm(modules) {
@@ -315,35 +325,45 @@
315325
});
316326

317327
if (moduleNames.length) {
318-
let req = $.ajax({
319-
context : this,
320-
type : 'post',
321-
url : this.icinga.config.baseUrl + '/icingadb/migrate/backend-support',
322-
headers : { 'Accept': 'application/json' },
323-
contentType : 'application/json',
324-
data : JSON.stringify(moduleNames)
325-
});
328+
fetch(
329+
this.icinga.config.baseUrl + '/icingadb/migrate/backend-support',
330+
{
331+
method : 'POST',
332+
headers :
333+
{
334+
'Accept': 'application/json',
335+
'Content-Type' : 'application/json' ,
336+
'X-Requested-With': 'XMLHttpRequest'
337+
},
338+
body : JSON.stringify(moduleNames),
339+
}
340+
)
341+
.then(response => {
342+
if (! response.ok) {
343+
throw new Error('Network response was not OK');
344+
}
326345

327-
req.modules = modules;
328-
req.moduleIndexToContainerId = containerIds;
329-
req.done(this.processBackendSupportResults);
330-
req.always(() => this.changeBackendSupportReadyState(true));
346+
return response.json();
347+
})
348+
.then(data => this.processBackendSupportResults(data, modules, containerIds))
349+
.catch(error => console.error('Request failed:', error))
350+
.finally(() => this.changeBackendSupportReadyState(true));
331351
} else {
332352
// All modules have already been checked once, show popup immediately
333353
this.setupBackendCheckboxForm(modules);
334354
this.changeBackendSupportReadyState(true);
335355
}
336356
}
337357

338-
processBackendSupportResults(data, textStatus, req) {
358+
processBackendSupportResults(data, modules, moduleIndexToContainerId) {
339359
let result = data.data;
340360

341361
result.forEach((state, i) => {
342-
let containerId = req.moduleIndexToContainerId[i];
343-
this.knownBackendSupport[req.modules[containerId]] = state;
362+
let containerId = moduleIndexToContainerId[i];
363+
this.knownBackendSupport[modules[containerId]] = state;
344364
});
345365

346-
this.setupBackendCheckboxForm(req.modules);
366+
this.setupBackendCheckboxForm(modules);
347367
}
348368

349369
setupBackendCheckboxForm(modules) {
@@ -359,17 +379,29 @@
359379
if (Object.keys(supportedModules).length) {
360380
this.backendSupportRelated = { ...this.backendSupportRelated, ...supportedModules };
361381

362-
let req = $.ajax({
363-
context : this,
364-
type : 'get',
365-
url : this.icinga.config.baseUrl + '/icingadb/migrate/checkbox-state?showCompact'
366-
});
382+
fetch(
383+
this.icinga.config.baseUrl + '/icingadb/migrate/checkbox-state?showCompact',
384+
{
385+
headers :
386+
{
387+
'X-Requested-With': 'XMLHttpRequest'
388+
}
389+
}
390+
)
391+
.then(response => {
392+
if (! response.ok) {
393+
throw new Error('Network response was not OK');
394+
}
367395

368-
req.done(this.setCheckboxState);
396+
return response.text();
397+
})
398+
.then(html => this.setCheckboxState(html))
399+
.catch(error => console.error('Request failed:', error))
369400
}
370401
}
371402

372-
setCheckboxState(html, textStatus, req) {
403+
setCheckboxState(html) {
404+
console.log(html);
373405
let form = this.Popup().querySelector('.suggestion-area > #setAsBackendForm');
374406
if (! form) {
375407
form = notjQuery.render(html);

0 commit comments

Comments
 (0)