Skip to content
Snippets Groups Projects

Update trainer

2 files
+ 63
3
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -148,4 +148,47 @@ function createTrainer() {
.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