service.js 5.38 KB
// 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";         

        // 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 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;