diff --git a/app/javascript/dashboard/api/ApiClient.js b/app/javascript/dashboard/api/ApiClient.js new file mode 100644 index 000000000..0bbd13d9c --- /dev/null +++ b/app/javascript/dashboard/api/ApiClient.js @@ -0,0 +1,31 @@ +/* global axios */ + +const API_VERSION = `/api/v1`; + +class ApiClient { + constructor(url) { + this.url = `${API_VERSION}/${url}`; + } + + get() { + return axios.get(this.url); + } + + show(id) { + return axios.get(`${this.url}/${id}`); + } + + create(data) { + return axios.post(this.url, data); + } + + update(id, data) { + return axios.patch(`${this.url}/${id}`, data); + } + + delete(id) { + return axios.delete(`${this.url}/${id}`); + } +} + +export default ApiClient; diff --git a/app/javascript/dashboard/api/cannedResponse.js b/app/javascript/dashboard/api/cannedResponse.js index 6153c4922..3a17d2735 100644 --- a/app/javascript/dashboard/api/cannedResponse.js +++ b/app/javascript/dashboard/api/cannedResponse.js @@ -1,112 +1,16 @@ -/* eslint no-console: 0 */ /* global axios */ -/* eslint no-undef: "error" */ -/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true }] */ -import endPoints from './endPoints'; -export default { - getAllCannedResponses() { - const urlData = endPoints('cannedResponse').get(); - const fetchPromise = new Promise((resolve, reject) => { - axios - .get(urlData.url) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, +import ApiClient from './ApiClient'; - searchCannedResponse({ searchKey }) { - let urlData = endPoints('cannedResponse').get(); - urlData = `${urlData.url}?search=${searchKey}`; - const fetchPromise = new Promise((resolve, reject) => { - axios - .get(urlData) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, +class CannedResponse extends ApiClient { + constructor() { + super('canned_responses'); + } - addCannedResponse(cannedResponseObj) { - const urlData = endPoints('cannedResponse').post(); - const fetchPromise = new Promise((resolve, reject) => { - axios - .post(urlData.url, cannedResponseObj) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, - editCannedResponse(cannedResponseObj) { - const urlData = endPoints('cannedResponse').put(cannedResponseObj.id); - const fetchPromise = new Promise((resolve, reject) => { - axios - .put(urlData.url, cannedResponseObj) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, - deleteCannedResponse(responseId) { - const urlData = endPoints('cannedResponse').delete(responseId); - const fetchPromise = new Promise((resolve, reject) => { - axios - .delete(urlData.url) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, - getLabels() { - const urlData = endPoints('fetchLabels'); - const fetchPromise = new Promise((resolve, reject) => { - axios - .get(urlData.url) - .then(response => { - resolve(response); - }) - .catch(error => { - reject(Error(error)); - }); - }); - return fetchPromise; - }, - // Get Inbox related to the account - getInboxes() { - const urlData = endPoints('fetchInboxes'); - const fetchPromise = new Promise((resolve, reject) => { - axios - .get(urlData.url) - .then(response => { - console.log('fetch inboxes success'); - resolve(response); - }) - .catch(error => { - console.log('fetch inboxes failure'); - reject(Error(error)); - }); - }); - return fetchPromise; - }, -}; + get({ searchKey }) { + const url = searchKey ? `${this.url}?search=${searchKey}` : this.url; + return axios.get(url); + } +} + +export default new CannedResponse(); diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue index fca785e51..38497daf1 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue @@ -1,49 +1,71 @@