From 68e432df093b74d4f1cdbd479c5ed6f8df1e2d81 Mon Sep 17 00:00:00 2001 From: s3190285 <m.ruginosu@student.utwente.nl> Date: Tue, 7 May 2024 12:42:03 +0200 Subject: [PATCH] issue 1 --- src/main/webapp/js/trainers.js | 63 ++++++++++++++++++++++++++++++++++ src/main/webapp/trainers.html | 44 ++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/main/webapp/js/trainers.js create mode 100644 src/main/webapp/trainers.html diff --git a/src/main/webapp/js/trainers.js b/src/main/webapp/js/trainers.js new file mode 100644 index 0000000..c114fed --- /dev/null +++ b/src/main/webapp/js/trainers.js @@ -0,0 +1,63 @@ + +let pokemonTypes = {}; +let tableParentId = 'table'; +let detailsParentId = 'details'; + +function getRowId(resource) { + return `${resource.id}_row` +} + +function updateDetails(pokemonTypeId) { + const pt = pokemonTypes?.data?.find(item => item.id === pokemonTypeId); + const parent = document.getElementById(detailsParentId); + + parent.innerHTML = ` + <div class="card" id="${pt.id}_card"> + <img src="${pt.imgUrl}" class="card-img-top" alt="${pt.classification}"> + <div class="card-body"> + <h5 class="card-title">${pt.name} #${pt.pokedexNumber}</h5> + <p class="card-text"> + Japanese name: ${pt.japaneseName} </br> + Classification: ${pt.classification} </br> + Abilities: ${pt.abilities?.join(", ") || "none"} </br> + Type: ${pt.primaryType}${pt.secondaryType ? " , " + pt.secondaryType : ""} + </p> + </div> + </div> + ` +} + +function pokemonTypeToRow(pokemonType) { + return ` + <tr id="${getRowId(pokemonType)}" onclick="updateDetails('${pokemonType?.id?.trim()}')"> + <th scope="row">${pokemonType.id}</th> + <td>${pokemonType.name}</td> + <td>#${pokemonType.pokedexNumber}</td> + <td>${pokemonType.primaryType}</td> + <td>${pokemonType.secondaryType || '-'}</td> + </tr> + ` +} + +function createPokemonTypesTable() { + 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">Pokedex Number</th> + <th scope="col">Primary Type</th> + <th scope="col">Secondary Type</th> + </tr> + </thead> + <tbody> + ${ + pokemonTypes.data.map(resource => `${pokemonTypeToRow(resource)}`).join("\n") + || "no data" + } + </tbody> + </table> + ` +} \ 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..93f51ca --- /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/trainers.js"></script> +<script> + tableParentId = "tableDiv"; + detailsParentId = "detailsDiv"; + + fetch('/pokemon/api/trainers') + .then(res => res.json()) + .then(data => { + pokemonTypes = data; + createPokemonTypesTable(); + }) + .catch(err => { + console.error(`Unable to fetch Pokemon Types: ${err.status}`); + console.error(err); + }); +</script> + +<h1>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