Skip to content
Snippets Groups Projects
Commit e4eb33f6 authored by Gyunderov, I.A. (Ivan, Student B-TCS)'s avatar Gyunderov, I.A. (Ivan, Student B-TCS)
Browse files

Issue #3 solved

parent 6a3ddb4e
No related branches found
No related tags found
1 merge request!102Update trainer
This commit is part of merge request !102. Comments created here will be created in the context of that merge request.
...@@ -148,4 +148,47 @@ function createTrainer() { ...@@ -148,4 +148,47 @@ function createTrainer() {
.catch((error) => { .catch((error) => {
console.error('Error:', 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
...@@ -39,8 +39,9 @@ ...@@ -39,8 +39,9 @@
<div id="detailsDiv" class="col-4">No data</div> <div id="detailsDiv" class="col-4">No data</div>
</div> </div>
</div> </div>
</br>
<form id="createTrainerForm" onsubmit="event.preventDefault(); createTrainer();"> <h1>Create Trainer</h1>
<form id="createTrainerForm" onsubmit="createTrainer();">
<div class="form-group col-md-6"> <div class="form-group col-md-6">
<label for="trainerName">Name</label> <label for="trainerName">Name</label>
<input type="text" class="form-control" id="trainerName" required> <input type="text" class="form-control" id="trainerName" required>
...@@ -51,6 +52,22 @@ ...@@ -51,6 +52,22 @@
</div> </div>
<button type="submit" class="btn btn-primary">Create Trainer</button> <button type="submit" class="btn btn-primary">Create Trainer</button>
</form> </form>
</br>
<h1>Update Trainer</h1>
<form id="updateTrainerForm" onsubmit="updateTrainer();">
<div class="form-group col-md-6">
<label for="updateTrainerId">ID</label>
<input type="text" class="form-control" id="updateTrainerId" required>
</div>
<div class="form-group col-md-6">
<label for="updateTrainerName">Name</label>
<input type="text" class="form-control" id="updateTrainerName" required>
</div>
<div class="form-group col-md-6">
<label for="updateTrainerProfileUrl">Profile URL (optional)</label>
<input type="text" class="form-control" id="updateTrainerProfileUrl">
</div>
<button type="submit" class="btn btn-primary">Update Trainer</button>
</form>
</body> </body>
</html> </html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment