import { Injectable } from '@angular/core';
import { State, Action, StateContext, createSelector } from '@ngxs/store';
import { tap } from 'rxjs/operators';
export interface NovelsInfoStateModel {
export class GetNovelById {
static readonly type = '[Novels info] Get novel by ID';
constructor(public id: string) {}
@State<NovelsInfoStateModel>({
export class NovelsInfoState {
static getNovelById(id: string) {
return createSelector([NovelsInfoState], (state: NovelsInfoStateModel) => state[id]);
constructor(private novelsService: NovelsService) {}
getNovelById(ctx: StateContext<NovelsInfoStateModel>, action: GetNovelById) {
const novels = ctx.getState();
// If the novel with ID has been already loaded
// we just break the execution
return this.novelsService.getNovelById(id).pipe(
ctx.patchState({ [id]: novel });