service.js
4.37 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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";
this.activationDataUrl = "profilation/SaveActivationData";
this.subscriptionSessionUrl = "profilation/GetSubscriptionSession";
this.profilationDataUrl = "profilation/SaveProfilationData";
// Axios singleton instance
this._axios = axios.create({
withCredentials: true,
baseURL: 'http://localhost:3001/api/',
timeout: 60000,
headers: {
'Content-Type': 'application/json' // i dati sono nel body della request in formato Json
}
});
// Interceptor
this._axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
console.error("ERROR FROM INTERCEPTOR");
if (error.response.status === 404){
//history.push('/public/src/404.html');
}
else if (error.response.status === 500){
//history.push('/public/src/404.html');
}
return Promise.reject(error.response.data.message);
});
}
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
};
getSubscriptionSession() {
return this._axios.get(this.subscriptionSessionUrl)
.then(response => {
console.log("Session started successfully");
})
.catch(err => {
console.error(err);
}
);
}
// Post dei dati di attivazione al server
saveActivationData(request) {
// Debug
console.log("SAVE ACTIVATION DATA DEBUG:");
console.log(request);
return this._axios.post(this.activationDataUrl, request)
.then(response => {
console.log("Activation data saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
saveProfilationData(request) {
// Debug
console.log("SAVE PROFILATION DATA DEBUG:");
console.log(request);
return this._axios.post(this.profilationDataUrl, request)
.then(response => {
console.log("Profilation data saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
}
/*
// 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;