From 45bac88b26f562ca7ba6fd66f251c8af148ac446 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 7 Sep 2022 17:28:41 +0200 Subject: [PATCH] (quality) refactored categories sorting + fix ts issues --- .../javascript/components/base/fab-modal.tsx | 2 +- .../payment/stripe/stripe-card-update.tsx | 2 +- .../categories/manage-product-category.tsx | 1 - .../store/categories/product-categories.tsx | 14 ++-------- .../components/store/product-form.tsx | 26 +++++-------------- .../components/store/product-stock-form.tsx | 6 ++--- .../components/store/product-stock-modal.tsx | 3 ++- .../javascript/components/store/products.tsx | 14 ++-------- .../components/user/user-profile-form.tsx | 2 +- app/frontend/src/javascript/lib/product.ts | 21 +++++++++++++++ app/frontend/src/javascript/models/product.ts | 6 ++--- 11 files changed, 43 insertions(+), 54 deletions(-) create mode 100644 app/frontend/src/javascript/lib/product.ts diff --git a/app/frontend/src/javascript/components/base/fab-modal.tsx b/app/frontend/src/javascript/components/base/fab-modal.tsx index 3a091f1dc..84fa446d6 100644 --- a/app/frontend/src/javascript/components/base/fab-modal.tsx +++ b/app/frontend/src/javascript/components/base/fab-modal.tsx @@ -42,7 +42,7 @@ export const FabModal: React.FC = ({ title, isOpen, toggleModal, return ( {closeButton && {t('app.shared.fab_modal.close')}} diff --git a/app/frontend/src/javascript/components/payment/stripe/stripe-card-update.tsx b/app/frontend/src/javascript/components/payment/stripe/stripe-card-update.tsx index ca48eaa79..d6fccc677 100644 --- a/app/frontend/src/javascript/components/payment/stripe/stripe-card-update.tsx +++ b/app/frontend/src/javascript/components/payment/stripe/stripe-card-update.tsx @@ -95,7 +95,7 @@ export const StripeCardUpdate: React.FC = ({ onSubmit, on }; return ( -
+ {children} diff --git a/app/frontend/src/javascript/components/store/categories/manage-product-category.tsx b/app/frontend/src/javascript/components/store/categories/manage-product-category.tsx index 71df0ce11..42fdf6161 100644 --- a/app/frontend/src/javascript/components/store/categories/manage-product-category.tsx +++ b/app/frontend/src/javascript/components/store/categories/manage-product-category.tsx @@ -69,7 +69,6 @@ export const ManageProductCategory: React.FC = ({ pr
{ toggleBtn() } = ({ onSuccess, onErro */ const refreshCategories = () => { ProductCategoryAPI.index().then(data => { - // Map product categories by position - const sortedCategories = data - .filter(c => !c.parent_id) - .sort((a, b) => a.position - b.position); - const childrenCategories = data - .filter(c => typeof c.parent_id === 'number') - .sort((a, b) => b.position - a.position); - childrenCategories.forEach(c => { - const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id); - sortedCategories.splice(parentIndex + 1, 0, c); - }); - setProductCategories(sortedCategories); + setProductCategories(new ProductLib().sortCategories(data)); }).catch((error) => onError(error)); }; diff --git a/app/frontend/src/javascript/components/store/product-form.tsx b/app/frontend/src/javascript/components/store/product-form.tsx index 7839ed8b2..496e50c46 100644 --- a/app/frontend/src/javascript/components/store/product-form.tsx +++ b/app/frontend/src/javascript/components/store/product-form.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from 'react'; -import { useForm, useWatch } from 'react-hook-form'; +import { SubmitHandler, useForm, useWatch } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import slugify from 'slugify'; import _ from 'lodash'; @@ -19,6 +19,7 @@ import MachineAPI from '../../api/machine'; import ProductAPI from '../../api/product'; import { Plus } from 'phosphor-react'; import { ProductStockForm } from './product-stock-form'; +import ProductLib from '../../lib/product'; interface ProductFormProps { product: Product, @@ -53,18 +54,7 @@ export const ProductForm: React.FC = ({ product, title, onSucc useEffect(() => { ProductCategoryAPI.index().then(data => { - // Map product categories by position - const sortedCategories = data - .filter(c => !c.parent_id) - .sort((a, b) => a.position - b.position); - const childrenCategories = data - .filter(c => typeof c.parent_id === 'number') - .sort((a, b) => b.position - a.position); - childrenCategories.forEach(c => { - const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id); - sortedCategories.splice(parentIndex + 1, 0, c); - }); - setProductCategories(buildSelectOptions(sortedCategories)); + setProductCategories(buildSelectOptions(new ProductLib().sortCategories(data))); }).catch(onError); MachineAPI.index({ disabled: false }).then(data => { setMachines(buildChecklistOptions(data)); @@ -111,10 +101,8 @@ export const ProductForm: React.FC = ({ product, title, onSucc /** * Callback triggered when the form is submitted: process with the product creation or update. */ - const onSubmit = (event: React.FormEvent) => { - return handleSubmit((data: Product) => { - saveProduct(data); - })(event); + const onSubmit: SubmitHandler = (data: Product) => { + saveProduct(data); }; /** @@ -236,13 +224,13 @@ export const ProductForm: React.FC = ({ product, title, onSucc {t('app.admin.store.product_form.save')}
-
+

setStockTab(false)}>{t('app.admin.store.product_form.product_parameters')}

setStockTab(true)}>{t('app.admin.store.product_form.stock_management')}

{stockTab - ? + ? :
{ @@ -26,7 +27,7 @@ interface ProductStockFormProps { /** * Form tab to manage a product's stock */ -export const ProductStockForm = ({ product, register, control, formState, onError, onSuccess }: ProductStockFormProps) => { +export const ProductStockForm = ({ product, register, control, formState, onError, onSuccess }: ProductStockFormProps) => { const { t } = useTranslation('admin'); const [activeThreshold, setActiveThreshold] = useState(false); @@ -197,12 +198,11 @@ export const ProductStockForm = ({ produ
- +
); diff --git a/app/frontend/src/javascript/components/store/product-stock-modal.tsx b/app/frontend/src/javascript/components/store/product-stock-modal.tsx index 132f52a87..ba4f465f9 100644 --- a/app/frontend/src/javascript/components/store/product-stock-modal.tsx +++ b/app/frontend/src/javascript/components/store/product-stock-modal.tsx @@ -6,6 +6,7 @@ import { Control, FormState } from 'react-hook-form/dist/types/form'; import { FormSelect } from '../form/form-select'; import { FormInput } from '../form/form-input'; import { FabButton } from '../base/fab-button'; +import { FieldValues } from 'react-hook-form/dist/types/fields'; type selectOption = { value: number, label: string }; @@ -23,7 +24,7 @@ interface ProductStockModalProps { */ // TODO: delete next eslint disable // eslint-disable-next-line @typescript-eslint/no-unused-vars -export const ProductStockModal = ({ product, register, control, formState, onError, onSuccess }: ProductStockModalProps) => { +export const ProductStockModal = ({ product, register, control, formState, onError, onSuccess }: ProductStockModalProps) => { const { t } = useTranslation('admin'); const [movement, setMovement] = useState<'in' | 'out'>('in'); diff --git a/app/frontend/src/javascript/components/store/products.tsx b/app/frontend/src/javascript/components/store/products.tsx index c1ab15dad..f1d9ff010 100644 --- a/app/frontend/src/javascript/components/store/products.tsx +++ b/app/frontend/src/javascript/components/store/products.tsx @@ -14,6 +14,7 @@ import MachineAPI from '../../api/machine'; import { AccordionItem } from './accordion-item'; import { X } from 'phosphor-react'; import { StoreListHeader } from './store-list-header'; +import ProductLib from '../../lib/product'; declare const Application: IApplication; @@ -52,18 +53,7 @@ const Products: React.FC = ({ onSuccess, onError }) => { }); ProductCategoryAPI.index().then(data => { - // Map product categories by position - const sortedCategories = data - .filter(c => !c.parent_id) - .sort((a, b) => a.position - b.position); - const childrenCategories = data - .filter(c => typeof c.parent_id === 'number') - .sort((a, b) => b.position - a.position); - childrenCategories.forEach(c => { - const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id); - sortedCategories.splice(parentIndex + 1, 0, c); - }); - setProductCategories(sortedCategories); + setProductCategories(new ProductLib().sortCategories(data)); }).catch(onError); MachineAPI.index({ disabled: false }).then(data => { diff --git a/app/frontend/src/javascript/components/user/user-profile-form.tsx b/app/frontend/src/javascript/components/user/user-profile-form.tsx index 1c449c0e5..eae3d01c2 100644 --- a/app/frontend/src/javascript/components/user/user-profile-form.tsx +++ b/app/frontend/src/javascript/components/user/user-profile-form.tsx @@ -179,7 +179,7 @@ export const UserProfileForm: React.FC = ({ action, size, const userNetworks = new UserLib(user).getUserSocialNetworks(); return ( - +
): Array => { + const sortedCategories = categories + .filter(c => !c.parent_id) + .sort((a, b) => a.position - b.position); + const childrenCategories = categories + .filter(c => typeof c.parent_id === 'number') + .sort((a, b) => b.position - a.position); + childrenCategories.forEach(c => { + const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id); + sortedCategories.splice(parentIndex + 1, 0, c); + }); + return sortedCategories; + }; +} diff --git a/app/frontend/src/javascript/models/product.ts b/app/frontend/src/javascript/models/product.ts index 27665b281..62129dc4e 100644 --- a/app/frontend/src/javascript/models/product.ts +++ b/app/frontend/src/javascript/models/product.ts @@ -16,11 +16,11 @@ export interface Stock { } export interface Product { - id: number, + id?: number, name: string, slug: string, - sku: string, - description: string, + sku?: string, + description?: string, is_active: boolean, product_category_id?: number, amount?: number,