I have these few helper function that are used to check if the string in the given array is present and if the certain steps are present and then only have the other function fire. The reason I currently have them separated is because arrTours need to be associated with only those arrSteps.
// Handles for manditory action needed by user
const arrTourOver = ['OverviewTour'];
const arrStepOver = [6, 7];
export const handleJoyrideOverview = (
dispatch: any,
tour: any,
index: number
) => {
arrTourOver.includes(tour?.openTour) &&
arrStepOver.includes(tour?.stepIndex) &&
JoyRideDelayContinue(dispatch, tour, tour?.openTour, index);
};
// Handles for manditory action needed by user
const arrTourResize = ['ResizingWidgets'];
const arrStepResize = [0, 1];
export const handleJoyrideResize = (
dispatch: any,
tour: any,
index: number
) => {
arrTourResize.includes(tour?.openTour) &&
arrStepResize.includes(tour?.stepIndex) &&
JoyRideDelayContinue(dispatch, tour, tour?.openTour, index);
};
// Handles for manditory action needed by user
const arrTourDock = ['DockbarFunctions'];
const arrStepDock = [3, 4];
export const handleJoyrideDock = (dispatch: any, tour: any, index: number) => {
arrTourDock.includes(tour?.openTour) &&
arrStepDock.includes(tour?.stepIndex) &&
JoyRideDelayContinue(dispatch, tour, tour?.openTour, index);
};
These are the 3 I currently have but I will be adding a few more I just want to figure out reducing the redundancy before I keep going
this part isn't really needed but I'll put JoyRideDelayContinue function below just in case
export const JoyRideDelayContinue = (
dispatch: any,
tour: any,
tourType: string,
stepIndex: number
) => {
setTimeout(() => {
if (tour && tour.openTour === tourType) {
dispatch({ type: 'SET_OPEN_TOUR', payload: '' });
dispatch({
type: 'PROGRESS_NEXT_OR_PREV',
payload: { type: tourType, stepIndex: stepIndex }
});
setTimeout(
() => dispatch({ type: 'SET_OPEN_TOUR', payload: tourType }),
500
);
}
}, 1000);
};
arrTourOver,arrStepOver,arrTourResize, etc supposed to by dynamic? Or are those always the values for them?arrTour...is always a list of strings andarrStep...is always a list integers, you can combine yourarrTour...into a dictionary/array and likewise forarrStep.... 2. Is there a reason why yourarr...are arrays? A set might be better if you only need to check containment. 3. You could very well have a wrapper that accepts the necessary parameters along with anoptionsone, which will checks the necessaryarrTour...andarrStep.... 4. Consider posting in codereview.stackexchange.com since you're looking to improve your code, not necessarily the algorithm.