40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
|
import * as Data from "./data.js";
|
||
|
|
||
|
export async function populateSports(sportDropdown, selectedSportID = undefined) {
|
||
|
sportDropdown.innerHTML = "";
|
||
|
|
||
|
const sportsList = await Data.getSports();
|
||
|
|
||
|
let currentIndex = 0;
|
||
|
let selectedSportIndex;
|
||
|
sportsList.forEach(sport => {
|
||
|
const option = document.createElement('option');
|
||
|
option.text = sport.name;
|
||
|
option.value = sport.id;
|
||
|
sportDropdown.appendChild(option);
|
||
|
|
||
|
if(sport.id == selectedSportID) selectedSportIndex = currentIndex;
|
||
|
currentIndex++;
|
||
|
});
|
||
|
|
||
|
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
|
||
|
}
|
||
|
|
||
|
export async function addHiddenValue(name, value, form) {
|
||
|
const valueInput = document.createElement('input');
|
||
|
valueInput.setAttribute('name', name);
|
||
|
valueInput.setAttribute('value', value);
|
||
|
valueInput.setAttribute('type', 'hidden');
|
||
|
form.appendChild(valueInput);
|
||
|
}
|
||
|
|
||
|
export async function addRemoveFunction(removeButton, form, objectTitle) {
|
||
|
removeButton.addEventListener('click', async () => {
|
||
|
const verified = confirm(`This ${objectTitle} will be removed.`);
|
||
|
|
||
|
if(verified) {
|
||
|
await addHiddenValue('remove', 1, form);
|
||
|
form.submit();
|
||
|
}
|
||
|
});
|
||
|
}
|