/
api.js
65 lines (53 loc) · 1.67 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import 'whatwg-fetch';
const HOST = 'http://ms00015080d';
const BASE = `http://${HOST}:8080/api`;
const request = (method, url) => {
url = `${BASE}${url}`;
const parsePayload = json => {
if (json && json.code === 0 && json.message === 'success') {
if (json.data === undefined) {
return {};
} else {
return json.data;
}
} else if (json) {
throw new Error({ message: json.message, code: json.code });
} else {
throw new Error(json);
}
};
const handleResponse = (resolve, reject) => {
return json => {
try {
resolve(parsePayload(json));
} catch (e) {
reject(e);
}
};
};
const createResponseError = error => {
const errText = error.statusText || '';
const errorObj = new Error(errText);
errorObj.response = error;
return errorObj;
};
return new Promise((resolve, reject) => {
fetch(url)
.then(response => {
if (response.ok) return response.json();
throw createResponseError(response);
})
.then(handleResponse(resolve, reject))
.catch(reject);
});
};
// HELPER FUNCTIONS
const get = (url, body) => request('GET', url, body);
const post = (url, body) => {
return request('POST', url, body);
};
// API CALLS
export const fetchMillsList = () => get(`/userpreferences/mill/fhenao`);
export const fetchLoadPlanDetails = (loadplanid) => get(`/LoadPlanViewer/GetLoadPlanDetails/${loadplanid}`)
export const postUserPreferences = () => post(`/userpreferences/mill/`);
export const populateSearchResults = (mode, mill, searchcondition, loadplanname) => get(`Query/SearchLoadPlans/${mode}/${mill}/${searchcondition}/${loadplanname}`)