Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
inventory-promotion-react
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ascend
inventory-promotion-react
Commits
17319145
Commit
17319145
authored
May 06, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added form validations for product creation
parent
95104079
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
6 deletions
+60
-6
ProductForm.jsx
src/component/ProductForm.jsx
+60
-6
No files found.
src/component/ProductForm.jsx
View file @
17319145
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"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment