FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptTest/CrudStructure.js at MAIN · rbusdon/JavaScriptTest · GitHub
rbusdon
/
JavaScriptTest
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptTest
/
CrudStructure.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
255 lines (207 loc) · 10.7 KB
Breadcrumbs
JavaScriptTest
/
CrudStructure.js
Copy path
File metadata and controls
255 lines (207 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const
form
=
document
.
getElementById
(
"inputForm"
)
;
const
gridContainer
=
document
.
getElementById
(
"grid"
)
;
let
gridCount
=
0
;
form
.
addEventListener
(
"submit"
,
function
(
event
)
{
event
.
preventDefault
(
)
;
updateGrid
(
)
;
}
)
;
const
updateGrid
=
(
)
=>
{
const
user
=
document
.
getElementById
(
"input-user"
)
.
value
;
const
name
=
document
.
getElementById
(
"input-name"
)
.
value
;
const
surname
=
document
.
getElementById
(
"input-surname"
)
.
value
;
const
email
=
document
.
getElementById
(
"input-email"
)
.
value
;
if
(
!
validateUser
(
user
)
||
!
validateEmail
(
email
)
)
{
alert
(
"Invalid user or email. Please check your inputs."
)
;
return
;
}
const
userExists
=
checkUserExists
(
user
)
;
const
emailExists
=
checkEmailExists
(
email
)
;
if
(
userExists
)
{
alert
(
"User already exists. Please choose a different user."
)
;
return
;
}
if
(
emailExists
)
{
alert
(
"Email already exists. Please choose a different email."
)
;
return
;
}
const
gridItem
=
document
.
createElement
(
"div"
)
;
gridItem
.
classList
.
add
(
"grid-item"
)
;
gridItem
.
dataset
.
identity
=
++
gridCount
;
gridItem
.
innerHTML
=
`
<p>Identity:
${
gridCount
}
</p>
<p>User:
${
user
}
</p>
<p>Name:
${
name
}
</p>
<p>Surname:
${
surname
}
</p>
<p>Email:
${
email
}
</p>
`
;
gridContainer
.
appendChild
(
gridItem
)
;
clearInputFields
(
)
;
}
const
validateUser
=
(
user
)
=>
{
if
(
!
user
)
return
false
;
return
true
;
}
const
validateEmail
=
(
email
)
=>
{
if
(
!
email
)
return
false
;
if
(
!
email
.
includes
(
"@"
)
||
!
email
.
includes
(
"."
)
)
return
false
;
return
true
;
}
const
checkUserExists
=
(
user
)
=>
{
const
users
=
Array
.
from
(
gridContainer
.
querySelectorAll
(
".grid-item p:nth-of-type(2)"
)
)
.
map
(
p
=>
p
.
textContent
.
split
(
": "
)
[
1
]
)
;
return
users
.
includes
(
user
)
;
}
const
checkEmailExists
=
(
email
)
=>
{
const
emails
=
Array
.
from
(
gridContainer
.
querySelectorAll
(
".grid-item p:nth-of-type(5)"
)
)
.
map
(
p
=>
p
.
textContent
.
split
(
": "
)
[
1
]
)
;
return
emails
.
includes
(
email
)
;
}
const
clearInputFields
=
(
)
=>
{
document
.
getElementById
(
"input-user"
)
.
value
=
""
;
document
.
getElementById
(
"input-name"
)
.
value
=
""
;
document
.
getElementById
(
"input-surname"
)
.
value
=
""
;
document
.
getElementById
(
"input-email"
)
.
value
=
""
;
}
const
modifyButton
=
document
.
getElementById
(
"modify-button"
)
;
modifyButton
.
addEventListener
(
"click"
,
function
(
)
{
const
inputs
=
form
.
getElementsByTagName
(
"input"
)
;
for
(
let
i
=
0
;
i
<
inputs
.
length
;
i
++
)
{
inputs
[
i
]
.
disabled
=
true
;
}
document
.
getElementById
(
"submit-button"
)
.
disabled
=
true
;
document
.
getElementById
(
"delete-button"
)
.
disabled
=
true
;
const
identityInput
=
document
.
createElement
(
"input"
)
;
identityInput
.
type
=
"number"
;
identityInput
.
id
=
"identity-input"
;
identityInput
.
placeholder
=
"Identity Value"
;
form
.
insertBefore
(
identityInput
,
modifyButton
)
;
const
submitIdentityButton
=
document
.
createElement
(
"button"
)
;
submitIdentityButton
.
type
=
"button"
;
submitIdentityButton
.
id
=
"submit-identity-button"
;
submitIdentityButton
.
textContent
=
"Submit Identity"
;
form
.
insertBefore
(
submitIdentityButton
,
modifyButton
)
;
modifyButton
.
disabled
=
true
;
deleteButton
.
disabled
=
true
;
submitIdentityButton
.
addEventListener
(
"click"
,
function
(
)
{
const
identityValue
=
parseInt
(
document
.
getElementById
(
"identity-input"
)
.
value
,
10
)
;
modifyGrid
(
identityValue
)
;
}
)
;
}
)
;
const
modifyGrid
=
(
identityValue
)
=>
{
const
gridItems
=
gridContainer
.
getElementsByClassName
(
"grid-item"
)
;
const
gridItem
=
Array
.
from
(
gridItems
)
.
find
(
item
=>
parseInt
(
item
.
dataset
.
identity
,
10
)
===
identityValue
)
;
if
(
gridItem
)
{
const
user
=
gridItem
.
querySelector
(
"p:nth-of-type(2)"
)
.
textContent
.
split
(
": "
)
[
1
]
;
const
name
=
gridItem
.
querySelector
(
"p:nth-of-type(3)"
)
.
textContent
.
split
(
": "
)
[
1
]
;
const
surname
=
gridItem
.
querySelector
(
"p:nth-of-type(4)"
)
.
textContent
.
split
(
": "
)
[
1
]
;
const
email
=
gridItem
.
querySelector
(
"p:nth-of-type(5)"
)
.
textContent
.
split
(
": "
)
[
1
]
;
const
inputs
=
form
.
getElementsByTagName
(
"input"
)
;
for
(
let
i
=
0
;
i
<
inputs
.
length
;
i
++
)
{
inputs
[
i
]
.
disabled
=
false
;
}
document
.
getElementById
(
"submit-button"
)
.
disabled
=
false
;
document
.
getElementById
(
"delete-button"
)
.
disabled
=
false
;
document
.
getElementById
(
"identity-input"
)
.
remove
(
)
;
document
.
getElementById
(
"submit-identity-button"
)
.
remove
(
)
;
const
label
=
document
.
createElement
(
"label"
)
;
label
.
textContent
=
"ATTENTION: you selected the Modify option. You have now to type again the values."
;
form
.
insertBefore
(
label
,
modifyButton
)
;
const
confirmButton
=
document
.
createElement
(
"button"
)
;
confirmButton
.
type
=
"button"
;
confirmButton
.
id
=
"confirm-button"
;
confirmButton
.
textContent
=
"Confirm"
;
form
.
insertBefore
(
confirmButton
,
modifyButton
)
;
document
.
getElementById
(
"input-user"
)
.
value
=
user
;
document
.
getElementById
(
"input-name"
)
.
value
=
name
;
document
.
getElementById
(
"input-surname"
)
.
value
=
surname
;
document
.
getElementById
(
"input-email"
)
.
value
=
email
;
modifyButton
.
disabled
=
true
;
deleteButton
.
disabled
=
true
;
confirmButton
.
addEventListener
(
"click"
,
function
(
)
{
if
(
validateForm
(
)
)
{
modifyGridItem
(
gridItem
)
;
}
}
)
;
}
}
const
validateForm
=
(
)
=>
{
const
user
=
document
.
getElementById
(
"input-user"
)
.
value
;
const
email
=
document
.
getElementById
(
"input-email"
)
.
value
;
if
(
checkUserExists
(
user
)
)
{
alert
(
"User already exists. Please choose a different user."
)
;
return
false
;
}
if
(
checkEmailExists
(
email
)
)
{
alert
(
"Email already exists. Please choose a different email."
)
;
return
false
;
}
if
(
!
Email
(
email
)
)
{
alert
(
"Invalid email format. Please enter a valid email."
)
;
return
false
;
}
return
true
;
}
const
modifyGridItem
=
(
gridItem
)
=>
{
const
user
=
document
.
getElementById
(
"input-user"
)
.
value
;
const
name
=
document
.
getElementById
(
"input-name"
)
.
value
;
const
surname
=
document
.
getElementById
(
"input-surname"
)
.
value
;
const
email
=
document
.
getElementById
(
"input-email"
)
.
value
;
gridItem
.
querySelector
(
"p:nth-of-type(2)"
)
.
textContent
=
`User:
${
user
}
`
;
gridItem
.
querySelector
(
"p:nth-of-type(3)"
)
.
textContent
=
`Name:
${
name
}
`
;
gridItem
.
querySelector
(
"p:nth-of-type(4)"
)
.
textContent
=
`Surname:
${
surname
}
`
;
gridItem
.
querySelector
(
"p:nth-of-type(5)"
)
.
textContent
=
`Email:
${
email
}
`
;
const
inputs
=
form
.
getElementsByTagName
(
"input"
)
;
for
(
let
i
=
0
;
i
<
inputs
.
length
;
i
++
)
{
inputs
[
i
]
.
disabled
=
false
;
}
document
.
getElementById
(
"submit-button"
)
.
disabled
=
false
;
document
.
getElementById
(
"delete-button"
)
.
disabled
=
false
;
document
.
querySelector
(
"label"
)
.
remove
(
)
;
document
.
getElementById
(
"confirm-button"
)
.
remove
(
)
;
clearInputFields
(
)
;
modifyButton
.
disabled
=
false
;
deleteButton
.
disabled
=
false
;
}
;
const
deleteButton
=
document
.
getElementById
(
"delete-button"
)
;
deleteButton
.
addEventListener
(
"click"
,
function
(
)
{
const
inputs
=
form
.
getElementsByTagName
(
"input"
)
;
for
(
let
i
=
0
;
i
<
inputs
.
length
;
i
++
)
{
inputs
[
i
]
.
disabled
=
true
;
}
document
.
getElementById
(
"submit-button"
)
.
disabled
=
true
;
document
.
getElementById
(
"modify-button"
)
.
disabled
=
true
;
const
identityInput
=
document
.
createElement
(
"input"
)
;
identityInput
.
type
=
"number"
;
identityInput
.
id
=
"identity-input"
;
identityInput
.
placeholder
=
"Identity Value"
;
form
.
insertBefore
(
identityInput
,
deleteButton
)
;
const
submitIdentityButton
=
document
.
createElement
(
"button"
)
;
submitIdentityButton
.
type
=
"button"
;
submitIdentityButton
.
id
=
"submit-identity-button"
;
submitIdentityButton
.
textContent
=
"Submit Identity"
;
form
.
insertBefore
(
submitIdentityButton
,
deleteButton
)
;
deleteButton
.
disabled
=
true
;
modifyButton
.
disabled
=
true
;
submitIdentityButton
.
addEventListener
(
"click"
,
function
(
)
{
const
identityValue
=
parseInt
(
document
.
getElementById
(
"identity-input"
)
.
value
,
10
)
;
deleteGridItem
(
identityValue
)
;
}
)
;
}
)
;
const
deleteGridItem
=
(
identityValue
)
=>
{
const
gridItems
=
gridContainer
.
getElementsByClassName
(
"grid-item"
)
;
const
gridItem
=
Array
.
from
(
gridItems
)
.
find
(
item
=>
parseInt
(
item
.
dataset
.
identity
,
10
)
===
identityValue
)
;
if
(
gridItem
)
{
gridContainer
.
removeChild
(
gridItem
)
;
const
inputs
=
form
.
getElementsByTagName
(
"input"
)
;
for
(
let
i
=
0
;
i
<
inputs
.
length
;
i
++
)
{
inputs
[
i
]
.
disabled
=
false
;
}
document
.
getElementById
(
"submit-button"
)
.
disabled
=
false
;
document
.
getElementById
(
"modify-button"
)
.
disabled
=
false
;
document
.
getElementById
(
"identity-input"
)
.
remove
(
)
;
document
.
getElementById
(
"submit-identity-button"
)
.
remove
(
)
;
document
.
querySelector
(
"label"
)
.
remove
(
)
;
document
.
getElementById
(
"confirm-button"
)
.
remove
(
)
;
clearInputFields
(
)
;
deleteButton
.
disabled
=
false
;
modifyButton
.
disabled
=
false
;
}
}
;
Back
|
FazBrowse Home
|
New Git URL