Skip to content

Conversation

brunolm
Copy link
Owner

@brunolm brunolm commented Jun 17, 2018

npm i -S redux redux-thunk @angular-redux/store @angular-redux/router @angular-redux/form
npm i -S ngx-redux-state-props@latest
export class AboutComponent {
  constructor(private actions: AboutActions, private redux: NgxReduxStatePropsService) {}

  get state() {
    return this.redux.appState;
  }

  dispatchSomeAction() {
    this.actions.someAction();
  }
}

Action

import { dispatch } from '@angular-redux/store';
import { Injectable } from '@angular/core';

const wait = (ms) => {
  return new Promise((r) => setTimeout(r, ms));
};

@Injectable()
export class AboutActions {
  static readonly getApiValueType = 'getApiValueType';
  static readonly apiSuccessType = 'apiSuccessType';

  @dispatch()
  getApiValue() {
    console.log('ACTION: getApiValue');

    const testTimeout = 1000;

    return async (dispatcher) => {
      await wait(testTimeout);
      console.log('after 1s');
      dispatcher(this.apiSuccess());
    };
  }

  @dispatch()
  apiSuccess() {
    console.log('ACTION: apiSuccess');

    return {
      type: AboutActions.apiSuccessType,
    };
  }
}

Reducer

import { AboutActions } from './about.actions';

export class AboutReducer {
  [AboutActions.getApiValueType](state = {}, action) {
    console.log('REDUCER: getApiValueType');

    return {
      ...state,
      somethingFromReducer: 'foobar',
      action,
    };
  }

  [AboutActions.apiSuccessType](state = {}, action) {
    console.log('REDUCER: apiSuccessType');
    const offset = 32;

    return {
      ...state,
      somethingFromReducer: 'foobar',
      type: action.type,
      action: Math.random().toString(offset),
    };
  }
}

@coveralls
Copy link

Coverage Status

Coverage decreased (-7.7%) to 81.731% when pulling 420e67d on feature/redux into 6a02742 on master.

@brunolm brunolm merged commit 1d13170 into master Jun 17, 2018
@brunolm brunolm deleted the feature/redux branch June 17, 2018 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants