Skip to content

Instantly share code, notes, and snippets.

View thecodinganalyst's full-sized avatar
🏠
Working from home

Dennis Cai thecodinganalyst

🏠
Working from home
View GitHub Profile
@thecodinganalyst
thecodinganalyst / books.state.ts
Created June 13, 2022 01:23
NgRx State Example
import {Book} from "../book";
export interface AppState {
bookStore: BookStore;
}
export interface BookStore {
books: ReadonlyArray<Book>;
selectedBook?: Book;
showDetail: boolean;
@thecodinganalyst
thecodinganalyst / books.selectors.ts
Created June 13, 2022 01:24
NgRx Selector Example
import {createFeatureSelector, createSelector} from "@ngrx/store";
import {BookStore} from "./books.state";
const bookStore = createFeatureSelector<BookStore>('bookStore');
const books = createSelector( bookStore, (bookStoreState => bookStoreState.books));
const selectedBook = createSelector(bookStore, (bookStoreState => bookStoreState.selectedBook));
const showDetail = createSelector(bookStore, (bookStoreState => bookStoreState.showDetail));
@thecodinganalyst
thecodinganalyst / books.actions.ts
Created June 13, 2022 01:24
NgRx Action Example
import {createAction, props} from "@ngrx/store";
import {Book} from "../book";
const loadBooks = createAction("[BookList] Load Books");
const booksLoaded = createAction("[BookList] Books Loaded", props<{ books: ReadonlyArray<Book> }>())
const showBook = createAction("[BookList] Show Book", props<{book: Book}>());
const newBook = createAction("[BookList] New Book");
@thecodinganalyst
thecodinganalyst / books.reducer.ts
Created June 13, 2022 01:25
NgRx Reducer Example
import {createReducer, on} from "@ngrx/store";
import {BookStoreActions} from "./books.actions";
import {BookStore} from "./books.state";
export const initialState: BookStore = { books: [], selectedBook: undefined, showDetail: false};
export const booksReducer = createReducer(
initialState,
on(BookStoreActions.booksLoaded, (state, {books}) => ({...state, books: books})),
on(BookStoreActions.showBook, (state, {book}) => ({...state, selectedBook: book, showDetail: true})),
@thecodinganalyst
thecodinganalyst / books.effects.ts
Created June 13, 2022 01:25
NgRx Effects Example
@Injectable()
export class BooksEffects {
loadBooks$ = createEffect(() => this.actions$.pipe(
ofType(BookStoreActions.loadBooks),
mergeMap(() => this.booksService.getBooks().pipe(
map(books => BookStoreActions.booksLoaded({books})),
catchError(() => EMPTY)
))
));
@thecodinganalyst
thecodinganalyst / books.app.module1.ts
Created June 13, 2022 01:30
NgRx Module with Store Only
@NgModule({
declarations: [
AppComponent,
ItemComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {dataEncapsulation: false}),
ReactiveFormsModule,
@thecodinganalyst
thecodinganalyst / books.app.module2.ts
Created June 13, 2022 01:31
NgRx module with Store and Effects
@NgModule({
declarations: [
AppComponent,
ItemComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {dataEncapsulation: false}),
ReactiveFormsModule,
ngOnInit(): void{
this.loadBooks();
this.store.select(Selector.books).subscribe(books => this.books$ = books);
this.store.select(Selector.selectedBook).subscribe(book => {
this.selectedBook = book;
});
this.store.select(Selector.showDetail).subscribe(showDetail => {
this.showDetail = showDetail;
showDetail ? this.showPopup() : this.hidePopup();
});
@thecodinganalyst
thecodinganalyst / app.component.spec.ts
Created June 16, 2022 13:59
Angular TestBed Configure
let store: MockStore<AppState>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent, ItemComponent],
imports: [ReactiveFormsModule],
providers: [provideMockStore()],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
fixture = TestBed.createComponent(AppComponent);
app = fixture.componentInstance;
@thecodinganalyst
thecodinganalyst / books.effects.spec.ts
Created June 16, 2022 14:01
Angular NgRx Effects TestBed ConfigureTestingModule
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BooksEffects,
provideMockActions(() => actions$),
provideMockStore({initialState}),
],
imports: [HttpClientModule]
})
booksService = TestBed.inject(BooksService)