diff --git a/src/hooks/useWizard.ts b/src/hooks/useWizard.ts new file mode 100644 index 000000000..963f4de96 --- /dev/null +++ b/src/hooks/useWizard.ts @@ -0,0 +1,47 @@ +import { useState } from "react"; + +export interface UseWizardOpts { + next: () => void; + prev: () => void; + set: (step: T) => void; +} +export type UseWizard = [T, UseWizardOpts]; +function useWizard(initial: T, steps: T[]): UseWizard { + const [stepIndex, setStepIndex] = useState(steps.indexOf(initial)); + + function next() { + if (stepIndex === steps.length - 1) { + console.error("This is the last step"); + } else { + setStepIndex(stepIndex + 1); + } + } + + function prev() { + if (stepIndex === 0) { + console.error("This is the first step"); + } else { + setStepIndex(stepIndex - 1); + } + } + + function set(step: T) { + const newStepIndex = steps.findIndex(s => s === step); + if (newStepIndex === -1) { + console.error("Step does not exist"); + } else { + setStepIndex(newStepIndex); + } + } + + return [ + steps[stepIndex], + { + next, + prev, + set + } + ]; +} + +export default useWizard; diff --git a/src/products/components/ProductVariantCreateDialog/ProductVariantCreateContent.tsx b/src/products/components/ProductVariantCreateDialog/ProductVariantCreateContent.tsx index 02bc0ae66..2b0fae0b1 100644 --- a/src/products/components/ProductVariantCreateDialog/ProductVariantCreateContent.tsx +++ b/src/products/components/ProductVariantCreateDialog/ProductVariantCreateContent.tsx @@ -62,7 +62,7 @@ const ProductVariantCreateContent: React.FC =
- {step === "values" && ( + {step === ProductVariantCreateStep.values && ( = } /> )} - {step === "prices" && ( + {step === ProductVariantCreateStep.prices && ( = } /> )} - {step === "summary" && ( + {step === ProductVariantCreateStep.summary && ( attribute.values.length > 0); - case "prices": + case ProductVariantCreateStep.prices: if (data.price.all) { if (data.price.value === "") { return false; @@ -69,7 +70,7 @@ function canHitNext( } return true; - case "summary": + case ProductVariantCreateStep.summary: return data.variants.every(variant => variant.sku !== ""); default: @@ -88,9 +89,7 @@ export interface ProductVariantCreateDialogProps onSubmit: (data: ProductVariantBulkCreateInput[]) => void; } -const ProductVariantCreateDialog: React.FC< - ProductVariantCreateDialogProps -> = props => { +const ProductVariantCreateDialog: React.FC = props => { const { attributes, defaultPrice, @@ -101,29 +100,13 @@ const ProductVariantCreateDialog: React.FC< ...contentProps } = props; const classes = useStyles(props); - const [step, setStep] = React.useState("values"); - - function handleNextStep() { - switch (step) { - case "values": - setStep("prices"); - break; - case "prices": - setStep("summary"); - break; - } - } - - function handlePrevStep() { - switch (step) { - case "prices": - setStep("values"); - break; - case "summary": - setStep("prices"); - break; - } - } + const [step, { next, prev, set: setStep }] = useWizard< + ProductVariantCreateStep + >(ProductVariantCreateStep.values, [ + ProductVariantCreateStep.values, + ProductVariantCreateStep.prices, + ProductVariantCreateStep.summary + ]); const [data, dispatchFormDataAction] = React.useReducer( reduceProductVariantCreateFormData, @@ -141,7 +124,7 @@ const ProductVariantCreateDialog: React.FC< useModalDialogOpen(open, { onClose: () => { reloadForm(); - setStep("values"); + setStep(ProductVariantCreateStep.values); } }); @@ -171,25 +154,21 @@ const ProductVariantCreateDialog: React.FC<
- {step !== "values" && ( - )} - {step !== "summary" ? ( + {step !== ProductVariantCreateStep.summary ? ( diff --git a/src/products/components/ProductVariantCreateDialog/ProductVariantCreateTabs.tsx b/src/products/components/ProductVariantCreateDialog/ProductVariantCreateTabs.tsx index 40f03e03f..039c5b8e0 100644 --- a/src/products/components/ProductVariantCreateDialog/ProductVariantCreateTabs.tsx +++ b/src/products/components/ProductVariantCreateDialog/ProductVariantCreateTabs.tsx @@ -17,21 +17,21 @@ function getSteps(intl: IntlShape): Step[] { defaultMessage: "Select Values", description: "attribute values, variant creation step" }), - value: "values" + value: ProductVariantCreateStep.values }, { label: intl.formatMessage({ defaultMessage: "Prices and SKU", description: "variant creation step" }), - value: "prices" + value: ProductVariantCreateStep.prices }, { label: intl.formatMessage({ defaultMessage: "Summary", description: "variant creation step" }), - value: "summary" + value: ProductVariantCreateStep.summary } ]; } @@ -71,9 +71,7 @@ export interface ProductVariantCreateTabsProps { onStepClick: (step: ProductVariantCreateStep) => void; } -const ProductVariantCreateTabs: React.FC< - ProductVariantCreateTabsProps -> = props => { +const ProductVariantCreateTabs: React.FC = props => { const { step: currentStep, onStepClick } = props; const classes = useStyles(props); const intl = useIntl(); diff --git a/src/products/components/ProductVariantCreateDialog/types.ts b/src/products/components/ProductVariantCreateDialog/types.ts index 576a569aa..f8b088f0a 100644 --- a/src/products/components/ProductVariantCreateDialog/types.ts +++ b/src/products/components/ProductVariantCreateDialog/types.ts @@ -1 +1,5 @@ -export type ProductVariantCreateStep = "values" | "prices" | "summary"; +export enum ProductVariantCreateStep { + values, + prices, + summary +} diff --git a/src/products/views/ProductMultipleVariantCreate/ProductMultipleVariantCreate.tsx b/src/products/views/ProductMultipleVariantCreate/ProductMultipleVariantCreate.tsx new file mode 100644 index 000000000..e69de29bb diff --git a/src/products/views/ProductMultipleVariantCreate/index.ts b/src/products/views/ProductMultipleVariantCreate/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/products/views/ProductMultipleVariantCreate/paginate.ts b/src/products/views/ProductMultipleVariantCreate/paginate.ts new file mode 100644 index 000000000..e69de29bb