|
@@ -1,14 +1,33 @@
|
|
|
-import React from "react";
|
|
|
-import { useAdminPublishableApiKeys } from "medusa-react";
|
|
|
-import { StepContentProps } from "../../../../widgets/onboarding-flow/onboarding-flow";
|
|
|
-import { Button, CodeBlock, Text } from "@medusajs/ui";
|
|
|
+import React, { useEffect, useMemo } from "react"
|
|
|
+import {
|
|
|
+ useAdminPublishableApiKeys,
|
|
|
+ useAdminCreatePublishableApiKey
|
|
|
+} from "medusa-react"
|
|
|
+import { StepContentProps } from "../../../../widgets/onboarding-flow/onboarding-flow"
|
|
|
+import { Button, CodeBlock, Text } from "@medusajs/ui"
|
|
|
|
|
|
const ProductDetailDefault = ({ onNext, isComplete, data }: StepContentProps) => {
|
|
|
- const { publishable_api_keys: keys, isLoading } = useAdminPublishableApiKeys({
|
|
|
+ const { publishable_api_keys: keys, isLoading, refetch } = useAdminPublishableApiKeys({
|
|
|
offset: 0,
|
|
|
limit: 1,
|
|
|
});
|
|
|
- const api_key = keys?.[0]?.id || "pk_01H0PY648BTMEJR34ZDATXZTD9";
|
|
|
+ const createPublishableApiKey = useAdminCreatePublishableApiKey()
|
|
|
+
|
|
|
+ const api_key = useMemo(() => keys?.[0]?.id || "", [keys])
|
|
|
+ const backendUrl = process.env.MEDUSA_BACKEND_URL || process.env.MEDUSA_ADMIN_BACKEND_URL || "http://location:9000"
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (!isLoading && !keys?.length) {
|
|
|
+ createPublishableApiKey.mutate({
|
|
|
+ "title": "Development"
|
|
|
+ }, {
|
|
|
+ onSuccess: () => {
|
|
|
+ refetch()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }, [isLoading, keys])
|
|
|
+
|
|
|
return (
|
|
|
<div>
|
|
|
<div className="flex flex-col gap-2">
|
|
@@ -24,12 +43,12 @@ const ProductDetailDefault = ({ onNext, isComplete, data }: StepContentProps) =>
|
|
|
{
|
|
|
label: "cURL",
|
|
|
language: "bash",
|
|
|
- code: `curl "http://localhost:9000/store/products/${data?.product_id}" -H "x-publishable-key: ${api_key}"`,
|
|
|
+ code: `curl "${backendUrl}/store/products/${data?.product_id}"${api_key ? ` -H "x-publishable-key: ${api_key}"` : ``}`,
|
|
|
},
|
|
|
{
|
|
|
label: "Medusa JS Client",
|
|
|
language: "js",
|
|
|
- code: `// Install the JS Client in your storefront project: @medusajs/medusa-js\n\nimport Medusa from "@medusajs/medusa-js"\n\nconst medusa = new Medusa({ publishableApiKey: "${api_key}"})\nconst product = await medusa.products.retrieve("${data?.product_id}")\nconsole.log(product.id)`,
|
|
|
+ code: `// Install the JS Client in your storefront project: @medusajs/medusa-js\n\nimport Medusa from "@medusajs/medusa-js"\n\nconst medusa = new Medusa(${api_key ? `{ publishableApiKey: "${api_key}"}` : ``})\nconst product = await medusa.products.retrieve("${data?.product_id}")\nconsole.log(product.id)`,
|
|
|
},
|
|
|
{
|
|
|
label: "Medusa React",
|
|
@@ -49,7 +68,7 @@ const ProductDetailDefault = ({ onNext, isComplete, data }: StepContentProps) =>
|
|
|
</div>
|
|
|
<div className="flex gap-2">
|
|
|
<a
|
|
|
- href={`http://localhost:9000/store/products/${data?.product_id}`}
|
|
|
+ href={`${backendUrl}/store/products/${data?.product_id}`}
|
|
|
target="_blank"
|
|
|
>
|
|
|
<Button variant="secondary" size="base">
|