Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngrx/store after action/effects UI notification

Tags:

angular

ngrx

I am using ngrx/store and ngrx/effects.

This is the flow,

  1. user click login button
  2. LOGIN Action dispatched
  3. $effects perform http.post credentials for login
  4. dispatch LOGIN_SUCCESS or LOGIN_FAILURE Action

Question: I would like to perform some UI task, eg, pull down the modal, or show a pop up of the error message, after the action.

How would I go about subscribing to the response in my component?

Thanks guys.

like image 634
jho Avatar asked Aug 27 '16 18:08

jho


1 Answers

Your state should have a flag that would notify your component that it should do an action.

Something like this:

State:

const initialState: SomeState = {
    loggedIn: false,
    ...
};

export default function(state = initialState, action: Action): SomeState {
    switch (action.type) {
        case StateActions.LOGIN_SUCCESS:
            return Object.assign({}, state, {loggedIn: true});
            ...

Then in your component you subscribe to the state and if loggedIn is true you know that you should for example show the modal.

Another approach would be to perform the task right in your effect through a service.

like image 122
Filip Lauc Avatar answered Oct 07 '22 06:10

Filip Lauc