|
| 1 | +import apiConfig from 'constants/api'; |
| 2 | +import { getToken, getUserID } from "service/storage"; |
| 3 | + |
| 4 | +class AdminAPI { |
| 5 | + static processResponse(response, handleResponse) { |
| 6 | + console.log('=== response: ', response); |
| 7 | + if(response.error) { |
| 8 | + console.log('error: ', response.error); |
| 9 | + handleResponse(response.error, true); |
| 10 | + } |
| 11 | + else { |
| 12 | + if (response){ |
| 13 | + console.log('success: ', response); |
| 14 | + handleResponse(response, false); |
| 15 | + } else { |
| 16 | + console.log('error: ', response); |
| 17 | + handleResponse(response.error, true); |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + static processRequest(url, method, data, handleResponse) { |
| 23 | + console.log('==== processRequest: ', url, data); |
| 24 | + let params = { |
| 25 | + method: method, |
| 26 | + headers: { |
| 27 | + "Content-Type": "application/json" |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + if (data) { |
| 32 | + params = { |
| 33 | + ...params, |
| 34 | + body: JSON.stringify(data) |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + fetch(`${apiConfig.url}/${url}`, params) |
| 39 | + .then(response => response.json()) |
| 40 | + .then(response => { |
| 41 | + this.processResponse(response, handleResponse); |
| 42 | + }) |
| 43 | + .catch(error => { |
| 44 | + console.log('error: ', error); |
| 45 | + handleResponse(error, true); |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + static processRequestWithToken(url, method, data, handleResponse) { |
| 50 | + console.log('==== processRequest: ', url, data); |
| 51 | + let parameters = { |
| 52 | + method: method, |
| 53 | + headers: { |
| 54 | + 'Accept': 'application/json', |
| 55 | + "Content-Type": "application/json", |
| 56 | + "Authorization": `Bearer ${getToken()}` |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + if (method !== 'get' && data !== '' && data !== null) { |
| 61 | + parameters = {...parameters, body: JSON.stringify(data)}; |
| 62 | + } |
| 63 | + |
| 64 | + console.log('==== parameters: ', parameters); |
| 65 | + console.log('==== url: ', `${apiConfig.url}/${url}/`); |
| 66 | + |
| 67 | + fetch(`${apiConfig.url}/${url}/`, parameters) |
| 68 | + .then(response => {console.log('=== response: ', response); return response.json()}) |
| 69 | + .then(response => { |
| 70 | + this.processResponse(response, handleResponse); |
| 71 | + }) |
| 72 | + .catch(error => { |
| 73 | + console.log('error: ', error); |
| 74 | + handleResponse(error, true); |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + static getProfile(profileId, handleResponse) { |
| 79 | + this.processRequestWithToken(`talent/${profileId}`, 'get', null, handleResponse); |
| 80 | + } |
| 81 | + |
| 82 | +} |
| 83 | +export default AdminAPI |
0 commit comments