From bf1700e43a60b638efa3e320cab7b7604993dce2 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 18 Jul 2022 14:57:33 +0200 Subject: [PATCH] Convert product category form to RHF --- .../store/manage-product-category.tsx | 2 +- .../components/store/product-categories.tsx | 3 +- .../store/product-category-form.tsx | 35 ++++++++++++++----- config/locales/app.admin.en.yml | 12 ++++--- config/locales/app.admin.fr.yml | 12 ++++--- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/frontend/src/javascript/components/store/manage-product-category.tsx b/app/frontend/src/javascript/components/store/manage-product-category.tsx index 52dcb1557..28b99b63f 100644 --- a/app/frontend/src/javascript/components/store/manage-product-category.tsx +++ b/app/frontend/src/javascript/components/store/manage-product-category.tsx @@ -15,7 +15,7 @@ interface ManageProductCategoryProps { /** * This component shows a button. - * When clicked, we show a modal dialog allowing to fill the parameters of a product category (create new or update existing). + * When clicked, we show a modal dialog allowing to fill the parameters of a product category. */ export const ManageProductCategory: React.FC = ({ productCategories, productCategory, action, onSuccess, onError }) => { const { t } = useTranslation('admin'); diff --git a/app/frontend/src/javascript/components/store/product-categories.tsx b/app/frontend/src/javascript/components/store/product-categories.tsx index 17e72baaf..977bbe01f 100644 --- a/app/frontend/src/javascript/components/store/product-categories.tsx +++ b/app/frontend/src/javascript/components/store/product-categories.tsx @@ -18,7 +18,8 @@ interface ProductCategoriesProps { } /** - * This component shows a Tree list of all Product's Categories + * This component shows a list of all product categories and offer to manager them + * by creating, deleting, modifying and reordering each product categories. */ const ProductCategories: React.FC = ({ onSuccess, onError }) => { const { t } = useTranslation('admin'); diff --git a/app/frontend/src/javascript/components/store/product-category-form.tsx b/app/frontend/src/javascript/components/store/product-category-form.tsx index 339765be7..9f1f31b54 100644 --- a/app/frontend/src/javascript/components/store/product-category-form.tsx +++ b/app/frontend/src/javascript/components/store/product-category-form.tsx @@ -29,7 +29,7 @@ interface ProductCategoryFormProps { export const ProductCategoryForm: React.FC = ({ action, productCategories, productCategory, onSuccess, onError }) => { const { t } = useTranslation('admin'); - const { register, watch, setValue, control, handleSubmit } = useForm({ defaultValues: { ...productCategory } }); + const { register, watch, setValue, control, handleSubmit, formState } = useForm({ defaultValues: { ...productCategory } }); // filter all first level product categorie const parents = productCategories.filter(c => !c.parent_id); @@ -53,15 +53,26 @@ export const ProductCategoryForm: React.FC = ({ action }); return () => subscription.unsubscribe(); }, [watch]); + // Check slug pattern + // Only lowercase alphanumeric groups of characters separated by an hyphen + const slugPattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/g; // Form submit const onSubmit: SubmitHandler = (category: ProductCategory) => { switch (action) { case 'create': - console.log('create:', category); + ProductCategoryAPI.create(category).then(() => { + onSuccess(t('app.admin.store.product_category_form.create.success')); + }).catch((error) => { + onError(t('app.admin.store.product_category_form.create.error') + error); + }); break; case 'update': - console.log('update:', category); + ProductCategoryAPI.update(category).then(() => { + onSuccess(t('app.admin.store.product_category_form.update.success')); + }).catch((error) => { + onError(t('app.admin.store.product_category_form.update.error') + error); + }); break; case 'delete': ProductCategoryAPI.destroy(category.id).then(() => { @@ -84,13 +95,21 @@ export const ProductCategoryForm: React.FC = ({ action : <> + register={register} + rules={{ required: `${t('app.admin.store.product_category_form.required')}` }} + formState={formState} + label={t('app.admin.store.product_category_form.name')} + defaultValue={productCategory?.name || ''} />