diff --git a/src/main/webapp/js/requests.js b/src/main/webapp/js/requests.js index d15df8f6d4bcbe336986a8486b237a1a10084352..e2eaebf5757058efea3ecdc39e2aaae086ba2813 100644 --- a/src/main/webapp/js/requests.js +++ b/src/main/webapp/js/requests.js @@ -111,4 +111,41 @@ function updateDetailsTrainers(trainerId) { </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); + }); } \ No newline at end of file diff --git a/src/main/webapp/trainers.html b/src/main/webapp/trainers.html index 48fac72582e84bf4bd2d31997a78da96bf6e73bd..1e12a0883423d88459ec55c7e93923770ac5bf69 100644 --- a/src/main/webapp/trainers.html +++ b/src/main/webapp/trainers.html @@ -40,5 +40,17 @@ </div> </div> +<form id="createTrainerForm" onsubmit="event.preventDefault(); createTrainer();"> + <div class="form-group col-md-6"> + <label for="trainerName">Name</label> + <input type="text" class="form-control" id="trainerName" required> + </div> + <div class="form-group col-md-6"> + <label for="trainerProfileUrl">Profile URL (optional)</label> + <input type="text" class="form-control" id="trainerProfileUrl"> + </div> + <button type="submit" class="btn btn-primary">Create Trainer</button> +</form> + </body> </html> \ No newline at end of file