How to use the @ngrx/store.createReducer function in @ngrx/store

To help you get started, we’ve selected a few @ngrx/store examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github trasukg / local-aprs / src / app / aprs-situation / aprs-situation.reducer.ts View on Github external
return packet;
    }
}

function processPacket(aprsSituation: State, packet) {
  packet=ensurePacketReceivedAtIsDate(packet);
  aprsSituation.rawPackets.push(packet);
  aprsSituation.lastServerTime=Math.max(aprsSituation.lastServerTime,
    packet.receivedAt.getTime());

  // I suspect that the deduplications and summaries might be best
  // done as selectors.
  //calculateSummaries(state);
}

const aprsSituationReducer = createReducer(
  initialState,

  on(AprsSituationActions.receivedPacket,
    function(aprsSituation, { packet }):State {
      console.log('receivedPacket');
      let start=Date.now();
      // Since state is immutable, we need to copy the list of packets.
      let newSituation:State = {
        ...aprsSituation,
        rawPackets: [],
        deduplicatedPackets: [],
        lastServerTime: 0
      };
      aprsSituation.rawPackets.map(packet => {
        processPacket(newSituation, packet);
      });
github legumeinfo / lis_context_viewer / client / src / app / gene / store / reducers / layout.reducer.ts View on Github external
// store
import * as LayoutActions from '@gcv/gene/store/actions/layout.actions';

export const layoutFeatureKey = 'layout';

export interface State {
  showLeftSlider: boolean;
  leftSliderContent: string;
}

const initialState: State = {
  showLeftSlider: false,
  leftSliderContent: null,
};

export const reducer = createReducer(
  initialState,
  on(LayoutActions.CloseLeftSlider, (state) => {
    return {
      ...state,
      showLeftSlider: false,
    };
  }),
  on(LayoutActions.OpenLeftSlider, (state) => {
    return {
      ...state,
      showLeftSlider: true,
    };
  }),
  on(LayoutActions.ToggleLeftSlider, (state) => {
    return {
      ...state,
github tensorflow / tensorboard / tensorboard / webapp / core / store / core_reducers.ts View on Github external
// HACK: These imports are for type inference.
// https://github.com/bazelbuild/rules_nodejs/issues/1013
/** @typehack */ import * as _typeHackStore from '@ngrx/store/store';

const initialState: CoreState = {
  activePlugin: null,
  plugins: {},
  pluginsListLoaded: {
    state: DataLoadState.NOT_LOADED,
    lastLoadedTimeInMs: null,
  },
  reloadPeriodInMs: 30000,
  reloadEnabled: true,
};

const reducer = createReducer(
  initialState,
  on(
    actions.changePlugin,
    (state: CoreState, {plugin}): CoreState => {
      return {...state, activePlugin: plugin};
    }
  ),
  on(
    actions.pluginsListingRequested,
    (state: CoreState): CoreState => {
      return {
        ...state,
        pluginsListLoaded: {
          ...state.pluginsListLoaded,
          state: DataLoadState.LOADING,
        },
github MrWolfZ / ngrx-forms / src / reducer.spec.ts View on Github external
it('should work with createReducer', () => {
    const state = {
      prop: 'value',
      control: INITIAL_STATE.controls.inner,
      group: INITIAL_STATE,
      array: INITIAL_STATE.controls.inner5,
    };

    const reducer = createReducer(
      state,
      onNgrxForms(),
    );

    const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, s => ({ ...s }));

    let resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER_ID));
    expect(resultState.control).not.toBe(INITIAL_STATE.controls.inner);

    resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_ID));
    expect(resultState.group).not.toBe(INITIAL_STATE);

    resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER5_ID));
    expect(resultState.array).not.toBe(INITIAL_STATE.controls.inner5);
  });
});
github chrisjwalk / angular-cli-netcore-ngrx-starter / ClientApp / src / app / core / store / reducers / layout.reducer.ts View on Github external
import { createReducer, on, Action } from '@ngrx/store';
import * as layoutActions from 'app/core/store/actions';

export interface State {
  showSidenav: boolean;
  title: string;
}

const initialState: State = {
  showSidenav: false,
  title: 'Home',
};

const layoutReducer = createReducer(
  initialState,
  on(layoutActions.closeSidenav, (state) => ({
    ...state,
    showSidenav: false,
  })),
  on(layoutActions.openSidenav, (state) => ({
    ...state,
    showSidenav: true,
  })),
  on(layoutActions.toggleSidenav, (state) => ({
    ...state,
    showSidenav: !state.showSidenav,
  })),
  on(layoutActions.setTitle, (state, { title }) => ({
    ...state,
    title,
github Verseghy / website_frontend / apps / math-competition / src / app / state / competition / competition.reducer.ts View on Github external
export const competitionFeatureKey = 'competition'

export interface State {
  problems: Problem[]
  solutions: Solution[]
  teamID: string
}

export const initialState: State = {
  problems: [],
  solutions: [],
  teamID: '',
}

const competitionReducer = createReducer(
  initialState,

  on(CompetitionActions.loadTeamSucceed, (state, { id }) => ({ ...state, teamID: id })),

  on(CompetitionActions.problemAdded, (state, payload) => ({ ...state, problems: [...state.problems, payload] })),
  on(CompetitionActions.problemModified, (state, payload) => ({
    ...state,
    problems: state.problems.map(e => {
      if (e.id !== payload.id) return e
      return payload
    }),
  })),
  on(CompetitionActions.problemRemoved, (state, payload) => ({
    ...state,
    problems: state.problems.filter(e => e.id !== payload.id),
  })),
github stefanoslig / angular-ngrx-nx-realworld-example-app / libs / article / src / lib / +state / article.reducer.ts View on Github external
favorited: false,
    favoritesCount: 0,
    author: {
      username: '',
      bio: '',
      image: '',
      following: false,
      loading: false,
    },
  },
  comments: [],
  loaded: false,
  loading: false,
};

const reducer = createReducer(
  articleInitialState,
  on(ArticleActions.loadArticleSuccess, (state, action) => ({
    ...state,
    data: action.article,
    loaded: true,
    loading: false,
  })),
  on(ArticleActions.loadArticleFail, state => ({
    ...state,
    data: articleInitialState.data,
    loaded: false,
    loading: false,
  })),
  on(ArticleActions.addCommentSuccess, (state, action) => {
    const comments: ArticleComment[] = [action.comment, ...state.comments];
    return { ...state, comments };
github tomastrajan / angular-ngrx-material-starter / projects / angular-ngrx-material-starter / src / app / features / examples / crud / books.reducer.ts View on Github external
});

export const initialState: BookState = bookAdapter.getInitialState({
  ids: ['123'],
  entities: {
    '123': {
      id: '123',
      title: 'Reactive Programming with Angular and ngrx',
      author: 'Oren Farhi',
      description:
        'Learn to Harness the Power of Reactive Programming with RxJS and ngrx Extensions'
    }
  }
});

const reducer = createReducer(
  initialState,
  on(actionBooksUpsertOne, (state, { book }) =>
    bookAdapter.upsertOne(book, state)
  ),
  on(actionBooksDeleteOne, (state, { id }) => bookAdapter.removeOne(id, state))
);

export function bookReducer(state: BookState | undefined, action: Action) {
  return reducer(state, action);
}
github devonfw / my-thai-star / angular / src / app / book-table / store / reducers / book-table.reducer.ts View on Github external
export const initialState: State = {
  pending: false,
  errorMessage: '',
  textMessage: '',
  booking: undefined,
  token: undefined,
  bookingResponse: {
    name: '',
    bookingDate: '',
    bookingToken: '',
    tableId: undefined,
    email: '',
  },
};

const bookTableReducer = createReducer(
  initialState,
  on(bookTableActions.bookTable, (state, { booking }) => ({
    ...state,
    pending: true,
    booking: booking,
  })),
  on(bookTableActions.bookTableSuccess, (state, { bookingResponse }) => ({
    ...state,
    pending: false,
    bookingResponse: bookingResponse,
  })),
  on(bookTableActions.bookTableFail, (state, { error }) => ({
    ...state,
    pending: false,
    errorMessage: error.message,
  })),
github SciCatProject / catanie / src / app / state-management / reducers / jobs.reducer.ts View on Github external
import { createReducer, Action, on } from "@ngrx/store";
import { JobsState, initialJobsState } from "state-management/state/jobs.store";
import * as fromActions from "state-management/actions/jobs.actions";

const reducer = createReducer(
  initialJobsState,
  on(fromActions.fetchJobsAction, state => ({ ...state, isLoading: true })),
  on(fromActions.fetchJobsCompleteAction, (state, { jobs }) => ({
    ...state,
    jobs,
    isLoading: false
  })),
  on(fromActions.fetchJobsFailedAction, state => ({
    ...state,
    isLoading: false
  })),

  on(fromActions.fetchCountAction, state => ({ ...state, isLoading: true })),
  on(fromActions.fetchCountCompleteAction, (state, { count }) => ({
    ...state,
    isLoading: false,