Skip to content

Instantly share code, notes, and snippets.

@teomanofficial
Created November 16, 2023 17:59
Show Gist options
  • Save teomanofficial/31e1eb9fbe1469a489d36d988e05e4be to your computer and use it in GitHub Desktop.
Save teomanofficial/31e1eb9fbe1469a489d36d988e05e4be to your computer and use it in GitHub Desktop.
Anguar NGXS - ProductsState
import { tap } from "rxjs";
import { Injectable } from "@angular/core";
import { Action, Selector, State, StateContext } from "@ngxs/store";
import { GetProducts } from "./products.actions";
import { ProductsStateModel } from "../models/products-state.model";
import { ProductsService } from "@store/products/services/products.service";
@State<ProductsStateModel>({
name: 'products',
defaults: {
skip: 0,
limit: 0,
total: 0,
products: []
}
})
@Injectable()
export class ProductsState {
constructor(private readonly productsService: ProductsService) {
}
@Selector()
static selectProducts({ products }: ProductsStateModel) {
return products;
}
@Action(GetProducts)
dispatchProductsState(context: StateContext<ProductsStateModel>, { request }: GetProducts) {
return this.productsService.getProducts(request)
.pipe(tap(response => context.patchState({ ...response })))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment