Skip to content

Commit

Permalink
feat: Add product routes and components to v2 in admin-next
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed Apr 5, 2024
1 parent a0005fa commit 4b4f7c0
Show file tree
Hide file tree
Showing 69 changed files with 201 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@
},
"weight": {
"label": "Weight"
},
"options": {
"label": "Product options",
"hint": "Options are used to define the color, size, etc. of the product",
"optionTitle": "Option title",
"variations": "Variations (comma-separated)"
},
"variants": {
"label": "Product variants",
"hint": "Variants left unchecked won't be created, This ranking will affect how the variants are ranked in your frontend."
},
"mid_code": {
"label": "Mid code"
},
"hs_code": {
"label": "HS code"
}
},
"variant": {
Expand Down
62 changes: 0 additions & 62 deletions packages/admin-next/dashboard/src/providers/router-provider/v1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type {
AdminDraftOrdersRes,
AdminGiftCardsRes,
AdminOrdersRes,
AdminProductsRes,
AdminPublishableApiKeysRes,
AdminRegionsRes,
AdminSalesChannelsRes,
AdminUserRes,
Expand Down Expand Up @@ -193,66 +191,6 @@ export const v1Routes: RouteObject[] = [
},
],
},
{
path: "/products",
handle: {
crumb: () => "Products",
},
children: [
{
path: "",
lazy: () => import("../../routes/products/product-list"),
children: [
{
path: "create",
lazy: () => import("../../routes/products/product-create"),
},
],
},
{
path: ":id",
lazy: () => import("../../routes/products/product-detail"),
handle: {
crumb: (data: AdminProductsRes) => data.product.title,
},
children: [
{
path: "edit",
lazy: () => import("../../routes/products/product-edit"),
},
{
path: "sales-channels",
lazy: () =>
import("../../routes/products/product-sales-channels"),
},
{
path: "attributes",
lazy: () =>
import("../../routes/products/product-attributes"),
},
{
path: "options/create",
lazy: () =>
import("../../routes/products/product-create-option"),
},
{
path: "options/:option_id/edit",
lazy: () =>
import("../../routes/products/product-edit-option"),
},
{
path: "media",
lazy: () => import("../../routes/products/product-media"),
},
{
path: "variants/:variant_id/edit",
lazy: () =>
import("../../routes/products/product-edit-variant"),
},
],
},
],
},
{
path: "/categories",
handle: {
Expand Down
67 changes: 64 additions & 3 deletions packages/admin-next/dashboard/src/providers/router-provider/v2.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Navigate, RouteObject, useLocation } from "react-router-dom"
import { SalesChannelDTO, UserDTO } from "@medusajs/types"
import { SalesChannelDTO, UserDTO, ApiKeyDTO } from "@medusajs/types"

import { AdminCollectionsRes } from "@medusajs/medusa"
import { AdminCollectionsRes, AdminProductsRes } from "@medusajs/medusa"
import { ErrorBoundary } from "../../components/error/error-boundary"
import { MainLayout } from "../../components/layout-v2/main-layout"
import { Outlet } from "react-router-dom"
import { SearchProvider } from "../search-provider"
import { SettingsLayout } from "../../components/layout/settings-layout"
import { SidebarProvider } from "../sidebar-provider"
import { ApiKeyDTO } from "@medusajs/types"
import { Spinner } from "@medusajs/icons"
import { useV2Session } from "../../lib/api-v2"

Expand Down Expand Up @@ -67,6 +66,68 @@ export const v2Routes: RouteObject[] = [
path: "/",
element: <MainLayout />,
children: [
{
path: "/products",
handle: {
crumb: () => "Products",
},
children: [
{
path: "",
lazy: () => import("../../v2-routes/products/product-list"),
children: [
{
path: "create",
lazy: () =>
import("../../v2-routes/products/product-create"),
},
],
},
{
path: ":id",
lazy: () => import("../../v2-routes/products/product-detail"),
handle: {
crumb: (data: AdminProductsRes) => data.product.title,
},
children: [
{
path: "edit",
lazy: () => import("../../v2-routes/products/product-edit"),
},
{
path: "sales-channels",
lazy: () =>
import("../../v2-routes/products/product-sales-channels"),
},
{
path: "attributes",
lazy: () =>
import("../../v2-routes/products/product-attributes"),
},
{
path: "options/create",
lazy: () =>
import("../../v2-routes/products/product-create-option"),
},
{
path: "options/:option_id/edit",
lazy: () =>
import("../../v2-routes/products/product-edit-option"),
},
{
path: "media",
lazy: () =>
import("../../v2-routes/products/product-media"),
},
{
path: "variants/:variant_id/edit",
lazy: () =>
import("../../v2-routes/products/product-edit-variant"),
},
],
},
],
},
{
path: "/orders",
handle: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,18 @@ export const CreateProductDetails = ({ form }: CreateProductPropsProps) => {
<Form.Field
control={form.control}
name="type_id"
render={({ field }) => {
render={({ field: { onChange, ...field } }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.type.label")}
</Form.Label>
<Form.Control>
<Select disabled={isLoadingTypes} {...field}>
<Select
disabled={isLoadingTypes}
{...field}
onValueChange={onChange}
>
<Select.Trigger ref={field.ref}>
<Select.Value />
</Select.Trigger>
Expand All @@ -229,14 +233,18 @@ export const CreateProductDetails = ({ form }: CreateProductPropsProps) => {
<Form.Field
control={form.control}
name="collection_id"
render={({ field }) => {
render={({ field: { onChange, ...field } }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.collection.label")}
</Form.Label>
<Form.Control>
<Select disabled={isLoadingCollections} {...field}>
<Select
disabled={isLoadingCollections}
{...field}
onValueChange={onChange}
>
<Select.Trigger ref={field.ref}>
<Select.Value />
</Select.Trigger>
Expand Down Expand Up @@ -351,6 +359,48 @@ export const CreateProductDetails = ({ form }: CreateProductPropsProps) => {
</div>
<div id="variants" className="flex flex-col gap-y-8">
<Heading level="h2">{t("products.variants")}</Heading>
<div className="grid grid-cols-1 gap-x-4 gap-y-8">
<Form.Field
control={form.control}
name="options"
render={({ field }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.options.label")}
</Form.Label>
<Form.Hint>
{t("products.fields.options.hint")}
</Form.Hint>
<Form.Control>
<></>
</Form.Control>
</Form.Item>
)
}}
/>
</div>
<div className="grid grid-cols-1 gap-x-4 gap-y-8">
<Form.Field
control={form.control}
name="variants"
render={({ field }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.variants.label")}
</Form.Label>
<Form.Hint>
{t("products.fields.variants.hint")}
</Form.Hint>
<Form.Control>
<></>
</Form.Control>
</Form.Item>
)
}}
/>
</div>
</div>

<div id="attributes" className="flex flex-col gap-y-8">
Expand Down Expand Up @@ -454,8 +504,39 @@ export const CreateProductDetails = ({ form }: CreateProductPropsProps) => {
)
}}
/>
<Form.Field
control={form.control}
name="mid_code"
render={({ field }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.mid_code.label")}
</Form.Label>
<Form.Control>
<Input {...field} />
</Form.Control>
</Form.Item>
)
}}
/>
<Form.Field
control={form.control}
name="hs_code"
render={({ field }) => {
return (
<Form.Item>
<Form.Label optional>
{t("products.fields.hs_code.label")}
</Form.Label>
<Form.Control>
<Input {...field} />
</Form.Control>
</Form.Item>
)
}}
/>
</div>
{/* TODO: Add missing attribute fields */}
</div>
<div id="media" className="flex flex-col gap-y-8">
<Heading level="h2">{t("products.media.label")}</Heading>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const CreateProductSchema = zod.object({
weight: zod.string().optional(),
mid_code: zod.string().optional(),
hs_code: zod.string().optional(),
options: zod.array(
zod.object({
title: zod.string(),
values: zod.array(zod.string()),
})
),
variants: zod.array(
zod.object({
variant_rank: zod.number(),
Expand All @@ -55,6 +61,7 @@ export const CreateProductForm = () => {
discountable: true,
tags: [],
sales_channels: [],
options: [],
variants: [],
images: [],
},
Expand All @@ -64,24 +71,25 @@ export const CreateProductForm = () => {
const { mutateAsync, isLoading } = useAdminCreateProduct()

const handleSubmit = form.handleSubmit(async (values) => {
await mutateAsync(
{
...values,
is_giftcard: false,
tags: values.tags?.map((tag) => ({ value: tag })),
sales_channels: values.sales_channels?.map((sc) => ({ id: sc })),
width: values.width ? parseFloat(values.width) : undefined,
length: values.length ? parseFloat(values.length) : undefined,
height: values.height ? parseFloat(values.height) : undefined,
weight: values.weight ? parseFloat(values.weight) : undefined,
variants: values.variants.map((v) => ({ title: "", prices: [] })),
const reqData = {
...values,
is_giftcard: false,
tags: values.tags?.map((tag) => ({ value: tag })),
sales_channels: values.sales_channels?.map((sc) => ({ id: sc })),
width: values.width ? parseFloat(values.width) : undefined,
length: values.length ? parseFloat(values.length) : undefined,
height: values.height ? parseFloat(values.height) : undefined,
weight: values.weight ? parseFloat(values.weight) : undefined,
variants: values.variants.map((v) => ({ title: "", prices: [] })),
} as any

delete reqData.sales_channels

await mutateAsync(reqData, {
onSuccess: ({ product }) => {
handleSuccess(`../${product.id}`)
},
{
onSuccess: ({ product }) => {
handleSuccess(`../${product.id}`)
},
}
)
})
})

return (
Expand Down
Loading

0 comments on commit 4b4f7c0

Please sign in to comment.