Skip to content
Snippets Groups Projects
shared.state.ts 1.62 KiB
Newer Older
import {BaseStateModel} from "@app/base.state";
import {StateEnum} from "@app/shared/enums/state.enum";
import {GetAllLanguages} from "@app/shared/language.action";
import {LanguageState, LanguageStateModel} from "@app/shared/language.state";
import {GetAllLicenses} from "@app/shared/license.action";
import {LicenseState, LicenseStateModel} from "@app/shared/license.state";
import {GetAllResources, GetAllResourcesFail, GetAllResourcesSuccess} from "@app/shared/shared.action";
import {Action, State, StateContext} from "@ngxs/store";
Florent Poittevin's avatar
Florent Poittevin committed

export interface SharedStateModel extends BaseStateModel {
  language: LanguageStateModel;
  license: LicenseStateModel;
Florent Poittevin's avatar
Florent Poittevin committed
}

@State<SharedStateModel>({
  name: StateEnum.shared,
Florent Poittevin's avatar
Florent Poittevin committed
  defaults: {
    isLoading: false,
    language: null,
    license: null,
Florent Poittevin's avatar
Florent Poittevin committed
  },
  children: [
    LanguageState,
    LicenseState,
  ],
Florent Poittevin's avatar
Florent Poittevin committed
})
export class SharedState {
Florent Poittevin's avatar
Florent Poittevin committed
  constructor() {
  @Action(GetAllResources, {cancelUncompleted: true})
  getAllResources(ctx: StateContext<SharedStateModel>, action: GetAllResources) {
Florent Poittevin's avatar
Florent Poittevin committed
    ctx.patchState({
      isLoading: true,
    });

Florent Poittevin's avatar
Florent Poittevin committed
    ctx.dispatch([
      new GetAllLanguages(),
      new GetAllLicenses(),
      new GetAllResourcesSuccess(), // TO FIX Should be raised only when GetAllLanguagesSuccess and GetAllLicensesSuccess is raised)
    ]);
  @Action(GetAllResourcesSuccess)
  getAllResourcesSuccess(ctx: StateContext<SharedStateModel>, action: GetAllResourcesSuccess) {
Florent Poittevin's avatar
Florent Poittevin committed
    ctx.patchState({
      isLoading: false,
    });
  }

  @Action(GetAllResourcesFail)
  getAllResourcesFail(ctx: StateContext<SharedStateModel>) {
Florent Poittevin's avatar
Florent Poittevin committed
    ctx.patchState({
      isLoading: false,
    });
  }
}