From 30999777eaf8e155d30c62f0eb55a22b86863b8e Mon Sep 17 00:00:00 2001 From: IGyunderov <i.gyunderov@student.utwente.nl> Date: Mon, 6 May 2024 15:20:04 +0200 Subject: [PATCH] Issue #1 solved --- src/main/webapp/js/requests.js | 51 ++++++++++++++++++++++++++++++++++ src/main/webapp/trainers.html | 44 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/main/webapp/trainers.html diff --git a/src/main/webapp/js/requests.js b/src/main/webapp/js/requests.js index 9f11ad9..d15df8f 100644 --- a/src/main/webapp/js/requests.js +++ b/src/main/webapp/js/requests.js @@ -1,5 +1,6 @@ let pokemonTypes = {}; +let pokemonTrainers = {}; let tableParentId = 'table'; let detailsParentId = 'details'; @@ -60,4 +61,54 @@ 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> + ` } \ No newline at end of file diff --git a/src/main/webapp/trainers.html b/src/main/webapp/trainers.html new file mode 100644 index 0000000..48fac72 --- /dev/null +++ b/src/main/webapp/trainers.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <!-- Required meta tags --> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" + integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous"> + + <title>Title</title> + <link rel="icon" type="image/x-icon" href="/pokemon/favicon.ico"> +</head> +<body> + +<script src="js/requests.js"></script> +<script> + tableParentId = "tableDiv"; + detailsParentId = "detailsDiv"; + + 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); + }); +</script> + +<h1>Pokemon Trainers</h1> + +<div class="container"> + <div class="row"> + <div id="tableDiv" class="col-8">No data</div> + <div id="detailsDiv" class="col-4">No data</div> + </div> +</div> + +</body> +</html> \ No newline at end of file -- GitLab