Skip to content

Instantly share code, notes, and snippets.

@guilhermemmb
Last active October 7, 2015 17:44
Show Gist options
  • Select an option

  • Save guilhermemmb/01900da334601fa7411d to your computer and use it in GitHub Desktop.

Select an option

Save guilhermemmb/01900da334601fa7411d to your computer and use it in GitHub Desktop.
import flux from '../flux';
import Fetcher from 'utils/Fetcher';
import AuthenticationHelper from 'utils/AuthenticationHelper';
class VtexActions {
checkLogin() {
Fetcher.checkVtexIdAuth().then((userData) => {
Fetcher.getProfileSystemData('omniera', userData.user).then((response) => {
const data = {
user: userData,
store: response
};
this.actions.VtexIdAuthSuccess(data);
}, (err) => {
console.log('error on get user profile data', err);
AuthenticationHelper.login(this.actions.checkLogin);
});
}, (err) => {
console.log('error on check user auth', err);
AuthenticationHelper.login(this.actions.checkLogin);
});
}
VtexIdAuthSuccess(data){
data.logged = true;
this.dispatch(data);
}
VtexIdAuthFail(error){
this.dispatch(error);
}
getStoreInfo(storeId) {
let promise = new Promise((resolve, reject) => {
if(!storeId) {
const reason = 'storeId is undefined. We\'ll use default trade policy';
this.actions.GetStoreInfoFail({message: reason});
reject(reason);
}
Fetcher.getStoreData('omniera', storeId).then((response) => {
this.actions.GetStoreInfoSuccess(response);
resolve(response);
}).catch((err) => {
const reason = 'Fail on get trade policy. We\'ll use default trade policy';
this.actions.GetStoreInfoFail({message: reason});
reject(reason);
});
});
return promise;
}
GetStoreInfoSuccess(data){
this.dispatch(data);
}
GetStoreInfoFail(error){
console.log(error.message);
this.dispatch(error);
}
}
export default flux.createActions(VtexActions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment