Commit 6a89ce29 authored by Phil Hughes's avatar Phil Hughes

Converted create_merge_request_dropdown to axios

parent 797f29d1
/* eslint-disable no-new */ /* eslint-disable no-new */
import _ from 'underscore'; import _ from 'underscore';
import axios from './lib/utils/axios_utils';
import Flash from './flash'; import Flash from './flash';
import DropLab from './droplab/drop_lab'; import DropLab from './droplab/drop_lab';
import ISetter from './droplab/plugins/input_setter'; import ISetter from './droplab/plugins/input_setter';
...@@ -74,60 +75,52 @@ export default class CreateMergeRequestDropdown { ...@@ -74,60 +75,52 @@ export default class CreateMergeRequestDropdown {
} }
checkAbilityToCreateBranch() { checkAbilityToCreateBranch() {
return $.ajax({ this.setUnavailableButtonState();
type: 'GET',
dataType: 'json', axios.get(this.canCreatePath)
url: this.canCreatePath, .then(({ data }) => {
beforeSend: () => this.setUnavailableButtonState(), this.setUnavailableButtonState(false);
})
.done((data) => { if (data.can_create_branch) {
this.setUnavailableButtonState(false); this.available();
this.enable();
if (data.can_create_branch) {
this.available(); if (!this.droplabInitialized) {
this.enable(); this.droplabInitialized = true;
this.initDroplab();
if (!this.droplabInitialized) { this.bindEvents();
this.droplabInitialized = true; }
this.initDroplab(); } else if (data.has_related_branch) {
this.bindEvents(); this.hide();
} }
} else if (data.has_related_branch) { })
this.hide(); .catch(() => {
} this.unavailable();
}).fail(() => { this.disable();
this.unavailable(); Flash('Failed to check if a new branch can be created.');
this.disable(); });
new Flash('Failed to check if a new branch can be created.');
});
} }
createBranch() { createBranch() {
return $.ajax({ this.isCreatingBranch = true;
method: 'POST',
dataType: 'json', return axios.post(this.createBranchPath)
url: this.createBranchPath, .then(({ data }) => {
beforeSend: () => (this.isCreatingBranch = true), this.branchCreated = true;
}) window.location.href = data.url;
.done((data) => { })
this.branchCreated = true; .catch(() => Flash('Failed to create a branch for this issue. Please try again.'));
window.location.href = data.url;
})
.fail(() => new Flash('Failed to create a branch for this issue. Please try again.'));
} }
createMergeRequest() { createMergeRequest() {
return $.ajax({ this.isCreatingMergeRequest = true;
method: 'POST',
dataType: 'json', return axios.post(this.createMrPath)
url: this.createMrPath, .then(({ data }) => {
beforeSend: () => (this.isCreatingMergeRequest = true), this.mergeRequestCreated = true;
}) window.location.href = data.url;
.done((data) => { })
this.mergeRequestCreated = true; .catch(() => Flash('Failed to create Merge Request. Please try again.'));
window.location.href = data.url;
})
.fail(() => new Flash('Failed to create Merge Request. Please try again.'));
} }
disable() { disable() {
...@@ -200,39 +193,33 @@ export default class CreateMergeRequestDropdown { ...@@ -200,39 +193,33 @@ export default class CreateMergeRequestDropdown {
getRef(ref, target = 'all') { getRef(ref, target = 'all') {
if (!ref) return false; if (!ref) return false;
return $.ajax({ return axios.get(this.refsPath + ref)
method: 'GET', .then(({ data }) => {
dataType: 'json', const branches = data[Object.keys(data)[0]];
url: this.refsPath + ref, const tags = data[Object.keys(data)[1]];
beforeSend: () => { let result;
this.isGettingRef = true;
}, if (target === 'branch') {
}) result = CreateMergeRequestDropdown.findByValue(branches, ref);
.always(() => { } else {
this.isGettingRef = false; result = CreateMergeRequestDropdown.findByValue(branches, ref, true) ||
}) CreateMergeRequestDropdown.findByValue(tags, ref, true);
.done((data) => { this.suggestedRef = result;
const branches = data[Object.keys(data)[0]]; }
const tags = data[Object.keys(data)[1]];
let result;
if (target === 'branch') { this.isGettingRef = false;
result = CreateMergeRequestDropdown.findByValue(branches, ref);
} else {
result = CreateMergeRequestDropdown.findByValue(branches, ref, true) ||
CreateMergeRequestDropdown.findByValue(tags, ref, true);
this.suggestedRef = result;
}
return this.updateInputState(target, ref, result); return this.updateInputState(target, ref, result);
}) })
.fail(() => { .catch(() => {
this.unavailable(); this.unavailable();
this.disable(); this.disable();
new Flash('Failed to get ref.'); new Flash('Failed to get ref.');
return false; this.isGettingRef = false;
});
return false;
});
} }
getTargetData(target) { getTargetData(target) {
...@@ -332,12 +319,12 @@ export default class CreateMergeRequestDropdown { ...@@ -332,12 +319,12 @@ export default class CreateMergeRequestDropdown {
xhr = this.createBranch(); xhr = this.createBranch();
} }
xhr.fail(() => { xhr.catch(() => {
this.isCreatingMergeRequest = false; this.isCreatingMergeRequest = false;
this.isCreatingBranch = false; this.isCreatingBranch = false;
});
xhr.always(() => this.enable()); this.enable();
});
this.disable(); this.disable();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment