Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mod4-wp-2023-2024-pokemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Morais Fonseca, Claudenir (UT-EEMCS)
mod4-wp-2023-2024-pokemon
Merge requests
!102
Update trainer
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Update trainer
s3138585/mod4-wp-2023-2024-pokemon:updateTrainer
into
main
Overview
0
Commits
3
Pipelines
0
Changes
2
Open
Gyunderov, I.A. (Ivan, Student B-TCS)
requested to merge
s3138585/mod4-wp-2023-2024-pokemon:updateTrainer
into
main
10 months ago
Overview
0
Commits
3
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Compare
main
version 1
e4eb33f6
10 months ago
main (HEAD)
and
latest version
latest version
e4eb33f6
3 commits,
10 months ago
version 1
e4eb33f6
3 commits,
10 months ago
2 files
+
204
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
src/main/webapp/js/requests.js
+
131
−
0
Options
let
pokemonTypes
=
{};
let
pokemonTrainers
=
{};
let
tableParentId
=
'
table
'
;
let
detailsParentId
=
'
details
'
;
@@ -60,4 +61,134 @@ function createPokemonTypesTable() {
</tbody>
</table>
`
}
function
createPokemonTrainersTable
()
{
const
tableParent
=
document
.
getElementById
(
tableParentId
);
tableParent
.
innerHTML
=
`
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Created</th>
</tr>
</thead>
<tbody>
${
pokemonTrainers
.
data
.
map
(
resource
=>
`
${
pokemonTrainerToRow
(
resource
)}
`
).
join
(
"
\n
"
)
||
"
no data
"
}
</tbody>
</table>
`
}
function
pokemonTrainerToRow
(
pokemonTrainer
)
{
return
`
<tr id="
${
getRowId
(
pokemonTrainer
)}
" onclick="updateDetailsTrainers('
${
pokemonTrainer
?.
id
?.
trim
()}
')">
<th scope="row">
${
pokemonTrainer
.
id
}
</th>
<td>
${
pokemonTrainer
.
name
}
</td>
<td>#
${
pokemonTrainer
.
created
}
</td>
</tr>
`
}
function
updateDetailsTrainers
(
trainerId
)
{
const
trainer
=
pokemonTrainers
?.
data
?.
find
(
item
=>
item
.
id
===
trainerId
);
const
parent
=
document
.
getElementById
(
detailsParentId
);
parent
.
innerHTML
=
`
<div class="card" id="
${
trainer
.
id
}
_card">
<img src="
${
trainer
.
profileUrl
}
" class="card-img-top" alt="
${
trainer
.
name
}
">
<div class="card-body">
<h5 class="card-title">
${
trainer
.
name
}
</h5>
<p class="card-text">
ID:
${
trainer
.
id
}
</br>
Created:
${
trainer
.
created
}
</br>
Last Update:
${
trainer
.
lastUpDate
}
</p>
</div>
</div>
`
}
function
createTrainer
()
{
const
name
=
document
.
getElementById
(
'
trainerName
'
).
value
;
const
profileUrl
=
document
.
getElementById
(
'
trainerProfileUrl
'
).
value
;
const
trainer
=
{
name
:
name
,
profileUrl
:
profileUrl
};
fetch
(
'
/pokemon/api/trainers
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
(
trainer
),
})
.
then
(
response
=>
response
.
json
())
.
then
(
data
=>
{
console
.
log
(
'
Success:
'
,
data
);
// Refresh the trainers table
fetch
(
'
/pokemon/api/trainers
'
)
.
then
(
res
=>
res
.
json
())
.
then
(
data
=>
{
pokemonTrainers
=
data
;
createPokemonTrainersTable
();
})
.
catch
(
err
=>
{
console
.
error
(
`Unable to fetch Pokemon Trainers:
${
err
.
status
}
`
);
console
.
error
(
err
);
});
})
.
catch
((
error
)
=>
{
console
.
error
(
'
Error:
'
,
error
);
});
}
function
updateTrainer
()
{
const
id
=
document
.
getElementById
(
'
updateTrainerId
'
).
value
;
const
name
=
document
.
getElementById
(
'
updateTrainerName
'
).
value
;
const
profileUrl
=
document
.
getElementById
(
'
updateTrainerProfileUrl
'
).
value
;
const
currentTrainer
=
pokemonTrainers
?.
data
?.
find
(
item
=>
item
.
id
==
id
);
const
creationDate
=
currentTrainer
.
created
;
const
trainer
=
{
id
:
id
,
name
:
name
,
profileUrl
:
profileUrl
,
created
:
creationDate
,
lastUpDate
:
new
Date
().
toLocaleDateString
()
};
fetch
(
`/pokemon/api/trainers/
${
id
}
`
,
{
method
:
'
PUT
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
(
trainer
),
})
.
then
(
response
=>
response
.
json
())
.
then
(
data
=>
{
console
.
log
(
'
Success:
'
,
data
);
// Refresh the trainers table
fetch
(
'
/pokemon/api/trainers
'
)
.
then
(
res
=>
res
.
json
())
.
then
(
data
=>
{
pokemonTrainers
=
data
;
createPokemonTrainersTable
();
})
.
catch
(
err
=>
{
console
.
error
(
`Unable to fetch Pokemon Trainers:
${
err
.
status
}
`
);
console
.
error
(
err
);
});
})
.
catch
((
error
)
=>
{
console
.
error
(
'
Error:
'
,
error
);
});
}
\ No newline at end of file
Loading