import { on } from '@ngrx/store'; import { HttpError } from 'ish-core/models/http-error/http-error.model'; export function payload

() { return (args: P) => ({ payload: { ...args } }); } export function httpError

() { return (args: { error: HttpError } & P) => ({ payload: { ...args } }); } /* eslint-disable @typescript-eslint/no-explicit-any */ export function setLoadingOn(...actionCreators: any) { const stateFnc = (state: any) => ({ ...state, loading: typeof state.loading === 'boolean' ? true : state.loading + 1, }); return on(...actionCreators, stateFnc); } export function unsetLoadingOn(...actionCreators: any) { const stateFnc = (state: any) => ({ ...state, loading: typeof state.loading === 'boolean' ? false : state.loading - 1, }); return on(...actionCreators, stateFnc); } function calculateLoading(state: S): T { if (typeof state.loading === 'number' && state.loading - 1 < 0) { console.warn('State loading would be decreased below 0.'); } return (typeof state.loading === 'boolean' ? false : Math.max((state.loading as number) - 1, 0)) as T; } export function unsetLoadingAndErrorOn(...actionCreators: any) { const stateFnc = (state: any) => ({ ...state, loading: calculateLoading(state), error: undefined as HttpError, }); return on(...actionCreators, stateFnc); } export function setErrorOn(...actionCreators: any) { const stateFnc = (state: any, action: { payload: { error: HttpError }; type: string }) => ({ ...state, error: action.payload.error, loading: calculateLoading(state), }); return on(...actionCreators, stateFnc); }