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
fcd0852e
Commit
fcd0852e
authored
May 07, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added edit functionality to product form
parent
d6f57876
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
44 deletions
+49
-44
Main.jsx
src/component/Main.jsx
+1
-2
ProductForm.jsx
src/component/ProductForm.jsx
+45
-19
ProductLoader.jsx
src/component/ProductLoader.jsx
+0
-20
emptProduct.js
src/emptProduct.js
+3
-3
No files found.
src/component/Main.jsx
View file @
fcd0852e
...
@@ -2,7 +2,6 @@ import React from "react";
...
@@ -2,7 +2,6 @@ import React from "react";
import
{
Redirect
,
Switch
}
from
"react-router"
;
import
{
Redirect
,
Switch
}
from
"react-router"
;
import
AuthRoute
from
"./AuthRoute"
;
import
AuthRoute
from
"./AuthRoute"
;
import
ProductForm
from
"./ProductForm"
;
import
ProductForm
from
"./ProductForm"
;
import
ProductLoader
from
"./ProductLoader"
;
export
default
function
Main
()
{
export
default
function
Main
()
{
return
(
return
(
...
@@ -12,7 +11,7 @@ export default function Main() {
...
@@ -12,7 +11,7 @@ export default function Main() {
<
ProductForm
method=
"POST"
/>
<
ProductForm
method=
"POST"
/>
</
AuthRoute
>
</
AuthRoute
>
<
AuthRoute
path=
"/products/edit/:productId"
>
<
AuthRoute
path=
"/products/edit/:productId"
>
<
Product
Loader
component=
{
()
=>
<
ProductForm
method=
"PUT"
/>
}
/>
<
Product
Form
method=
"PUT"
/>
</
AuthRoute
>
</
AuthRoute
>
<
AuthRoute
exact
path=
"/promos/new"
>
<
AuthRoute
exact
path=
"/promos/new"
>
NEW PROMO
NEW PROMO
...
...
src/component/ProductForm.jsx
View file @
fcd0852e
import
React
,
{
useState
}
from
"react"
;
import
React
,
{
use
Effect
,
use
State
}
from
"react"
;
import
{
useHistory
}
from
"react-router"
;
import
{
useHistory
,
useParams
}
from
"react-router"
;
import
emptyProduct
from
"../emptProduct"
;
import
emptyProduct
from
"../emptProduct"
;
const
emptyForm
=
{
const
emptyForm
=
{
...
@@ -12,16 +12,34 @@ ProductForm.defaultProps = {
...
@@ -12,16 +12,34 @@ ProductForm.defaultProps = {
};
};
export
default
function
ProductForm
(
props
)
{
export
default
function
ProductForm
(
props
)
{
const
{
product
,
method
}
=
props
;
const
{
method
}
=
props
;
const
[
form
,
setForm
]
=
useState
(
product
);
const
[
form
,
setForm
]
=
useState
(
{
...
emptyForm
}
);
const
[
imageMode
,
setImageMode
]
=
useState
(
"url"
);
const
[
imageMode
,
setImageMode
]
=
useState
(
"url"
);
const
[
errors
,
setErrors
]
=
useState
([]);
const
[
errors
,
setErrors
]
=
useState
([]);
const
history
=
useHistory
();
const
history
=
useHistory
();
const
{
productId
}
=
useParams
(
"productId"
);
useEffect
(()
=>
{
fetch
(
`http://localhost:8080/api/products/
${
productId
}
/`
).
then
((
res
)
=>
{
if
(
res
.
ok
)
{
console
.
log
(
res
);
res
.
json
().
then
((
data
)
=>
{
Object
.
keys
(
data
).
forEach
((
key
)
=>
{
if
(
data
[
key
]
===
null
)
{
data
[
key
]
=
""
;
}
});
console
.
log
(
data
);
setForm
({
...
data
,
availableStock
:
data
.
stock
});
});
}
});
},
[
productId
]);
const
validate
=
()
=>
{
const
validate
=
()
=>
{
setErrors
([]);
setErrors
([]);
const
errs
=
[];
const
errs
=
[];
console
.
log
(
form
)
if
(
form
.
sku
.
length
<
3
)
{
if
(
form
.
sku
.
length
<
3
)
{
errs
.
push
(
"SKU must be at least 3 characters"
);
errs
.
push
(
"SKU must be at least 3 characters"
);
}
}
...
@@ -40,7 +58,7 @@ export default function ProductForm(props) {
...
@@ -40,7 +58,7 @@ export default function ProductForm(props) {
if
(
Number
(
form
.
price
)
<=
0
)
{
if
(
Number
(
form
.
price
)
<=
0
)
{
errs
.
push
(
"Price must be greater than 0"
);
errs
.
push
(
"Price must be greater than 0"
);
}
}
if
(
Number
(
form
.
s
tock
)
<=
0
)
{
if
(
Number
(
form
.
availableS
tock
)
<=
0
)
{
errs
.
push
(
"Stock must be greater than 0"
);
errs
.
push
(
"Stock must be greater than 0"
);
}
}
...
@@ -58,7 +76,11 @@ export default function ProductForm(props) {
...
@@ -58,7 +76,11 @@ export default function ProductForm(props) {
if
(
errors
.
length
===
0
)
{
if
(
errors
.
length
===
0
)
{
// console.log(form);
// console.log(form);
fetch
(
"http://localhost:8080/api/products"
,
{
const
url
=
method
===
"PUT"
?
`http://localhost:8080/api/products/
${
productId
}
`
:
"http://localhost:8080/api/products"
;
fetch
(
url
,
{
method
,
method
,
headers
:
{
headers
:
{
"Content-Type"
:
"application/json"
,
"Content-Type"
:
"application/json"
,
...
@@ -137,7 +159,9 @@ export default function ProductForm(props) {
...
@@ -137,7 +159,9 @@ export default function ProductForm(props) {
className=
"form-control"
className=
"form-control"
id=
"productName"
id=
"productName"
value=
{
form
.
productName
}
value=
{
form
.
productName
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
productName
:
e
.
target
.
value
})
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
productName
:
e
.
target
.
value
})
}
/>
/>
</
div
>
</
div
>
<
div
>
<
div
>
...
@@ -174,13 +198,15 @@ export default function ProductForm(props) {
...
@@ -174,13 +198,15 @@ export default function ProductForm(props) {
Description
Description
</
label
>
</
label
>
<
textarea
<
textarea
name=
"productDesc"
name=
"productDesc
iption
"
id=
"productDesc"
id=
"productDesc"
cols=
"40"
cols=
"40"
rows=
"7"
rows=
"7"
className=
"form-control"
className=
"form-control"
value=
{
form
.
productDesc
}
value=
{
form
.
productDescription
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
productDesc
:
e
.
target
.
value
})
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
productDescription
:
e
.
target
.
value
})
}
></
textarea
>
></
textarea
>
</
div
>
</
div
>
</
div
>
</
div
>
...
@@ -205,12 +231,12 @@ export default function ProductForm(props) {
...
@@ -205,12 +231,12 @@ export default function ProductForm(props) {
</
label
>
</
label
>
<
input
<
input
required
required
name=
"
s
tock"
name=
"
availableS
tock"
type=
"number"
type=
"number"
className=
"form-control"
className=
"form-control"
id=
"stock"
id=
"stock"
value=
{
form
.
s
tock
}
value=
{
form
.
availableS
tock
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
s
tock
:
e
.
target
.
value
})
}
onChange=
{
(
e
)
=>
setForm
({
...
form
,
availableS
tock
:
e
.
target
.
value
})
}
/>
/>
</
div
>
</
div
>
</
div
>
</
div
>
...
@@ -232,7 +258,7 @@ export default function ProductForm(props) {
...
@@ -232,7 +258,7 @@ export default function ProductForm(props) {
></
input
>
></
input
>
<
button
<
button
type=
"button"
type=
"button"
class=
"btn btn-outline-primary mt-3"
class
Name
=
"btn btn-outline-primary mt-3"
onClick=
{
()
=>
{
onClick=
{
()
=>
{
setForm
({
...
form
,
imageFile
:
""
});
setForm
({
...
form
,
imageFile
:
""
});
setImageMode
(
"url"
);
setImageMode
(
"url"
);
...
@@ -250,18 +276,18 @@ export default function ProductForm(props) {
...
@@ -250,18 +276,18 @@ export default function ProductForm(props) {
Image URL
Image URL
</
label
>
</
label
>
<
input
<
input
name=
"prod
Img
Url"
name=
"prod
uctImage
Url"
type=
"text"
type=
"text"
className=
"form-control"
className=
"form-control"
id=
"prodImgUrl"
id=
"prodImgUrl"
value=
{
form
.
prod
Img
Url
}
value=
{
form
.
prod
uctImage
Url
}
onChange=
{
(
e
)
=>
onChange=
{
(
e
)
=>
setForm
({
...
form
,
prod
Img
Url
:
e
.
target
.
value
})
setForm
({
...
form
,
prod
uctImage
Url
:
e
.
target
.
value
})
}
}
/>
/>
<
button
<
button
type=
"button"
type=
"button"
class=
"btn btn-outline-primary mt-3"
class
Name
=
"btn btn-outline-primary mt-3"
onClick=
{
()
=>
setImageMode
(
"upload"
)
}
onClick=
{
()
=>
setImageMode
(
"upload"
)
}
>
>
Upload Image
Upload Image
...
...
src/component/ProductLoader.jsx
deleted
100644 → 0
View file @
d6f57876
import
React
,
{
useEffect
,
useState
}
from
"react"
;
import
{
useParams
}
from
"react-router"
;
import
emptyProduct
from
"../emptProduct"
;
export
default
function
ProductLoader
(
props
)
{
const
{
component
:
Component
}
=
props
;
const
{
productId
}
=
useParams
(
"productId"
);
const
[
product
,
setProduct
]
=
useState
(
emptyProduct
)
useEffect
(()
=>
{
fetch
(
`http://localhost:8080/api/products/
${
productId
}
`
)
.
then
(
res
=>
{
if
(
res
.
ok
)
{
res
.
json
.
then
(
data
=>
setProduct
({...
data
}))
}
})
},
[
productId
]);
return
<
Component
product=
{
product
}
/>;
}
src/emptProduct.js
View file @
fcd0852e
...
@@ -5,9 +5,9 @@ const emptyProduct = {
...
@@ -5,9 +5,9 @@ const emptyProduct = {
brand
:
""
,
brand
:
""
,
category
:
""
,
category
:
""
,
price
:
0.0
,
price
:
0.0
,
s
tock
:
0
,
availableS
tock
:
0
,
productDesc
:
""
,
productDesc
iption
:
""
,
prod
ImgUrl
:
""
prod
uctImageUrl
:
""
,
};
};
export
default
emptyProduct
;
export
default
emptyProduct
;
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