dataBucketObservers.js 3.67 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);
            }
        );                     
    }
)