One way to minimize the code is by identifying common returns and setting up a map. For instance, if reducerUtils.requestListData
is being called multiple times (in this case 5 times), you can simplify it by using a map to handle the differences and eliminate unnecessary switch cases.
My initial suggestion is to convert action.types
that have shared return functions into a new map (myMap
) with keys corresponding to the specific different parameters (for example, option1
and option2
). If you're unsure about the rest of your code, another approach could be to replace option1
with unknown
or a list of possibilities separated by |
inside <>
.
const myMap = {
[EquipmentActionTypes.REQUEST_EQUIPMENT]: {
option1: IEquipment[],
option2: ['equipmentList']
},
[EquipmentActionTypes.REQUEST_INITIAL_SPECIFICATION]: {
option1: IEquipmentInitialSpecification[],
option2: ['equipmentInitialSpecification']
},
...
}
switch (action.type) {
case EquipmentActionTypes.REQUEST_INITIAL_SPECIFICATION:
case EquipmentActionTypes.REQUEST_MANUFACTURERS:
...
return reducerUtils.requestListData < IEquipmentState, myMap[action.type].option1 > (lastState, myMap[action.type].option2);
Building on the previous concept, you might be able to streamline the process further by moving the function into option1
and completely eliminating the switch case. For more insights, check out this related article or this one.
const myMap = {
[EquipmentActionTypes.REQUEST_EQUIPMENT]:{
option1: reducerUtils.requestListData<IEquipmentState, IEquipment[] >(),
option2: ['equipmentList']
},
...
}
myMap[action.type].option1(lastState, myMap[action.type].option2, actionPayload)