Commit 0ee57281 authored by ibooking-sigurd's avatar ibooking-sigurd
Browse files

Refactor to fix the last two critical code smells.

parent 1147b2e9
......@@ -53,7 +53,7 @@ function createFilledRow(templateFilledAthlete, inputValue, controls, disabled)
async function getDietInstances() {
let response = await sendRequest("GET", `${HOST}/api/diet-instances/`);
return await response.json();
return response.json();
}
function createDiet(templateFile, name) {
......@@ -63,7 +63,32 @@ function createDiet(templateFile, name) {
return aDiet;
}
async function createDietFiles(dietInstances, templateDiet) {
for (let diet of dietInstances.results) {
let dietURL = diet.diet
let userURL = diet.user;
let dietRes = await sendRequest("GET", `${dietURL}`);
let userRes = await sendRequest("GET", `${userURL}`);
let dietData = await dietRes.json();
let userData = await userRes.json();
if (userData.id === athlete.id) {
assignBtn.disabled = true;
let deleteBtn = selectionClone.querySelector("#delete-diet-button");
deleteBtn.id = `diet-delete-button-${athlete.username}`;
deleteBtn.addEventListener("click", async () => {
deleteDietInstance(diet);
});
tabPanel.append(deleteBtn);
let tabP = tabPanel.querySelector("#dietfile");
let aDiet = createDiet(templateDiet, dietData.name);
tabP.appendChild(aDiet);
}
}
}
async function displayFiles() {
let user = await getCurrentUser();
......@@ -118,30 +143,9 @@ async function displayFiles() {
tabPanel.prepend(assignBtn);
tabPanel.prepend(dietSelection);
for (let diet of dietInstances.results) {
let dietURL = diet.diet
let userURL = diet.user;
let dietRes = await sendRequest("GET", `${dietURL}`);
let userRes = await sendRequest("GET", `${userURL}`);
let dietData = await dietRes.json();
let userData = await userRes.json();
if (userData.id === athlete.id) {
assignBtn.disabled = true;
let deleteBtn = selectionClone.querySelector("#delete-diet-button");
deleteBtn.id = `diet-delete-button-${athlete.username}`;
deleteBtn.addEventListener("click", async () => {
deleteDietInstance(diet);
});
tabPanel.append(deleteBtn);
createDietFiles(dietInstances, templateDiet);
let tabP = tabPanel.querySelector("#dietfile");
let aDiet = createDiet(templateDiet, dietData.name);
tabP.appendChild(aDiet);
}
}
}
......@@ -301,14 +305,8 @@ async function submitRoster() {
body.athletes.push(athlete.id);
} else {
// create offer
let offerBody = { 'status': 'p', 'recipient': athlete.url };
let offerResponse = await sendRequest("POST", `${HOST}/api/offers/`, offerBody);
if (!offerResponse.ok) {
let data = await response.json();
let alert = createAlert("Could not create offer!", data);
document.body.prepend(alert);
await createOffer(athlete)
}
}
} else {
let data = await response.json();
let alert = createAlert(`Could not retrieve user ${rosterInput.value}!`, data);
......@@ -320,6 +318,16 @@ async function submitRoster() {
location.reload();
}
async function createOffer(athlete) {
let offerBody = { 'status': 'p', 'recipient': athlete.url };
let offerResponse = await sendRequest("POST", `${HOST}/api/offers/`, offerBody);
if (!offerResponse.ok) {
let data = await offerResponse.json();
let alert = createAlert("Could not create offer!", data);
document.body.prepend(alert);
}
}
async function uploadFiles(event, athlete) {
let form = event.currentTarget.parentElement;
let inputFormData = new FormData(form);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment