Created
June 21, 2022 19:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Step } from "../../components/Stepper"; | |
import { createCtx } from "./createCtx"; | |
import * as R from "ramda"; | |
export const stepperInitialState: Step[] = [ | |
{ | |
id: "1", | |
name: "Connect", | |
description: "wallet and check network", | |
href: "", | |
status: "current", | |
}, | |
{ | |
id: "2", | |
name: "Checkout", | |
description: "quantity and mint", | |
href: "", | |
status: "upcoming", | |
}, | |
{ | |
id: "3", | |
name: "Review", | |
description: "receipt", | |
href: "", | |
status: "upcoming", | |
}, | |
]; | |
type AppState = typeof stepperInitialState; | |
type Action = | |
| { type: "setSteps"; payload: Step[] } | |
| { | |
type: "setStepComplete"; | |
payload: number; | |
} | |
| { | |
type: "setCurrentStep"; | |
payload: number; | |
}; | |
const setStepComplete = (steps: Step[], completedStepIndex: number): Step[] => { | |
const stepsCopy = R.clone(steps); | |
stepsCopy.map((step, idx) => { | |
if (idx <= completedStepIndex) { | |
step.status = "complete"; | |
} else { | |
step.status = "upcoming"; | |
} | |
}); | |
return stepsCopy; | |
}; | |
const setCurrentStep = (steps: Step[], completedStepIndex: number): Step[] => { | |
const stepsCopy = R.clone(steps); | |
stepsCopy.map((step, idx) => { | |
if (idx === completedStepIndex) { | |
step.status = "current"; | |
} | |
}); | |
return stepsCopy; | |
}; | |
export function stepperReducer(state: AppState, action: Action): AppState { | |
switch (action.type) { | |
case "setSteps": | |
return action.payload; | |
case "setStepComplete": | |
return setStepComplete(state, action.payload); | |
case "setCurrentStep": | |
return setCurrentStep(state, action.payload); | |
default: | |
return state; | |
} | |
} | |
const [ctx, provider] = createCtx(stepperReducer, stepperInitialState); | |
export const StepperContext = ctx; | |
export const StepperProvider = provider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment