Created
November 16, 2023 17:59
-
-
Save teomanofficial/31e1eb9fbe1469a489d36d988e05e4be to your computer and use it in GitHub Desktop.
Anguar NGXS - ProductsState
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
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