dataBucketObservers.js 6.08 KB
import LemaApi from "./service.js";
import { observer } from 'redux-observers'
import {toast} from 'react-toastify';


// Observer sulla proprietà dataBucketProfilation dello store  
export const dataBucketsProfilationObserver = observer(
    state => state.ProfilationSelectReducer.dataBucketProfilation,

    (dispatch, current, previous) => {        

        // current is undefined on first call
        if(!current) return;

        console.log('Databucket profilazione aggiornato');
        console.log('Previous', previous);
        console.log('Current', current);       
                
        let request = {
            Job: current.job || "",
            Service: current.service || "",
            Expend: current.expend || "",
            Period: current.period || "",
            Light: current.light || 0,
            Gas: current.gas || 0,
            Environment: current.environment || "",
            Flexibility: current.flexibility || "",
            Customizable: current.customizable || ""
          };
      
          const lemaApi = new LemaApi();
          lemaApi.saveProfilationData(request)
            .then((response) => {
                console.log("dataBucket profilation Update successfully");
            })
            .catch((err) => {
                console.error(err);                
            }
        );
    }
)

// Observer sulla proprietà dataBucketPrivacy dello store  
export const dataBucketPrivacyObserver = observer(
    state => state.ProfilationSelectReducer.dataBucketPrivacy,

    (dispatch, current, previous) => {        

        // current is undefined on first call
        if(!current) return;

        console.log('Databucket privacy aggiornato');
        console.log('Previous', previous);
        console.log('Current', current);       
        
        let request = {
            contractAccepted: current.ContractAccepted || false,
            activationAdAccepted: current.ActivationAdAccepted || false,
            commercialAccepted: current.CommercialAccepted || false
          };
              
          const lemaApi = new LemaApi();
          lemaApi.savePrivacyInfo(request)            
            .then(response => {
                console.log("Privacy Info saved successfully");
            })
            .catch(err => {
                console.error(err);
            }
        );                    
    }
)

// Observer sulla proprietà dataBucketContact dello store  
export const dataBucketContactObserver = observer(
    state => state.ProfilationSelectReducer.dataBucketContact,

    (dispatch, current, previous) => {        

        // current is undefined on first call
        if(!current) return;

        console.log('Databucket contact aggiornato');
        console.log('Previous', previous);
        console.log('Current', current);       
        
        let email = 
        (/^[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/.test(current.Email)) ?
            current.Email : "";

        let phone =  (/^\+?\d+$/.test(current.PhoneNumber)) ?
            current.PhoneNumber : "";
    
        let request = {
            Description: current.Description || "",
            Email: email || "",
            PhoneNumber: phone || "",
            PrivacyAck: current.PrivacyAck || false,
            AllowContact: current.AllowContact || false,
            ContactReason: JSON.parse(localStorage.getItem("ContactsModel"))
        };
            
        const lemaApi = new LemaApi();
        lemaApi.saveContactData(request)
            .then(response => {
                console.log("Contact saved successfully");
            })
            .catch(err => {
                console.error(err);
            }
        );                     
    }
)


// Observer sulla proprietà dataBucketActivation dello store  
export const dataBucketActivationObserver = observer(
    state => state.ProfilationSelectReducer.dataBucketActivation,

    (dispatch, current, previous) => {        

        // current is undefined on first call
        if(!current) return;

        console.log('Databucket activation aggiornato');
        console.log('Previous', previous);
        console.log('Current', current);       
                
        let request = {			
			Iva: current.Iva || "",      
			Light: current.Light || "",
			Gas: current.Gas || "",
			MobilePhone: current.MobilePhone || "",
			EmailPec: current.EmailPec || "",
			Email: current.Email || "",	
			IvaService: current.IvaService || "",
			Iban: current.Iban || "",
			IsSottoscrittoreSepa: current.IsSottoscrittoreSepa || false,
			SepaNome: current.SepaNome || "",
            SepaCognome: current.SepaCognome || "",
            SepaCodiceFiscale: current.SepaCodiceFiscale || "",
            RegimeIva: current.RegimeIva || "",            
			TipologiaAttivita: current.TipologiaAttivita || "",
			CodiceAteco: current.CodiceAteco || "",	
			IsReferente: current.IsReferente || false,
			ReferenteNome: current.ReferenteNome || "",
			ReferenteCognome: current.ReferenteCognome || "",
			ReferenteEmail: current.ReferenteEmail || "",
			ReferenteMobilePhone: current.ReferenteMobilePhone || "",
			Accise: current.Accise || "",
            VolumiAccise: current.VolumiAccise || "",
            ConfermaAcciseRidotte: current.ConfermaAcciseRidotte || false,
			ConfermaAttoNotorieta: current.ConfermaAttoNotorieta || false,
			ConfermaCondizioniGas: current.ConfermaCondizioniGas || false,
			ConfermaIvaAgevolata: current.ConfermaIvaAgevolata || false,
			ConfermaCondizioniElettriche: current.ConfermaCondizioniElettriche || false,
			ConfermaCondizioniElettriche2: current.ConfermaCondizioniElettriche2 || false,
            ConfermaCondizioniElettriche3: current.ConfermaCondizioniElettriche3 || false,
            FormulaId: current.FormulaId || 0,
            FormulaCodice: current.FormulaCodice || ""
        };
        
        const lemaApi = new LemaApi();
        lemaApi.saveActivationData(request)
            .then(response => {
                console.log("Databucket activation saved successfully");
            })
            .catch(err => {
                console.error(err);
            }
        );                         
    }
)