I am working on a React application where I have a form for managing prices. The form includes the following price types: Wholesale price, Transfer price, and Sales price. I want to display a warning message if the value entered for any of these prices is less than the cost price.
const COST_PRICE_TYPE = 'cost';
const SALES_PRICE_TYPE = 'sales';
const TRANSFER_PRICE_TYPE = 'transfer';
const WHOLESALE_PRICE_TYPE = 'wholesale';
const handleSubmit = async (e) => {
e.preventDefault();
setIsTrue(true);
if (updateData.isUpdate) {
// ... (code for handling updates)
const costPriceObject = priceList.find((item) => item.type.key === COST_PRICE_TYPE);
const costPrice = costPriceObject ? costPriceObject.price : 0;
const isSalesPriceLessThanCost =
selectedPriceType.key === SALES_PRICE_TYPE && selectedPrice < costPrice;
const isTransferPriceLessThanCost =
selectedPriceType.key === TRANSFER_PRICE_TYPE && selectedPrice < costPrice;
const isWholesalePriceLessThanCost =
selectedPriceType.key === WHOLESALE_PRICE_TYPE && selectedPrice < costPrice;
console.log('Cost Price:', costPrice);
console.log('Is Sales Price Less Than Cost:', isSalesPriceLessThanCost);
console.log('Is Transfer Price Less Than Cost:', isTransferPriceLessThanCost);
console.log('Is Wholesale Price Less Than Cost:', isWholesalePriceLessThanCost);
if (isSalesPriceLessThanCost || isTransferPriceLessThanCost || isWholesalePriceLessThanCost) {
// Display a warning message
const warningMessage = `Based on the amount for ${selectedPriceType.name} that is less than the Cost price ${costPrice}, you may encounter a loss due to this.`;
dispatch(showMessage({ message: warningMessage, variant: 'warning' }));
}
} else {
// ... (code for handling new records)
}
setIsTrue(false);
setUpdateData({ isUpdate: false, data: {} });
setSelectedPrice('');
setSelectedCurrencyUnit({});
setSelectedPriceType({});
resetField('priceType', 'currencyId');
reset();
};
The warning message is not displaying when a sales price, transfer price, or wholesale price is less than the cost price.
I have already tried adding console logs to check the values of costPrice, isSalesPriceLessThanCost, isTransferPriceLessThanCost, and isWholesalePriceLessThanCost, but the logs are not executed.
console.log('Cost Price:', costPrice);
console.log('Is Sales Price Less Than Cost:', isSalesPriceLessThanCost);
console.log('Is Transfer Price Less Than Cost:', isTransferPriceLessThanCost);
console.log('Is Wholesale Price Less Than Cost:', isWholesalePriceLessThanCost);