Skip to content

Instantly share code, notes, and snippets.

@JohnRandom
Last active June 25, 2022 07:06
Show Gist options
  • Save JohnRandom/8f5f1112c1d393f9ebd12da976eb80b0 to your computer and use it in GitHub Desktop.
Save JohnRandom/8f5f1112c1d393f9ebd12da976eb80b0 to your computer and use it in GitHub Desktop.
let step = null;
const middleware = store => next => action => {
switch (action.type) {
case 'SET_ONBOARDING_STEP':
step = action.payload;
const { url } = steps[step];
// Make sure we "replace" instead of "push" when we have an on-boarding
// exclusive URL.
const redirectMethod = exclusiveUrls.indexOf(url) >= 0 ? replace : push;
store.dispatch( redirectMethod(url) );
break;
case 'ADD_ONBOARDING_MILESTONE':
milestones.push(action.payload);
break;
case 'REMOVE_ONBOARDING_MILESTONE':
const idx = milestones.indexOf(action.payload);
if (idx >= 0) milestones.splice(idx, 1);
break;
case 'INSERT_ONBOARDING_STEP':
const { stepBefore, step, stepLabel } = action.payload;
steps[stepBefore].nextStep = stepLabel;
steps[stepLabel] = step;
break;
case 'REMOVE_ONBOARDING_STEP':
const { stepLabel, newNextStep } = action.payload;
// Rewrite all pointers to the deleted step with the new next step label.
Object.keys(steps).forEach((label) => {
if (steps[label].nextStep === stepLabel) steps[label].nextStep = newNextStep;
});
delete steps[stepLabel];
break;
case 'SET_FINAL_ONBOARDIND_ACTION':
finalAction = action.payload;
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment