Comments (12)
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.
Thats odd cuz it calls forPromise
under the hood. See: https://github.com/amcdnl/ngxs/blob/master/src/ngxs.ts#L83
from store.
@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.
No I think if you define async it automatically converts the results into a promise.
from store.
@amcdnl hmmmm im not sure if that is the case. to the internet!
from store.
@amcdnl are you able to reproduce this issue ?
from store.
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.
I wonder if its related to the error?
from store.
Side note: I'm gonna put in place a global error handling system
from store.
@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.
I'm also seeing this problem where a Mutation is not called if returned from an async / await Action.
from store.
Fixed in 1.5.3
from store.
Related Issues (20)
- 🐞[BUG]: ngxs-cli - generated code - contains nbsp and the spec uses deprecated code HOT 1
- 🐞[BUG]: Using Angular 16 webapp is throwing "inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext` HOT 3
- best solution in impliment microfrontEnd HOT 1
- 🚀[FEATURE]: Support latest angular and typescript versions (decorators arent working) HOT 5
- 🐞[BUG]: Uncaught TypeError: Cannot read properties of undefined (reading 'SystemState') HOT 7
- 🐞[BUG]: @Select decorator doesn't trigger value HOT 1
- 🐞[BUG]: it seems that Ngxs is not compatible with Jest ESM HOT 4
- 🐞[BUG]: patch operator does not handle null existing values
- 🐞[BUG]: export 'StateContext' (imported as 'StateContext') was not found in '@ngxs/store' HOT 2
- 🚀[FEATURE]: How to Load New State at Runtime? HOT 1
- 🐞[BUG]: Could not use NgxsFormPlugin in subModule on Federation Architecture HOT 1
- 🚀[FEATURE]: Support Angular 17 HOT 9
- 🐞[BUG]: NgxsFormPlugin not work in Angular17 HOT 1
- 🐞[BUG]: ngxs-store.js: Invalid count value HOT 16
- 🐞[BUG]: ResetForm action side effect completes after the action
- 🚀[FEATURE]: Allow Actions with non-static type fields HOT 2
- NoInfer TS 5.4 HOT 2
- 🐞[BUG]: Cannot read properties of undefined (reading 'NGXS_META') HOT 2
- 📚[DOCS]: Setup on application root level HOT 2
- Typing inline generic operators seems much more annoying with 3.8 (coming from 3.6) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store.