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(...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);
}