From 1c9061061716c39519fbcccee1f60bbe6c9ea5c7 Mon Sep 17 00:00:00 2001 From: LULU Date: Thu, 7 Dec 2023 09:51:21 +0000 Subject: [PATCH] feat(admin-ui): metadata for categories in admin ui and refactor to react-hook-form (#5697) * WIP * add metadata to category add and edit ui * remove console.log * lint * remove console.log * requested changes getDefaultCategoryValues util * change placeholder and wrap handleSubmit * Update edit-product-category.tsx --------- Co-authored-by: lulu Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --- .../modals/add-product-category.tsx | 192 ++++++++++++----- .../modals/edit-product-category.tsx | 193 +++++++++++------- .../domain/product-categories/utils/index.tsx | 32 +++ 3 files changed, 287 insertions(+), 130 deletions(-) diff --git a/packages/admin-ui/ui/src/domain/product-categories/modals/add-product-category.tsx b/packages/admin-ui/ui/src/domain/product-categories/modals/add-product-category.tsx index 1175c377bc..84a3fe0d1a 100644 --- a/packages/admin-ui/ui/src/domain/product-categories/modals/add-product-category.tsx +++ b/packages/admin-ui/ui/src/domain/product-categories/modals/add-product-category.tsx @@ -1,5 +1,3 @@ -import { useState } from "react" - import { ProductCategory } from "@medusajs/medusa" import { adminProductCategoryKeys, @@ -17,23 +15,64 @@ import { NextSelect } from "../../../components/molecules/select/next-select" import useNotification from "../../../hooks/use-notification" import { getErrorMessage } from "../../../utils/error-messages" import TreeCrumbs from "../components/tree-crumbs" +import MetadataForm, { + getSubmittableMetadata, + MetadataFormType, +} from "../../../components/forms/general/metadata-form" +import { Controller, useForm } from "react-hook-form" +import { nestedForm } from "../../../utils/nested-form" +import { TFunction } from "i18next" +import { getDefaultCategoryValues } from "../utils" -const visibilityOptions = (t) => [ +export enum CategoryStatus { + Active = "active", + Inactive = "inactive", +} + +export enum CategoryVisibility { + Public = "public", + Private = "private", +} + +const visibilityOptions = ( + t: TFunction<"translation", undefined, "translation"> +) => [ { label: t("modals-public", "Public"), - value: "public", + value: CategoryVisibility.Public, }, - { label: t("modals-private", "Private"), value: "private" }, + { label: t("modals-private", "Private"), value: CategoryVisibility.Private }, ] -const statusOptions = (t) => [ - { label: t("modals-active", "Active"), value: "active" }, - { label: t("modals-inactive", "Inactive"), value: "inactive" }, +const statusOptions = ( + t: TFunction<"translation", undefined, "translation"> +) => [ + { label: t("modals-active", "Active"), value: CategoryStatus.Active }, + { label: t("modals-inactive", "Inactive"), value: CategoryStatus.Inactive }, ] type CreateProductCategoryProps = { closeModal: () => void parentCategory?: ProductCategory + categories: ProductCategory[] +} + + + + +export type CategoryFormData = { + name: string + handle: string | undefined + description: string | undefined + metadata: MetadataFormType + is_active: { + value: CategoryStatus + label: string + } + is_public: { + value: CategoryVisibility + label: string + } } /** @@ -45,23 +84,33 @@ function CreateProductCategory(props: CreateProductCategoryProps) { const notification = useNotification() const queryClient = useQueryClient() - const [name, setName] = useState("") - const [handle, setHandle] = useState("") - const [description, setDescription] = useState("") - const [isActive, setIsActive] = useState(true) - const [isPublic, setIsPublic] = useState(true) + const form = useForm({ + defaultValues: getDefaultCategoryValues(t), + mode: "onChange", + }) + const { + register, + handleSubmit, + watch, + reset, + control, + formState: { errors, isDirty, isValid, isSubmitting }, + } = form + const name = watch("name", "") - const { mutateAsync: createProductCategory } = useAdminCreateProductCategory() + const { mutateAsync: createProductCategory } = + useAdminCreateProductCategory() - const onSubmit = async () => { + const submit = handleSubmit(async (data) => { try { await createProductCategory({ - name, - handle, - description, - is_active: isActive, - is_internal: !isPublic, + name: data.name, + handle: data.handle, + description: data.description, + is_active: data.is_active.value === CategoryStatus.Active, + is_internal: data.is_public.value === CategoryVisibility.Private, parent_category_id: parentCategory?.id ?? null, + metadata: getSubmittableMetadata(data.metadata), }) // TODO: temporary here, investigate why `useAdminCreateProductCategory` doesn't invalidate this await queryClient.invalidateQueries(adminProductCategoryKeys.lists()) @@ -83,7 +132,7 @@ function CreateProductCategory(props: CreateProductCategoryProps) { ) notification(t("modals-error", "Error"), errorMessage, "error") } - } + }) return ( @@ -96,8 +145,8 @@ function CreateProductCategory(props: CreateProductCategoryProps) {