Converted create_merge_request_dropdown to axios

This commit is contained in:
Phil Hughes 2018-01-26 10:27:50 +00:00
parent 797f29d184
commit 6a89ce2953
No known key found for this signature in database
GPG key ID: 32245528C52E0F9F

View file

@ -1,5 +1,6 @@
/* eslint-disable no-new */
import _ from 'underscore';
import axios from './lib/utils/axios_utils';
import Flash from './flash';
import DropLab from './droplab/drop_lab';
import ISetter from './droplab/plugins/input_setter';
@ -74,13 +75,10 @@ export default class CreateMergeRequestDropdown {
}
checkAbilityToCreateBranch() {
return $.ajax({
type: 'GET',
dataType: 'json',
url: this.canCreatePath,
beforeSend: () => this.setUnavailableButtonState(),
})
.done((data) => {
this.setUnavailableButtonState();
axios.get(this.canCreatePath)
.then(({ data }) => {
this.setUnavailableButtonState(false);
if (data.can_create_branch) {
@ -95,39 +93,34 @@ export default class CreateMergeRequestDropdown {
} else if (data.has_related_branch) {
this.hide();
}
}).fail(() => {
})
.catch(() => {
this.unavailable();
this.disable();
new Flash('Failed to check if a new branch can be created.');
Flash('Failed to check if a new branch can be created.');
});
}
createBranch() {
return $.ajax({
method: 'POST',
dataType: 'json',
url: this.createBranchPath,
beforeSend: () => (this.isCreatingBranch = true),
})
.done((data) => {
this.isCreatingBranch = true;
return axios.post(this.createBranchPath)
.then(({ data }) => {
this.branchCreated = true;
window.location.href = data.url;
})
.fail(() => new Flash('Failed to create a branch for this issue. Please try again.'));
.catch(() => Flash('Failed to create a branch for this issue. Please try again.'));
}
createMergeRequest() {
return $.ajax({
method: 'POST',
dataType: 'json',
url: this.createMrPath,
beforeSend: () => (this.isCreatingMergeRequest = true),
})
.done((data) => {
this.isCreatingMergeRequest = true;
return axios.post(this.createMrPath)
.then(({ data }) => {
this.mergeRequestCreated = true;
window.location.href = data.url;
})
.fail(() => new Flash('Failed to create Merge Request. Please try again.'));
.catch(() => Flash('Failed to create Merge Request. Please try again.'));
}
disable() {
@ -200,18 +193,8 @@ export default class CreateMergeRequestDropdown {
getRef(ref, target = 'all') {
if (!ref) return false;
return $.ajax({
method: 'GET',
dataType: 'json',
url: this.refsPath + ref,
beforeSend: () => {
this.isGettingRef = true;
},
})
.always(() => {
this.isGettingRef = false;
})
.done((data) => {
return axios.get(this.refsPath + ref)
.then(({ data }) => {
const branches = data[Object.keys(data)[0]];
const tags = data[Object.keys(data)[1]];
let result;
@ -224,13 +207,17 @@ export default class CreateMergeRequestDropdown {
this.suggestedRef = result;
}
this.isGettingRef = false;
return this.updateInputState(target, ref, result);
})
.fail(() => {
.catch(() => {
this.unavailable();
this.disable();
new Flash('Failed to get ref.');
this.isGettingRef = false;
return false;
});
}
@ -332,12 +319,12 @@ export default class CreateMergeRequestDropdown {
xhr = this.createBranch();
}
xhr.fail(() => {
xhr.catch(() => {
this.isCreatingMergeRequest = false;
this.isCreatingBranch = false;
});
xhr.always(() => this.enable());
this.enable();
});
this.disable();
}