import React, {Component} from 'react'; // @ts-ignore import {Field, reduxForm} from "redux-form"; type Props = { handleSubmit: (func: any) => void; onSubmit: any; initialValues: any; reset: () => void; renderButton: any; }; class ProductsForm extends Component { renderError(args: any){ const { touched, error } = args; if (touched && error) { return (
{error}
) } return ""; } renderInput = (args: any) => { const { label, id, name, type, placeholder, input, meta, classes } = args; const borderColor = meta.touched && meta.error ? "#e44947": ""; return (
{this.renderError(meta)}
) }; submitHandle = async (formValues: any) => { await this.props.onSubmit(formValues); this.props.reset() }; render() { return (
{this.props.renderButton()}
); } } const validate = (formValues: any) => { let error: any = {}; if (!formValues.trade_name) { error.trade_name = "Enter the trade name of the product"; } if (!formValues.brand) { error.brand = "Enter the brand name of the product"; } if (!formValues.manufacture) { error.manufacture = "Enter manufacture name"; } if (!formValues.sale_price) { error.sale_price = "Enter the price of the product"; } if (!formValues.purchase_amount) { error.purchase_amount = "Enter the count of the product"; } if (!formValues.purchase_price) { error.purchase_price = "Enter the count of the product"; } if (!formValues.barcode) { error.barcode = "Enter the barcode of the product"; } if(!formValues.manufacture_date) { error.manufacture_date = "Enter manufacture date"; } if(!formValues.expire_date) { error.expire_date = "Enter expiry date"; } if(!formValues.free) { error.free = "Enter free brought pieces"; } if(!formValues.salesmen_free) { error.salesmen_free = "Enter free pieces given from the salesman"; } if (formValues.purchase_price <= 0) { error.purchase_price = "enter number bigger than 0"; } if(new Date(formValues.manufacture_date).getTime()/1000 > new Date().getTime()/1000) { error.manufacture_date = "not valid manufacture date"; } if(new Date(formValues.expire_date).getTime()/1000 < new Date().getTime()/1000) { error.expire_date = "not valid expiry date"; } if (formValues.price <= 0) { error.price = "enter number bigger than 0"; } return error; }; export default reduxForm({form: "productForm", validate, enableReinitialize: true})(ProductsForm);