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 <magdalena@markusik.com>

* fix incorrect logic

* fix incorrect logic

* fix disable form on product variant

Co-authored-by: Magdalena Markusik <magdalena@markusik.com>

Co-authored-by: Magdalena Markusik <magdalena@markusik.com>
This commit is contained in:
Michał Droń 2021-12-22 15:09:29 +01:00 committed by GitHub
parent 95c85a72ed
commit c6e6aeff50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 17 deletions

View file

@ -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,

View file

@ -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,