Skip to content
Snippets Groups Projects

Update trainer

2 files
+ 204
0
Compare changes
  • Side-by-side
  • Inline
Files
2
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