dataBucketObservers.js 2.41 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) => {
                //toast.success("Update OK", {position: toast.POSITION.BOTTOM_CENTER});
            })
            .catch((err) => {
                toast.error("Update Error", {position: toast.POSITION.BOTTOM_CENTER});
            }
        );
    }
)

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