Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse selector in other selectors in NGXS

Tags:

ngxs

I have two classes PizzasState and ToppingsState. PizzaState already has selector to get selected pizza.

@State<PizzaStateModel>({
  name: 'pizzas',
  defaults: initialState
})
export class PizzasState {
  constructor(private pizzaService: PizzasService) {
  }

  @Selector([RouterState])
  static getSelectedPizza(
    state: PizzaStateModel,
    routerState: RouterStateModel<RouterStateParams>
  ): Pizza {
    const pizzaId = routerState.state && routerState.state.params.pizzaId;
    return pizzaId && state.entities[pizzaId];
  }

  @Selector()
  getPizzaVisualized(state: PizzaStateModel): Pizza {
    //
    // what is here?
    //
  }
}

and ToppingsState has selectedToppings

@State({
  name: 'toppings',
  defaults: initialState
})
export class ToppingsState {
  constructor(private toppingsService: ToppingsService) {
  }

  @Selector()
  static selectedToppings(state: ToppingsStateModel): number[] {
    return state.selectedToppings;
  }

Now I want to join my selected pizza with selected toppings and get my visualized pizza.

How can I reuse getSelectedPizza and getSelectedToppings correctly? Thank you

like image 961
Сергей Avatar asked Oct 14 '25 07:10

Сергей


1 Answers

It looks like you need to use a joining selector.

@Selector([ToppingsState.selectedToppings])
getPizzaVisualized(state: PizzaStateModel, selectedToppings: number[]): Pizza {
    // User data from both states.
}
like image 165
Garth Mason Avatar answered Oct 19 '25 11:10

Garth Mason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!