zzdjk6/redux-thunk-routine

action like truncate?

majsterkoo opened this issue · 3 comments

Hi,
I often clean redux store on component umount (because there is no need for old data). In this situation I'm using REQUEST action which set the part of redux store to default state. But sometimes I won't set redux store to default state on REQUEST. So in this situation I need actions like REQUEST (for data request) and TRUNCATE or something else (for data clean).

Is there way how to globally added TRUNCATE action to routine, or Can you implement this action?
Thank you very much. Have a nice day!

Hi @majsterkoo

Sorry for the late reply.

I think there could be 2 ways to add TRUNCATE action.

  1. The simplest way is to treat TRUNCATE as a new routine with its own REQUEST/SUCCESS/FAILURE actions instead of considering it as an action of routine. In this way, you could have all benefits of routine if you want to process truncate asynchronously now or later on.

  2. It is also easy to extend the current ReduxThunkRoutine class to add your own actions. A possible implementation might be:

import { ReduxThunkRoutine } from "./index";
import { Action, createAction } from "redux-actions";

export class CustomThunkRoutine<TPayload, TError extends Error = Error> extends ReduxThunkRoutine<TPayload, TError> {

  readonly TRUNCATE: string;

  constructor(routineType: string) {
    super(routineType);
    this.TRUNCATE = `${this.routineType}/TRUNCATE`;
  }

  truncate = (): Action<any> => {
    const actionCreator = createAction(this.TRUNCATE);
    return actionCreator();
  };

  isTruncateAction = (action: Action<any>): action is Action<any> => {
    return action.type === this.TRUNCATE;
  };
}

Then you could use it like below:

const routine = new CustomThunkRoutine<DataType>('TEST/MOCK_ROUTINE');

dispatch(routine.truncate());

Could you please have a look and let me know what do you think?

Ooh, thank you very much.
The possibility of extending the class did not occur to me :) . I think, that you can add this to Readme (it has more ussages, for example: routine for show/hide elements - routine.show(), routine.hide() - etc..).

This is exactly what I want/need. Thank you very much again.

Thanks for the advice, I will add these information to Readme :)