forked from chatwoot/chatwoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] Add ApiClient, refactor CannedResponse (chatwoot#183)
- Loading branch information
1 parent
50fc066
commit 6c60b60
Showing
10 changed files
with
309 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.