Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
exam_12_Tsoy_Danil
Project
Project
Details
Activity
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
Цой Данил
exam_12_Tsoy_Danil
Commits
5e15b7bd
Commit
5e15b7bd
authored
Apr 15, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adde alerts in add form, that will indicate which fields was not added or has ivalid value
parent
e6a97ad8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
19 deletions
+63
-19
AddForm.tsx
frontend/src/containers/AddForm/AddForm.tsx
+63
-19
No files found.
frontend/src/containers/AddForm/AddForm.tsx
View file @
5e15b7bd
import
{
shallowEqual
}
from
"react-redux"
import
{
Button
,
TextField
}
from
'@mui/material'
;
import
{
Alert
,
Collapse
,
Icon
Button
,
TextField
}
from
'@mui/material'
;
import
{
AppDispatch
,
AppState
,
useAppDispatch
}
from
"../../store/store"
import
{
useSelector
}
from
"react-redux"
import
Preloader
from
"../../components/UI/Preloader/Preloader"
import
{
ChangeEvent
,
FormEvent
,
use
Effect
,
use
State
}
from
"react"
import
{
ChangeEvent
,
FormEvent
,
useState
}
from
"react"
import
IPhotoDto
from
"../../interfaces/IPhotoDto"
import
{
addPhoto
}
from
"../../store/photos/photos.slice"
import
Box
from
'@mui/material/Box'
;
import
CloseIcon
from
'@mui/icons-material/Close'
;
import
styles
from
'./AddForm.module.css'
const
AddForm
:
React
.
FunctionComponent
=
():
React
.
ReactElement
=>
{
const
{
loadingPhotos
}
=
useSelector
((
state
:
AppState
)
=>
state
.
photos
,
shallowEqual
)
const
[
buttonDisabled
,
setButtonDisabled
]
=
useState
<
boolean
>
(
true
)
const
[
fileName
,
setFileName
]
=
useState
<
string
>
(
''
)
const
[
photoDto
,
setPhotoDto
]
=
useState
<
IPhotoDto
>
({
...
...
@@ -21,6 +22,10 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
photo
:
undefined
})
const
[
titleError
,
setTitleError
]
=
useState
<
boolean
>
(
false
);
const
[
photoError
,
setPhotoError
]
=
useState
<
boolean
>
(
false
);
const
dispatch
:
AppDispatch
=
useAppDispatch
()
const
inputHandler
=
(
e
:
ChangeEvent
<
HTMLInputElement
>
):
void
=>
{
...
...
@@ -37,16 +42,17 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
setFileName
(
e
.
target
.
files
&&
e
.
target
.
files
[
0
]
?
e
.
target
.
files
[
0
].
name
:
''
)
}
const
checkButton
=
()
=>
{
if
(
photoDto
.
title
.
trim
()
===
''
||
!
photoDto
.
photo
){
setButtonDisabled
(
true
)
}
else
{
setButtonDisabled
(
false
)
}
}
const
submitHandler
=
(
e
:
FormEvent
):
void
=>
{
e
.
preventDefault
()
if
(
photoDto
.
title
.
trim
()
===
''
){
setTitleError
(
true
)
}
if
(
!
photoDto
.
photo
){
setPhotoError
(
true
)
}
if
(
photoDto
.
title
.
trim
()
===
''
||
!
photoDto
.
photo
){
return
}
const
formData
=
new
FormData
()
Object
.
keys
(
photoDto
).
forEach
((
key
:
string
)
=>
{
//@ts-ignore
...
...
@@ -59,10 +65,6 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
})
setFileName
(
''
)
}
useEffect
(()
=>
{
checkButton
()
},[
photoDto
])
return
(
<
div
...
...
@@ -75,6 +77,28 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
onSubmit=
{
submitHandler
}
style=
{
{
margin
:
'10px'
,
padding
:
'20px'
,
background
:
'#e0e0e0'
,
borderRadius
:
'5px'
,
display
:
'flex'
,
flexDirection
:
'column'
}
}
>
<
Box
sx=
{
{
width
:
'100%'
}
}
>
<
Collapse
in=
{
titleError
}
>
<
Alert
action=
{
<
IconButton
aria
-
label=
"close"
color=
"inherit"
size=
"small"
onClick=
{
()
=>
{
setTitleError
(
false
);
}
}
>
<
CloseIcon
fontSize=
"inherit"
/>
</
IconButton
>
}
sx=
{
{
mb
:
2
}
}
severity=
"error"
>
Title should exist!
</
Alert
>
</
Collapse
>
</
Box
>
<
TextField
id=
"outlined-basic"
label=
"Photo title"
...
...
@@ -85,6 +109,28 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
onChange=
{
inputHandler
}
autoComplete=
'off'
/>
<
Box
sx=
{
{
width
:
'100%'
}
}
>
<
Collapse
in=
{
photoError
}
>
<
Alert
action=
{
<
IconButton
aria
-
label=
"close"
color=
"inherit"
size=
"small"
onClick=
{
()
=>
{
setPhotoError
(
false
);
}
}
>
<
CloseIcon
fontSize=
"inherit"
/>
</
IconButton
>
}
sx=
{
{
mb
:
2
}
}
severity=
"error"
>
Photo should exist!
</
Alert
>
</
Collapse
>
</
Box
>
<
label
style=
{
{
height
:
'auto'
,
margin
:
'10px'
,
maxWidth
:
'30%'
,
display
:
'flex'
,
flexDirection
:
'column'
}
}
>
<
input
style=
{
{
display
:
"none"
}
}
...
...
@@ -99,9 +145,7 @@ const AddForm: React.FunctionComponent = (): React.ReactElement => {
</
h1
>
<
span
className=
{
styles
.
filename
}
>
{
fileName
}
</
span
>
</
label
>
<
button
disabled=
{
buttonDisabled
}
className=
{
styles
.
add_btn
}
>
SEND
</
button
>
<
button
className=
{
styles
.
add_btn
}
>
SEND
</
button
>
</
form
>
</
div
>
)
...
...
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