From 641f1d2c4b210026fd40c4c3d3acc1bd41dc9adc Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Wed, 31 Jan 2018 10:14:55 +0000 Subject: Converted common_utils to axios --- app/assets/javascripts/lib/utils/common_utils.js | 16 +++++++--------- app/assets/javascripts/notes.js | 12 +++++++----- spec/javascripts/lib/utils/common_utils_spec.js | 8 +++++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js index 03918762842..0fcf8b410af 100644 --- a/app/assets/javascripts/lib/utils/common_utils.js +++ b/app/assets/javascripts/lib/utils/common_utils.js @@ -1,3 +1,4 @@ +import axios from './axios_utils'; import { getLocationHash } from './url_utility'; export const getPagePath = (index = 0) => $('body').attr('data-page').split(':')[index]; @@ -27,17 +28,14 @@ export const isInIssuePage = () => { return page === 'issues' && action === 'show'; }; -export const ajaxGet = url => $.ajax({ - type: 'GET', - url, - dataType: 'script', +export const ajaxGet = url => axios.get(url, { + params: { format: 'js' }, + responseType: 'text', +}).then(({ data }) => { + $.globalEval(data); }); -export const ajaxPost = (url, data) => $.ajax({ - type: 'POST', - url, - data, -}); +export const ajaxPost = (url, data) => axios.post(url, data); export const rstrip = (val) => { if (val) { diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js index a2b8e6f6495..2f37d40ebad 100644 --- a/app/assets/javascripts/notes.js +++ b/app/assets/javascripts/notes.js @@ -1487,7 +1487,9 @@ export default class Notes { /* eslint-disable promise/catch-or-return */ // Make request to submit comment on server ajaxPost(formAction, formData) - .then((note) => { + .then((res) => { + const note = res.data; + // Submission successful! remove placeholder $notesContainer.find(`#${noteUniqueId}`).remove(); @@ -1560,7 +1562,7 @@ export default class Notes { } $form.trigger('ajax:success', [note]); - }).fail(() => { + }).catch(() => { // Submission failed, remove placeholder note and show Flash error message $notesContainer.find(`#${noteUniqueId}`).remove(); @@ -1631,11 +1633,11 @@ export default class Notes { /* eslint-disable promise/catch-or-return */ // Make request to update comment on server ajaxPost(formAction, formData) - .then((note) => { + .then(({ data }) => { // Submission successful! render final note element - this.updateNote(note, $editingNote); + this.updateNote(data, $editingNote); }) - .fail(() => { + .catch(() => { // Submission failed, revert back to original note $noteBodyText.html(_.escape(cachedNoteBodyText)); $editingNote.removeClass('being-posted fade-in'); diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js index 0a9d815f469..bb71c181b3d 100644 --- a/spec/javascripts/lib/utils/common_utils_spec.js +++ b/spec/javascripts/lib/utils/common_utils_spec.js @@ -1,5 +1,5 @@ /* eslint-disable promise/catch-or-return */ - +import axios from '~/lib/utils/axios_utils'; import * as commonUtils from '~/lib/utils/common_utils'; describe('common_utils', () => { @@ -451,10 +451,12 @@ describe('common_utils', () => { it('should perform `$.ajax` call and do `POST` request', () => { const requestURL = '/some/random/api'; const data = { keyname: 'value' }; - const ajaxSpy = spyOn($, 'ajax').and.callFake(() => {}); + const ajaxSpy = spyOn(axios, 'post').and.callFake(() => {}); commonUtils.ajaxPost(requestURL, data); - expect(ajaxSpy.calls.allArgs()[0][0].type).toEqual('POST'); + + expect(ajaxSpy.calls.allArgs()[0][0]).toEqual(requestURL); + expect(ajaxSpy.calls.allArgs()[0][1]).toEqual(data); }); }); -- cgit v1.2.1