1 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
2 import { useLoaderData, useParams } from "react-router-dom"
3
4 import { useProductVariant } from "../../../hooks/api/products"
5
6 import { TwoColumnPageSkeleton } from "../../../components/common/skeleton"
7 import { LayoutComposer, detailPageDefaultEntries } from "../../../components/layout-composer"
8 import { VariantGeneralSection } from "./components/variant-general-section"
9 import {
10 InventorySectionPlaceholder,
11 VariantInventorySection,
12 } from "./components/variant-inventory-section"
13 import { VariantMediaSection } from "./components/variant-media-section"
14 import { VariantPricesSection } from "./components/variant-prices-section"
15 import { ExtendedVariant, VARIANT_DETAIL_FIELDS } from "./constants"
16 import { variantLoader } from "./loader"
17
18 export const ProductVariantDetail = () => {
19 const initialData = useLoaderData() as Awaited<
20 ReturnType<typeof variantLoader>
21 >
22
23 const { id, variant_id } = useParams()
24 const { variant, isLoading, isError, error } = useProductVariant(
25 id!,
26 variant_id!,
27 { fields: VARIANT_DETAIL_FIELDS },
28 {
29 initialData,
30 }
31 )
32
33 if (isLoading || !variant) {
34 return (
35 <TwoColumnPageSkeleton
36 mainSections={2}
37 sidebarSections={1}
38 showJSON
39 showMetadata
40 />
41 )
42 }
43
44 if (isError) {
45 throw error
46 }
47
48 return (
49 <LayoutComposer
50 widgetsZonePrefix="product_variant.details"
51 preferredLayoutId={CORE_LAYOUT_IDS.TWO_COLUMN}
52 data={variant}
53 sections={{
54 main: (
55 <>
56 <LayoutComposer.Entry id="VariantGeneralSection">
57 <VariantGeneralSection variant={variant as ExtendedVariant} />
58 </LayoutComposer.Entry>
59 <LayoutComposer.Entry id="VariantMediaSection">
60 <VariantMediaSection variant={variant as ExtendedVariant} />
61 </LayoutComposer.Entry>
62 <LayoutComposer.Entry id="VariantInventorySection">
63 {!variant.manage_inventory ? (
64 <InventorySectionPlaceholder />
65 ) : (
66 <VariantInventorySection
67 inventoryItems={(variant.inventory_items ?? [])
68 .filter((i) => i.inventory)
69 .map((i) => {
70 return {
71 ...i.inventory!,
72 required_quantity: i.required_quantity,
73 variant,
74 }
75 })}
76 />
77 )}
78 </LayoutComposer.Entry>
79 {detailPageDefaultEntries(variant)}
80 </>
81 ),
82 side: (
83 <>
84 <LayoutComposer.Entry id="VariantPricesSection">
85 <VariantPricesSection variant={variant as ExtendedVariant} />
86 </LayoutComposer.Entry>
87 </>
88 ),
89 }}
90 />
91 )
92 }