Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
export const initialState: State = adapter.getInitialState({
selectedBookId: null,
});
export const reducer = createReducer(
initialState,
on(
BooksApiActions.searchSuccess,
CollectionApiActions.loadBooksSuccess,
(state, { books }) => adapter.addMany(books, state)
/** Action to log in the User from the Login Page */
export const login = createAction(
'[Login Page] Login',
// notice 👇 payload wrapping
props<{payload: {username: string; password: string;}}>(),
)
export class MyEffects {
constructor(private readonly actions$: Actions) {}
@Effect
login$ = this.actions$.pipe(
ofType(login), // provide type safety *without* ActionsUnion
...
)
}
export class MyEffects {
constructor(private readonly actions$: Actions<ActionsUnion>) {}
@Effect
login$ = this.actions$.pipe(
// ensures type safety only when ActionsUnion is provided
ofType(login.type),
...
)
/**
* Action to log in the User from the Login Page.
* Uses the most secure password by default.
*/
export const login = createAction(
'[Login Page] Login',
(username: string, password = 'p@ssw0rdLol') => ({username, password}),
);
function reducer(state, action) {
switch(action.type) {
case login.type: { // <----- usage in reducer
...
}
}
}
@Effect
login({username: 'Tim', password: 'NgRx'})
/** Action to log in the User from the Login Page */
export const login = createAction(
'[Login Page] Login',
props<{username: string; password: string;}>(),
)
/** see below - comment is required for all exported symbols */
export const LOGIN = '[Login Page] Login',
interface LoginPayload {
username: string;
password: string;
}
/** Action to log in the User from the Login Page */
export class Login implements Action {
export enum ActionTypes {
Login = '[Login Page] Login',
}
export class Login implements Action {
readonly type = ActionTypes.Login;
constructor(public payload: { username: string; password: string }) {}
}