Commit 17319145 authored by Ben Anderson's avatar Ben Anderson

Added form validations for product creation

parent 95104079
import React, { useState } from "react"; import React, { useState } from "react";
export default function ProductForm({ product }) { ProductForm.defaultProps = {
const [form, setForm] = useState({ product: {
sku: "", sku: "",
upc: "", upc: "",
productName: "", productName: "",
...@@ -11,16 +11,63 @@ export default function ProductForm({ product }) { ...@@ -11,16 +11,63 @@ export default function ProductForm({ product }) {
stock: 0, stock: 0,
productDescription: "", productDescription: "",
productImage: "", productImage: "",
}); },
};
export default function ProductForm({ product }) {
const [form, setForm] = useState(product);
const [errors, setErrors] = useState([]);
const validate = () => {
setErrors([]);
const errs = [];
if (form.sku.length < 3) {
errs.push("SKU must be at least 3 characters");
}
if (form.upc.length < 3) {
errs.push("UPC must be at least 3 characters");
}
if (form.productName.length === 0) {
errs.push("Please enter a product name");
}
if (form.brand.length === 0) {
errs.push("Please enter a brand");
}
if (form.category.length === 0) {
errs.push("Please specify a category");
}
if (Number(form.price) <= 0) {
errs.push("Price must be greater than 0");
}
if (Number(form.stock) <= 0) {
errs.push("Stock must be greater than 0");
}
setErrors([...errs]);
};
const onSubmit = (e) => { const onSubmit = (e) => {
e.preventDefault(); e.preventDefault();
// TODO send form to products API validate();
console.log(form);
if (errors.length === 0) {
// TODO send form to products API
console.log(form);
}
}; };
return ( return (
<div class="container"> <div className="container mt-3">
{errors.length ? (
<ul className="list-group">
{errors.map((error, i) => (
<li key={i} className="list-group-item list-group-item-danger">
{error}
</li>
))}
</ul>
) : null}
<form onSubmit={onSubmit}> <form onSubmit={onSubmit}>
<div className="row mt-3"> <div className="row mt-3">
<div className="col"> <div className="col">
...@@ -28,6 +75,7 @@ export default function ProductForm({ product }) { ...@@ -28,6 +75,7 @@ export default function ProductForm({ product }) {
SKU SKU
</label> </label>
<input <input
required
name="sku" name="sku"
type="text" type="text"
className="form-control" className="form-control"
...@@ -41,6 +89,7 @@ export default function ProductForm({ product }) { ...@@ -41,6 +89,7 @@ export default function ProductForm({ product }) {
UPC UPC
</label> </label>
<input <input
required
name="upc" name="upc"
type="text" type="text"
className="form-control" className="form-control"
...@@ -57,6 +106,7 @@ export default function ProductForm({ product }) { ...@@ -57,6 +106,7 @@ export default function ProductForm({ product }) {
Name Name
</label> </label>
<input <input
required
name="productName" name="productName"
type="text" type="text"
className="form-control" className="form-control"
...@@ -72,6 +122,7 @@ export default function ProductForm({ product }) { ...@@ -72,6 +122,7 @@ export default function ProductForm({ product }) {
Brand Brand
</label> </label>
<input <input
required
name="brand" name="brand"
type="text" type="text"
className="form-control" className="form-control"
...@@ -85,6 +136,7 @@ export default function ProductForm({ product }) { ...@@ -85,6 +136,7 @@ export default function ProductForm({ product }) {
Category Category
</label> </label>
<input <input
required
name="category" name="category"
type="text" type="text"
className="form-control" className="form-control"
...@@ -117,6 +169,7 @@ export default function ProductForm({ product }) { ...@@ -117,6 +169,7 @@ export default function ProductForm({ product }) {
Price Price
</label> </label>
<input <input
required
name="price" name="price"
type="number" type="number"
className="form-control" className="form-control"
...@@ -130,6 +183,7 @@ export default function ProductForm({ product }) { ...@@ -130,6 +183,7 @@ export default function ProductForm({ product }) {
Stock Stock
</label> </label>
<input <input
required
name="stock" name="stock"
type="number" type="number"
className="form-control" className="form-control"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment