|
1 | | -import { UploadFlow } from "./UploadFlow" |
| 1 | +import { StepState, StepType, UploadFlow } from "./UploadFlow" |
2 | 2 | import { ModalHeader } from "@chakra-ui/react" |
3 | 3 | import { useSteps, Step, Steps as Stepper } from "chakra-ui-steps" |
4 | 4 | import { CgCheck } from "react-icons/cg" |
| 5 | + |
5 | 6 | import { useRsi } from "../hooks/useRsi" |
6 | | -import { useRsiInitialStep } from "../hooks/useRsiInitialStep" |
| 7 | +import { useRef, useState } from "react" |
| 8 | +import { steps, stepTypeToStepIndex, stepIndexToStepType } from "../utils/steps" |
7 | 9 |
|
8 | 10 | const CheckIcon = ({ color }: { color: string }) => <CgCheck size="2.25rem" color={color} /> |
9 | 11 |
|
10 | 12 | export const Steps = () => { |
11 | | - const { initialStepState, translations } = useRsi() |
| 13 | + const { initialStepState, translations, isNavigationEnabled } = useRsi() |
12 | 14 |
|
13 | | - const { steps, initialStep } = useRsiInitialStep(initialStepState?.type) |
| 15 | + const initialStep = stepTypeToStepIndex(initialStepState?.type) |
14 | 16 |
|
15 | | - const { nextStep, activeStep } = useSteps({ |
| 17 | + const { nextStep, activeStep, setStep } = useSteps({ |
16 | 18 | initialStep, |
17 | 19 | }) |
18 | 20 |
|
| 21 | + const [state, setState] = useState<StepState>(initialStepState || { type: StepType.upload }) |
| 22 | + |
| 23 | + const history = useRef<StepState[]>([]) |
| 24 | + |
| 25 | + const onClickStep = (stepIndex: number) => { |
| 26 | + const type = stepIndexToStepType(stepIndex) |
| 27 | + const historyIdx = history.current.findIndex((v) => v.type === type) |
| 28 | + if (historyIdx === -1) return |
| 29 | + const nextHistory = history.current.slice(0, historyIdx + 1) |
| 30 | + history.current = nextHistory |
| 31 | + setState(nextHistory[nextHistory.length - 1]) |
| 32 | + setStep(stepIndex) |
| 33 | + } |
| 34 | + |
| 35 | + const onBack = () => { |
| 36 | + onClickStep(Math.max(activeStep - 1, 0)) |
| 37 | + } |
| 38 | + |
| 39 | + const onNext = (v: StepState) => { |
| 40 | + history.current.push(state) |
| 41 | + setState(v) |
| 42 | + v.type !== StepType.selectSheet && nextStep() |
| 43 | + } |
| 44 | + |
19 | 45 | return ( |
20 | 46 | <> |
21 | 47 | <ModalHeader display={["none", "none", "block"]}> |
22 | | - <Stepper activeStep={activeStep} checkIcon={CheckIcon}> |
| 48 | + <Stepper |
| 49 | + activeStep={activeStep} |
| 50 | + checkIcon={CheckIcon} |
| 51 | + onClickStep={isNavigationEnabled ? onClickStep : undefined} |
| 52 | + responsive={false} |
| 53 | + > |
23 | 54 | {steps.map((key) => ( |
24 | 55 | <Step label={translations[key].title} key={key} /> |
25 | 56 | ))} |
26 | 57 | </Stepper> |
27 | 58 | </ModalHeader> |
28 | | - <UploadFlow nextStep={nextStep} /> |
| 59 | + <UploadFlow state={state} onNext={onNext} onBack={isNavigationEnabled ? onBack : undefined} /> |
29 | 60 | </> |
30 | 61 | ) |
31 | 62 | } |
0 commit comments