service.js
5.99 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Import Axios HTTP Library
import axios from 'axios';
import createHistory from 'history/createBrowserHistory'
const history = createHistory();
const baseURLName = "http://localhost:3001";
// Public class exported globally
class LemaApi {
constructor() {
//this.productOptionsUrl = "profilation/GetProductOptions";
//this.jobsOptionsUrl = "profilation/GetJobsOptions";
this.webClientConfigDataUrl = "profilation/GetWebClientConfigData";
this.activationFileDataUrl = "activation/SaveSessionFile";
this.activationDataUrl = "activation/SaveActivationData";
this.subscriptionSessionUrl = "profilation/GetSubscriptionSession";
this.profilationDataUrl = "profilation/SaveProfilationData";
this.getProfilationDataUrl = "profilation/GetProfilationData";
this.saveContactData = "contact/SaveContactData";
// Axios singleton instance
this._axios = axios.create({
withCredentials: true,
baseURL: baseURLName + '/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) {
if (error.response.status === 404){
window.location.href = baseURLName + "/Home/PageNotFound";
}
else if (error.response.status === 500){
window.location.href = baseURLName + "/Home/Error";
}
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 contatti fornitura
saveFurnitureContactData(request) {
// Debug
console.log("SAVE CONTACTS DEBUG:");
console.log(request);
return this._axios.post(this.saveContactData, request)
.then(response => {
console.log("Contacts saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
// Post dei files di attivazione al server
saveActivationFiles(request, fileKind) {
// Debug
console.log("SAVE ACTIVATION FILES DEBUG:");
console.log(request);
return this._axios.post(this.activationFileDataUrl + "/" + fileKind, request)
.then(response => {
console.log("Activation files saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
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);
}
);
}
getProfilationData() {
// Debug
console.log("GET PROFILATION DATA DEBUG:");
return this._axios.get(this.getProfilationDataUrl)
.then(response => {
console.log("Profilation data retrieved successfully");
// ritorno una promise risolta contenente i dati di profilazione
return Promise.resolve(response.data);
})
.catch(err => {
console.error(err);
}
);
}
}
// Export singleton Service Class
export default LemaApi;