From c6e6aeff50b14fccd8d132f0e0a28caccdfa36c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dro=C5=84?= Date: Wed, 22 Dec 2021 15:09:29 +0100 Subject: [PATCH] Disable save in product create & edit when product name is empty (#1551) (#1710) * disable save in product edit & create when name is empty * code review refactor * Fix disabled button Co-authored-by: Magdalena Markusik * fix incorrect logic * fix incorrect logic * fix disable form on product variant Co-authored-by: Magdalena Markusik Co-authored-by: Magdalena Markusik --- .../components/ProductCreatePage/form.tsx | 37 ++++++++++++++----- .../components/ProductUpdatePage/form.tsx | 36 ++++++++++++++---- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/products/components/ProductCreatePage/form.tsx b/src/products/components/ProductCreatePage/form.tsx index c0e168d8c..ce0d9f643 100644 --- a/src/products/components/ProductCreatePage/form.tsx +++ b/src/products/components/ProductCreatePage/form.tsx @@ -327,16 +327,35 @@ function useProductCreateForm( const data = getData(); const submit = () => onSubmit(data); - const disabled = - (!opts.selectedProductType?.hasVariants && - (data.channelListings.some( - channel => - validatePrice(channel.price) || validateCostPrice(channel.costPrice) - ) || - !data.category)) || - (data.isPreorder && + const shouldEnableSave = () => { + if (!data.name || !data.productType) { + return false; + } + + if ( + data.isPreorder && data.hasPreorderEndDate && - !!form.errors.preorderEndDateTime); + !!form.errors.preorderEndDateTime + ) { + return false; + } + + if (opts.selectedProductType?.hasVariants) { + return true; + } + + const hasInvalidChannelListingPrices = data.channelListings.some( + channel => + validatePrice(channel.price) || validateCostPrice(channel.costPrice) + ); + + if (hasInvalidChannelListingPrices) { + return false; + } + return true; + }; + + const disabled = !shouldEnableSave(); return { change: handleChange, diff --git a/src/products/components/ProductUpdatePage/form.tsx b/src/products/components/ProductUpdatePage/form.tsx index 8c92d13bb..abcfc33c1 100644 --- a/src/products/components/ProductUpdatePage/form.tsx +++ b/src/products/components/ProductUpdatePage/form.tsx @@ -386,15 +386,35 @@ function useProductUpdateForm( const submit = async () => handleFormSubmit(getSubmitData(), handleSubmit, setChanged); - const disabled = - (!opts.hasVariants && - data.channelListings.some( - channel => - validatePrice(channel.price) || validateCostPrice(channel.costPrice) - )) || - (data.isPreorder && + const shouldEnableSave = () => { + if (!data.name) { + return false; + } + + if ( + data.isPreorder && data.hasPreorderEndDate && - !!form.errors.preorderEndDateTime); + !!form.errors.preorderEndDateTime + ) { + return false; + } + + if (opts.hasVariants) { + return true; + } + + const hasInvalidChannelListingPrices = data.channelListings.some( + channel => + validatePrice(channel.price) || validateCostPrice(channel.costPrice) + ); + + if (hasInvalidChannelListingPrices) { + return false; + } + return true; + }; + + const disabled = !shouldEnableSave(); return { change: handleChange,