service.js
2.14 KB
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
64
65
66
67
68
69
// Import Axios HTTP Library
import axios from 'axios';
// Public class exported globally
class LemaApi {
constructor() {
this.productOptionsUrl = "profilation/GetProductOptions";
this.jobsOptionsUrl = "profilation/GetJobsOptions";
this.webClientConfigDataUrl = "profilation/GetWebClientConfigData";
// Axios singleton instance
this._axios = axios.create({
baseURL: 'http://localhost:3001/api/',
timeout: 30000,
headers: {
'X-Custom-Header': 'foobar',
'Content-Type': 'application/json' // default
}
});
}
loadWebClientConfigData() {
return this._axios.get(this.webClientConfigDataUrl)
.then(response => {
// Salvo i dati nel local storage
localStorage.setItem("CONFIG_DATA", JSON.stringify(response.data));
// ritorno una promise risolta
return Promise.resolve('Data loaded successfully');
}
);
}
// Lista dei prodotti
static getProductOptions() {
console.log("Property ProductOptions called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.productOptions;
};
// Lista dei settori merceologici
static getJobOptions() {
console.log("Property JobsOptions called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.jobsOptions
};
}
/*
// Private module functions
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
const error = new Error(`HTTP Error ${response.statusText}`);
error.status = response.statusText;
error.response = response;
console.log(error);
throw error;
}
}
function parseJSON(response) {
return response.json();
}
*/
// Export singleton Service Class
export default LemaApi;