浏览代码

handle step change in order details page

Shahed nasser 1 年之前
父节点
当前提交
7f4c28d6a5

+ 1 - 1
src/admin/components/onboarding-flow/nextjs/orders/order-detail.tsx

@@ -86,7 +86,7 @@ const OrderDetailNextjs = () => {
             </div>
             </div>
             <div>
             <div>
               <Text size="xsmall" weight="plus" className="mb-1 text-ui-fg-base">
               <Text size="xsmall" weight="plus" className="mb-1 text-ui-fg-base">
-                Learn more about the Next.js Storefront
+                Build with the Next.js Storefront
               </Text>
               </Text>
               <Text size="small">
               <Text size="small">
                 Learn about the Next.js starter storefront’s features and how to customize it.
                 Learn about the Next.js starter storefront’s features and how to customize it.

+ 1 - 1
src/admin/components/onboarding-flow/nextjs/products/product-detail.tsx

@@ -12,7 +12,7 @@ const ProductDetailNextjs = ({ onNext, isComplete, data }: StepContentProps) =>
         You can preview your product using Medusa's Store APIs. You can copy any
         You can preview your product using Medusa's Store APIs. You can copy any
         of the following code snippets to try it out.
         of the following code snippets to try it out.
       </Text>
       </Text>
-      <div className="flex gap-2">
+      <div className="flex gap-2 mt-6">
         <a
         <a
           href={`http://localhost:8000/products/${product?.handle}`}
           href={`http://localhost:8000/products/${product?.handle}`}
           target="_blank"
           target="_blank"

+ 51 - 22
src/admin/widgets/onboarding-flow/onboarding-flow.tsx

@@ -107,11 +107,15 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "create_product_nextjs",
               id: "create_product_nextjs",
               title: "Create Product",
               title: "Create Product",
               component: ProductsListNextjs,
               component: ProductsListNextjs,
-              onNext: (product: Product, withOnComplete = true) => {
+              onNext: (product: Product) => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "create_product_nextjs",
                   step_id: "create_product_nextjs",
                   extraData: { product_id: product.id },
                   extraData: { product_id: product.id },
-                  onComplete: withOnComplete ? () => navigate(`/a/products/${product.id}`) : undefined,
+                  onComplete: () => {
+                    if (!location.pathname.startsWith(`/a/products/${product.id}`)) {
+                      navigate(`/a/products/${product.id}`)
+                    }
+                  },
                 });
                 });
               },
               },
             },
             },
@@ -119,10 +123,10 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "preview_product_nextjs",
               id: "preview_product_nextjs",
               title: "Preview Product in your Next.js Storefront",
               title: "Preview Product in your Next.js Storefront",
               component: ProductDetailNextjs,
               component: ProductDetailNextjs,
-              onNext: (withOnComplete = true) => {
+              onNext: () => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "preview_product_nextjs",
                   step_id: "preview_product_nextjs",
-                  onComplete: withOnComplete ? () => navigate(`/a/orders`) : undefined,
+                  onComplete: () => navigate(`/a/orders`),
                 });
                 });
               },
               },
             },
             },
@@ -130,14 +134,14 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "create_order_nextjs",
               id: "create_order_nextjs",
               title: "Create an Order using your Next.js Storefront",
               title: "Create an Order using your Next.js Storefront",
               component: OrdersListNextjs,
               component: OrdersListNextjs,
-              onNext: (order: Order, withOnComplete = true) => {
+              onNext: (order: Order) => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "create_order_nextjs",
                   step_id: "create_order_nextjs",
-                  onComplete: withOnComplete ? () => {
+                  onComplete: () => {
                     if (!location.pathname.startsWith(`/a/orders/${order.id}`)) {
                     if (!location.pathname.startsWith(`/a/orders/${order.id}`)) {
                       navigate(`/a/orders/${order.id}`)
                       navigate(`/a/orders/${order.id}`)
                     }
                     }
-                  } : undefined,
+                  },
                 });
                 });
               },
               },
             },
             },
@@ -153,11 +157,15 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "create_product",
               id: "create_product",
               title: "Create Product",
               title: "Create Product",
               component: ProductsListDefault,
               component: ProductsListDefault,
-              onNext: (product: Product, withOnComplete = true) => {
+              onNext: (product: Product) => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "create_product",
                   step_id: "create_product",
                   extraData: { product_id: product.id },
                   extraData: { product_id: product.id },
-                  onComplete: withOnComplete ? () => navigate(`/a/products/${product.id}`) : undefined,
+                  onComplete: () => {
+                    if (!location.pathname.startsWith(`/a/products/${product.id}`)) {
+                      navigate(`/a/products/${product.id}`)
+                    }
+                  },
                 });
                 });
               },
               },
             },
             },
@@ -165,10 +173,10 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "preview_product",
               id: "preview_product",
               title: "Preview Product",
               title: "Preview Product",
               component: ProductDetailDefault,
               component: ProductDetailDefault,
-              onNext: (withOnComplete = true) => {
+              onNext: () => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "preview_product",
                   step_id: "preview_product",
-                  onComplete: withOnComplete ? () => navigate(`/a/orders`) : undefined,
+                  onComplete: () => navigate(`/a/orders`),
                 });
                 });
               },
               },
             },
             },
@@ -176,10 +184,14 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
               id: "create_order",
               id: "create_order",
               title: "Create an Order",
               title: "Create an Order",
               component: OrdersListDefault,
               component: OrdersListDefault,
-              onNext: (order: Order, withOnComplete) => {
+              onNext: (order: Order) => {
                 setStepComplete({
                 setStepComplete({
                   step_id: "create_order",
                   step_id: "create_order",
-                  onComplete: withOnComplete ? () => navigate(`/a/orders/${order.id}`) : undefined,
+                  onComplete: () => {
+                    if (!location.pathname.startsWith(`/a/orders/${order.id}`)) {
+                      navigate(`/a/orders/${order.id}`)
+                    }
+                  },
                 });
                 });
               },
               },
             },
             },
@@ -191,7 +203,7 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
           ]
           ]
       }
       }
     }
     }
-  }, [])
+  }, [location.pathname])
 
 
   // used to retrieve the index of a step by its ID
   // used to retrieve the index of a step by its ID
   const findStepIndex = useCallback((step_id: STEP_ID) => {
   const findStepIndex = useCallback((step_id: STEP_ID) => {
@@ -242,6 +254,11 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
       currentStep === "create_product_nextjs"
       currentStep === "create_product_nextjs"
   }, [currentStep])
   }, [currentStep])
 
 
+  const isOrderCreateStep = useMemo(() => {
+    return currentStep === "create_order" || 
+      currentStep === "create_order_nextjs"
+  }, [currentStep])
+
   // used to change the open step when the current
   // used to change the open step when the current
   // step is retrieved from custom endpoints
   // step is retrieved from custom endpoints
   useEffect(() => {
   useEffect(() => {
@@ -250,6 +267,26 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
     if (findStepIndex(currentStep) === steps.length - 1) setCompleted(true);
     if (findStepIndex(currentStep) === steps.length - 1) setCompleted(true);
   }, [currentStep, findStepIndex]);
   }, [currentStep, findStepIndex]);
 
 
+  // used to check if the user created a product and has entered its details page
+  // the step is changed to the next one
+  useEffect(() => {
+    if (location.pathname.startsWith("/a/products/prod_") && isProductCreateStep && "product" in props) {
+      // change to the preview product step
+      const currentStepIndex = findStepIndex(currentStep)
+      steps[currentStepIndex].onNext?.(props.product)
+    }
+  }, [location.pathname, isProductCreateStep])
+
+  // used to check if the user created an order and has entered its details page
+  // the step is changed to the next one.
+  useEffect(() => {
+    if (location.pathname.startsWith("/a/orders/order_") && isOrderCreateStep && "order" in props) {
+      // change to the preview product step
+      const currentStepIndex = findStepIndex(currentStep)
+      steps[currentStepIndex].onNext?.(props.order)
+    }
+  }, [location.pathname, isOrderCreateStep])
+
   // used to check if the `onboarding_step` path
   // used to check if the `onboarding_step` path
   // parameter is passed and, if so, moves to that step
   // parameter is passed and, if so, moves to that step
   // only if it's the next step and its necessary data is passed
   // only if it's the next step and its necessary data is passed
@@ -274,14 +311,6 @@ const OnboardingFlow = (props: OnboardingWidgetProps) => {
     }
     }
   }, [searchParams, openStep, getOnboardingParamStepData])
   }, [searchParams, openStep, getOnboardingParamStepData])
 
 
-  useEffect(() => {
-    if (location.pathname.startsWith("/a/products/prod_") && isProductCreateStep && "product" in props) {
-      // change to the preview product step
-      const currentStepIndex = findStepIndex(currentStep)
-      steps[currentStepIndex].onNext?.(props.product, false)
-    }
-  }, [location.pathname, isProductCreateStep])
-
   if (
   if (
     !isLoading &&
     !isLoading &&
     data?.status?.is_complete &&
     data?.status?.is_complete &&