service.js 4.03 KB
// 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.request.use(function (config) {            
        //     config.withCredentials = true;
        //     console.log(config);
        //     return config;
        // });
    }

    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;