Skip to content

Commit

Permalink
[BackOffice][Add/Edit product] Should validate length > width (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
toannguyenk authored Oct 31, 2024
1 parent 0a49d70 commit ddef30e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import { FieldErrorsImpl, UseFormRegister, UseFormSetValue } from 'react-hook-form';
import { FieldErrorsImpl, UseFormRegister, UseFormSetValue, UseFormWatch } from 'react-hook-form';
import { toast } from 'react-toastify';
import slugify from 'slugify';

Expand All @@ -19,9 +19,10 @@ type Props = {
register: UseFormRegister<FormProduct>;
errors: FieldErrorsImpl<FormProduct>;
setValue: UseFormSetValue<FormProduct>;
watch: UseFormWatch<FormProduct>;
};

const ProductGeneralInformation = ({ register, errors, setValue }: Props) => {
const ProductGeneralInformation = ({ register, errors, setValue, watch }: Props) => {
//Get ID
const router = useRouter();
const { id } = router.query;
Expand All @@ -33,6 +34,8 @@ const ProductGeneralInformation = ({ register, errors, setValue }: Props) => {
const [isLoading, setLoading] = useState(false);
const [taxClasses, setTaxClasses] = useState<TaxClass[]>([]);

const width = watch('width', 0);

useEffect(() => {
getBrands().then((data) => {
setBrands(data);
Expand Down Expand Up @@ -176,7 +179,10 @@ const ProductGeneralInformation = ({ register, errors, setValue }: Props) => {
type="number"
registerOptions={{
required: { value: true, message: 'Product length is required' },
validate: { positive: (v) => v > 0 || 'Length must be greater than 0' },
validate: {
positive: (v) => v > 0 || 'Length must be greater than 0',
greaterThanWidth: (v) => (width && v > width) || 'Length must be greater than width',
},
}}
/>

Expand Down
8 changes: 7 additions & 1 deletion backoffice/pages/catalog/products/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const EditProduct: NextPage = () => {
setValue,
handleSubmit,
getValues,
watch,
formState: { errors },
} = useForm<FormProduct>();

Expand Down Expand Up @@ -91,7 +92,12 @@ const EditProduct: NextPage = () => {
<form onSubmit={handleSubmit(onSubmit)}>
<Tabs className="mb-3" activeKey={tabKey} onSelect={(e: any) => setTabKey(e)}>
<Tab eventKey={'general'} title="General Information">
<ProductGeneralInformation register={register} errors={errors} setValue={setValue} />
<ProductGeneralInformation
register={register}
errors={errors}
setValue={setValue}
watch={watch}
/>
</Tab>
<Tab eventKey={'image'} title="Product Images">
<ProductImage product={product} setValue={setValue} />
Expand Down
8 changes: 7 additions & 1 deletion backoffice/pages/catalog/products/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ProductCreate: NextPage = () => {
setValue,
handleSubmit,
getValues,
watch,
formState: { errors },
} = useForm<FormProduct>({
defaultValues: {
Expand Down Expand Up @@ -82,7 +83,12 @@ const ProductCreate: NextPage = () => {
<form onSubmit={handleSubmit(onSubmitForm)}>
<Tabs className="mb-3" activeKey={tabKey} onSelect={(e: any) => setTabKey(e)}>
<Tab eventKey={'general'} title="General Information">
<ProductGeneralInformation register={register} errors={errors} setValue={setValue} />
<ProductGeneralInformation
register={register}
errors={errors}
setValue={setValue}
watch={watch}
/>
</Tab>
<Tab eventKey={'image'} title="Product Images">
<ProductImage setValue={setValue} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private <T extends ProductVariationSaveVm> void validateProductVm(ProductSaveVm<

private <T extends ProductVariationSaveVm> void validateProductVm(ProductSaveVm<T> productSaveVm,
Product existingProduct) {
validateLengthMustGreaterThanWidth(productSaveVm);

validateExistingProductProperties(productSaveVm, existingProduct);

validateProductVariationDuplicates(productSaveVm);
Expand All @@ -175,6 +177,12 @@ private <T extends ProductVariationSaveVm> void validateProductVm(ProductSaveVm<
}
}

private <T extends ProductVariationSaveVm> void validateLengthMustGreaterThanWidth(ProductSaveVm<T> productPostVm) {
if (productPostVm.length() < productPostVm.width()) {
throw new BadRequestException(Constants.ErrorCode.MAKE_SURE_LENGTH_GREATER_THAN_WIDTH);
}
}

private void validateExistingProductProperties(ProductProperties productProperties, Product existingProduct) {
checkPropertyExists(productProperties.slug().toLowerCase(), existingProduct,
productRepository::findBySlugAndIsPublishedTrue, Constants.ErrorCode.SLUG_ALREADY_EXISTED_OR_DUPLICATED);
Expand Down
1 change: 1 addition & 0 deletions product/src/main/java/com/yas/product/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public final class ErrorCode {
public static final String PRODUCT_OPTION_VALUE_IS_NOT_FOUND = "PRODUCT_OPTION_VALUE_IS_NOT_FOUND";
public static final String PRODUCT_COMBINATION_PROCESSING_FAILED = "PRODUCT_COMBINATION_PROCESSING_FAILED";
public static final String NO_MATCHING_PRODUCT_OPTIONS = "NO_MATCHING_PRODUCT_OPTIONS";
public static final String MAKE_SURE_LENGTH_GREATER_THAN_WIDTH = "MAKE_SURE_LENGTH_GREATER_THAN_WIDTH";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public interface ProductSaveVm<T extends ProductVariationSaveVm> extends Product

Boolean isPublished();

Double length();

Double width();

@Override
default Long id() {
return null;
Expand Down
3 changes: 2 additions & 1 deletion product/src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ SKU_ALREADY_EXISTED_OR_DUPLICATED=Sku {} is already existed or is duplicated
GTIN_ALREADY_EXISTED_OR_DUPLICATED=Gtin {} is already existed or is duplicated
PRODUCT_OPTION_VALUE_IS_NOT_FOUND=Product option value {} is not found
PRODUCT_COMBINATION_PROCESSING_FAILED=An error occurred while processing product combinations
NO_MATCHING_PRODUCT_OPTIONS=The provided product options could not be found
NO_MATCHING_PRODUCT_OPTIONS=The provided product options could not be found
MAKE_SURE_LENGTH_GREATER_THAN_WIDTH=Please make sure length greater than width

0 comments on commit ddef30e

Please sign in to comment.