GithubHelp home page GithubHelp logo

Comments (12)

xmlking avatar xmlking commented on July 17, 2024

workaround for my use case: wrap the action function with fromPromise

import {Store, Action, Mutation} from 'ngxs';
import {OAuthService} from "angular-oauth2-oidc";
import {Login, LogoutSuccess, LoginSuccess, Logout} from "./auth.events";
import {fromPromise} from "rxjs/observable/fromPromise";

@Store({
  defaults: {
    isLoggedIn: false,
    profile: {}
  }
})
export class AuthStore {
  constructor(private oauthService: OAuthService) {
  }

  @Mutation(LoginSuccess)
  loginSuccess(state, {payload}) {
    state.isLoggedIn = payload.isLoggedIn;
    state.profile = payload.profile;
  }

  @Mutation(LogoutSuccess)
  logoutSuccess(state, {payload}) {
    state.isLoggedIn = false;
    state.profile = {};
  }

  @Action(Logout)
  logout(state, {payload}) {
    this.oauthService.logOut();
    return new LogoutSuccess()
  }

  //not working 
  @Action(Login)
  async login(state, { payload }) {
    // this.oauthService.initImplicitFlow();
    const profile = await this.oauthService.fetchTokenUsingPasswordFlowAndLoadUserProfile('sumodemo', 'sumodemo');
    const isLoggedIn = true;
    return new LoginSuccess({isLoggedIn, profile});
  }

  @Action(Login)
  workAround(state, { payload }) {
    return fromPromise( this.login(state, { payload }))
  }
}

from store.

amcdnl avatar amcdnl commented on July 17, 2024

Thats odd cuz it calls forPromise under the hood. See: https://github.com/amcdnl/ngxs/blob/master/src/ngxs.ts#L83

from store.

deebloo avatar deebloo commented on July 17, 2024

@xmlking in your add method you don't have an await. Im not an expert with async functions but don't you need it to resolve or am I missing something?

from store.

amcdnl avatar amcdnl commented on July 17, 2024

No I think if you define async it automatically converts the results into a promise.

from store.

deebloo avatar deebloo commented on July 17, 2024

@amcdnl hmmmm im not sure if that is the case. to the internet!

from store.

xmlking avatar xmlking commented on July 17, 2024

@amcdnl are you able to reproduce this issue ?

from store.

abalad avatar abalad commented on July 17, 2024

I'm having the same problem, I'm trying to fire a Mutation from within an action, but Mutation does not run.

import { Store, Mutation, Action } from 'ngxs';
import { AuthenticationService } from '../../shared/resources/authentication/authentication.service';
import { Login, LoginSuccess, LoginFailure } from '../actions/login.action';
import { LoginState } from '../reducers/login/login.state';
import { InitialState } from './login.state';

@Store({
  defaults: InitialState
})
export class LoginStore {

  constructor( private authenticationService: AuthenticationService ){}

  @Mutation(Login)
  load(state: LoginState, action: Login) {
    return {
      ...state,
      error: null,
      pending: true
    };
  }

  @Mutation(LoginSuccess)
  loginSuccess(state: LoginState, action: LoginSuccess) {
    return {
      ...state,
      error: null,
      pending: false
    };
  }

  @Mutation(LoginFailure)
  loginFailure(state: LoginState, action: LoginFailure) {
    console.log(state,  action);
    return {
      ...state,
      error: action.payload,
      pending: false
    };
  }

  @Action(Login)
  login(state, { payload }){
    return this.authenticationService.authenticate(payload)
      .then( (response) => { console.log(response); } )
      .catch( (error) =>  new LoginFailure(error.message)  ); // HE WILL NOT DISPATCH A MUTATION

  }

}

from store.

amcdnl avatar amcdnl commented on July 17, 2024

I wonder if its related to the error?

from store.

amcdnl avatar amcdnl commented on July 17, 2024

Side note: I'm gonna put in place a global error handling system

from store.

abalad avatar abalad commented on July 17, 2024

@amcdnl So it's similar, because my request this.authenticationService.authenticate(payload) also returns a promisse async.

Note: I use another library(Not the Angular standard) to make requests, and she works with promisses

from store.

perjansson avatar perjansson commented on July 17, 2024

I'm also seeing this problem where a Mutation is not called if returned from an async / await Action.

from store.

amcdnl avatar amcdnl commented on July 17, 2024

Fixed in 1.5.3

from store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.