How can I trigger errorType in a generalized way?
I'm trying to manage the loading state of request fired inside thunks based on the thunks startType, successType and failType actions. I have listeners in place on each thunk based on a naming convention. My issue right now is that thunks do not fire the failType action when a promise is rejected. I could not find a store configuration parameter to enable this or a general way to handle this without touching each thunk since from what I've seen the fail function comes from the helpers param.
This is my current approach to handle the loading and errors
import { actionOn } from "easy-peasy";
const SUFFIX = "Request";
function createLoadingThunks(store) {
const reducerNames = Object.keys(store);
let newStore = {};
reducerNames.map((reducerName) => (newStore[reducerName] = enhance(store[reducerName])));
return newStore;
}
const enhance = (reducer) => {
const thunkNames = Object.keys(reducer).filter((key) => key.endsWith(SUFFIX));
return {
...reducer,
loading: false,
...makeListener(thunkNames),
};
};
function makeListener(thunkNames) {
return {
loadingStart: actionOn(getTypes(thunkNames, "startType"), (state) => {
state.loading = true;
}),
loadingEnd: actionOn(getTypes(thunkNames, "successType"), (state) => {
state.loading = false;
}),
loadingFail: actionOn(getTypes(thunkNames, "failType"), (state, error) => {
state.loading = false;
state.error = error.message;
}),
};
}
function getTypes(thunkNames, actionType) {
return (actions) => thunkNames.map((name) => actions[name][actionType]);
}
export default createLoadingThunks;
Did you find a solution to this?
@jmyrland Nope.
As a workaround, I found the way it used to be done before and re-introduced it using patch-package