All files / src/app/user user.actions.ts

100% Statements 38/38
100% Branches 0/0
100% Functions 12/12
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x 1x 1x               1x   1x 1x 1x 1x 1x 1x           1x     2x     1x 2x                 1x 1x     1x 2x             1x 1x     1x 2x             1x 1x     1x 2x             1x 1x     1x 2x 2x 2x 2x             1x 1x     1x  
import { Injectable} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { NgRedux, select } from '@angular-redux/store';
 
import { IAppState } from '../store/state/AppState';
import { IUser } from '../model/user.model';
import { ifNull } from '../store/selector-helpers/selector-helpers';
import { UserActionType } from '../store/actions/userActionType';
 
@Injectable()
export class UserActions {
 
  static INITIALIZE_USERS_REQUEST = 'INITIALIZE_USERS_REQUEST';
  static INITIALIZE_USERS_SUCCESS = 'INITIALIZE_USERS_SUCCESS';
  static INITIALIZE_USERS_FAILED = 'INITIALIZE_USERS_FAILED';
  static SET_CURRENT_USER = 'SET_CURRENT_USER';
  static UPDATE_CURRENT_USER = 'UPDATE_CURRENT_USER';
  static ACTIONS = [UserActions.INITIALIZE_USERS_REQUEST,
                    UserActions.INITIALIZE_USERS_SUCCESS,
                    UserActions.INITIALIZE_USERS_FAILED,
                    UserActions.SET_CURRENT_USER,
                    UserActions.UPDATE_CURRENT_USER];
 
  @select('users') users$: Observable<IUser[]>;
 
  constructor(
    private ngRedux: NgRedux<IAppState>,
  ) {}
 
  createInitializeUsersRequest(): UserActionType {
    return {
      type: UserActions.INITIALIZE_USERS_REQUEST,
      httpRequest: {
        url: 'api/users',
        successAction: this.createInitializeUsersSuccess,
        failedAction: this.createInitializeUsersFailed,
      }
    };
  };
  initializeUsersRequest() {
    this.ngRedux.dispatch(this.createInitializeUsersRequest());
  }
 
  createInitializeUsersSuccess(response): UserActionType {
    return {
      type: UserActions.INITIALIZE_USERS_SUCCESS,
      payload: {
        users: [...response]
      }
    };
  };
  initializeUsersSuccess(response) {
    this.ngRedux.dispatch(this.createInitializeUsersSuccess(response));
  }
 
  createInitializeUsersFailed(error): UserActionType {
    return {
      type: UserActions.INITIALIZE_USERS_FAILED,
      payload: {
        error
      }
    };
  };
  initializeUsersFailed(error) {
    this.ngRedux.dispatch(this.createInitializeUsersFailed(error));
  }
 
  createSetCurrentUser(user: IUser): UserActionType {
    return {
      type: UserActions.SET_CURRENT_USER,
      payload: {
        currentUser: user
      }
    };
  }
  setCurrentUser(user: IUser | null) {
    this.ngRedux.dispatch(this.createSetCurrentUser(user));
  }
 
  createUpdateCurrentUser(firstName: string, lastName: string): UserActionType {
    const newCurrentUser = Object.assign({}, this.ngRedux.getState().user.currentUser);
    newCurrentUser.firstName = firstName;
    newCurrentUser.lastName = lastName;
    return {
      type: UserActions.UPDATE_CURRENT_USER,
      payload: {
        currentUser: newCurrentUser
      }
    };
  }
  updateCurrentUser(firstName: string, lastName: string) {
    this.ngRedux.dispatch(this.createUpdateCurrentUser(firstName, lastName));
  }
 
}