cartant/ts-action

Action Creator: Payload with props

Closed this issue · 4 comments

negue commented

Is there a way to have an action that accepts the normal payload with the creation method BUT also adds internally another property that is added during the effect/meta-reducer?

For example:

I want to add an requestedAction-property (besides the usual payload) which is filled with the prior action in my effect, that way I know what action triggered which one

Without an example, it's difficult to understand what it is that you want. And I don't like making assumptions, as that wastes time. I do, however, have a question, why don't you use two actions and two action creators?

negue commented

Example:

I have like 4 shared states (NGRX StoreModule.forFeature, lets say Feature A to Feature D) and additional SuperState 1 SuperState 2 that work on two states Feature A & B.

if I have following Actions:

const MySuperStateActions = {
   doSomething: action('[Super State Action] Do Something :)', payload<IDoSomethingPayload>()),
   
   doSomethingMore: action('[Super State Action] Do Something more :>', payload<IDoMorePayload>()),
} 

const MyFeatureAStateActions = {
   featureExecute: action('[Feature A] Execute changes', payload<number>()),
   featureExecuted: action('[Feature A] Changes executed', payload<number>()),
   featureExecuteFailed: action('[Feature A] Failed executing changes', payload<number>()),
}


const MyFeatureBStateActions = {
   featureExecute: action('[Feature B] Execute changes', payload<number>()),
   featureExecuted: action('[Feature B] Changes executed', payload<number>()),
   featureExecuteFailed: action('[Feature B] Failed executing changes', payload<number>()),
}

these feature actions may work separatly on their own but also could be trigger by the superstate itself.

The superstate-effects listen to the feature A / B actions, but should only act on it if the requestAction-prop was one of superstate-action.

So if I would go the two action/-creators-way. I'd have two action creators => two actions per (action I normally have in my effects / reducers), then I'd have twice as many reducer switches/effects/ stuff that would have to be changed aswell. Now if I add some more actions these would have to created twice / thrice / Nth times depending on how many different states would have to work together.

Thats why I thought it would be nice to have an action creator like this:

action(name, payload<Type>(), props<{requestAction: IActionOfSomething}>()) => which has the normal creation call like const myAction = action(myPayload) but then could also have the property accessed by myAction.IActionOfSomething = data

You should be able to do what you want using the action signature that accepts a creator function:

const foo = action("foo", payload<{ f: string }>());
const bar = action("bar",
  (payload: { b: string }) => ({
    payload,
    requestAction: undefined! as ReturnType<typeof foo>
  }));

Actions created using the bar action creator will have this shape:

{
  type: "bar";
  payload: { b: string };
  requestedAction: { type: "foo"; payload: { f: string } };
}

What you do with the requestedAction prop - e.g. make it optional, etc. - is up to you.

negue commented

Thank you!, that's quite a nice way to add those properties 🎉