Skip to content
Snippets Groups Projects
Commit d9a30f6b authored by Ernests Malnacs's avatar Ernests Malnacs
Browse files

Added an option to update the name of trainers.

parent 11e35be3
Branches update-trainer
No related tags found
No related merge requests found
......@@ -3,6 +3,8 @@ let tableParentId = 'table';
let detailsParentId = 'details';
let trainers = {}
let selectedTrainerID;
function getRowId(resource) {
return `${resource.id}_row`
}
......@@ -10,6 +12,7 @@ function getRowId(resource) {
function updateDetails(trainerId) {
const tr = trainers?.data?.find(item => item.id === trainerId);
const parent = document.getElementById(detailsParentId);
selectedTrainerID = tr.id;
parent.innerHTML = `
<div class="card" id="${tr.id}_card">
......@@ -22,7 +25,17 @@ function updateDetails(trainerId) {
</p>
</div>
</div>
<div class="card" id="${tr.id}_card_update" style="padding: 20px">
<form id="updateCardForm">
<div class="form-group">
<label for="newTrainerInput">Update Trainer Name</label>
<input type="text" class="form-control" id="updateTrainerInput" placeholder="New Trainer Name">
</div>
<button type="submit" class="btn btn-primary">Update Trainer</button>
</form>
</div>
`
document.getElementById("updateCardForm").addEventListener("submit", handleTrainerUpdate);
}
function trainerToRow(trainer) {
......@@ -93,4 +106,26 @@ function handleTrainerCreate(event) {
.catch(err => {
console.error('Error', err);
})
}
function handleTrainerUpdate(event) {
event.preventDefault();
const trainerName = document.getElementById("updateTrainerInput").value;
const putData = JSON.stringify({id: selectedTrainerID, name: trainerName});
fetch(
`api/trainers/${selectedTrainerID}`, {
method: 'PUT',
headers: {
'Content-Type' : 'application/json'
},
body: putData
})
.then(response => response.json())
.then(data => {
refreshTable();
})
.catch(err => {
console.error('Error', err);
})
}
\ No newline at end of file
......@@ -42,5 +42,6 @@
createTrainerForm.addEventListener("submit", handleTrainerCreate);
refreshTable();
</script>
</body>
</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