projects.reducer.ts
angular/examples/advanced/src/app/state/projects.reducer.ts
// @ts-nocheck
import { createReducer, on } from '@ngrx/store';
import { createEntityAdapter } from '@ngrx/entity';
import { ProjectsActions } from './projects.actions';
import { Project } from './projects.models';
export interface ProjectsState {
loading: boolean;
error: unknown;
ids: string[];
entities: Record<string, Project>;
}
export type ProjectsStateType = ReturnType<typeof projectsReducer>;
const adapter = createEntityAdapter<Project>();
const initialState = adapter.getInitialState({
loading: false,
error: null,
});
export const projectsReducer = createReducer(
initialState,
on(ProjectsActions.loadRequested, (state) => ({ ...state, loading: true, error: null })),
on(ProjectsActions.loadSucceeded, (state, { projects }) =>
adapter.setAll(projects, { ...state, loading: false }),
),
on(ProjectsActions.loadFailed, (state, { error }) => ({ ...state, loading: false, error })),
on(ProjectsActions.createSucceeded, (state, { project }) => adapter.addOne(project, state)),
);
Related articles
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
Read article →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
Read article →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
Read article →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
Read article →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
Read article →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
Read article →