This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const initialState: State = adapter.getInitialState({ | |
selectedBookId: null, | |
}); | |
export const reducer = createReducer( | |
initialState, | |
on( | |
BooksApiActions.searchSuccess, | |
CollectionApiActions.loadBooksSuccess, | |
(state, { books }) => adapter.addMany(books, state) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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;}}>(), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MyEffects { | |
constructor(private readonly actions$: Actions) {} | |
@Effect | |
login$ = this.actions$.pipe( | |
ofType(login), // provide type safety *without* ActionsUnion | |
... | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
... | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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}), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reducer(state, action) { | |
switch(action.type) { | |
case login.type: { // <----- usage in reducer | |
... | |
} | |
} | |
} | |
@Effect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
login({username: 'Tim', password: 'NgRx'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Action to log in the User from the Login Page */ | |
export const login = createAction( | |
'[Login Page] Login', | |
props<{username: string; password: string;}>(), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export enum ActionTypes { | |
Login = '[Login Page] Login', | |
} | |
export class Login implements Action { | |
readonly type = ActionTypes.Login; | |
constructor(public payload: { username: string; password: string }) {} | |
} |
NewerOlder