Commit 5730661c authored by Andreas Rimolsrønning's avatar Andreas Rimolsrønning
Browse files

Frontend/refactor/duplicate code

parent 9c3cde63
......@@ -55,23 +55,6 @@ async function fetchHighscores(exerciseId = undefined) {
return response;
}
async function fetchAllExercises() {
let exerciseList = [];
const response = await sendRequest("GET", `${HOST}/api/exercises/`);
if (response.ok) {
let data = await response.json();
exerciseList = data.results;
while (data.next !== null) {
const nextResponse = await sendRequest("GET", data.next);
if (nextResponse.ok) {
data = await nextResponse.json();
exerciseList = exerciseList.concat(data.results);
}
}
}
return exerciseList;
}
async function populateExerciseFilter() {
const selectFilter = document.getElementById("filter-exercise");
const exerciseList = await fetchAllExercises();
......
......@@ -226,4 +226,21 @@ function createAlert(header, data) {
return alertDiv;
}
async function fetchAllExercises() {
let exerciseList = [];
const response = await sendRequest("GET", `${HOST}/api/exercises/`);
if (response.ok) {
let data = await response.json();
exerciseList = data.results;
while (data.next !== null) {
const nextResponse = await sendRequest("GET", data.next);
if (nextResponse.ok) {
data = await nextResponse.json();
exerciseList = exerciseList.concat(data.results);
}
}
}
return exerciseList;
}
window.addEventListener("DOMContentLoaded", updateNavBar);
......@@ -49,7 +49,7 @@ async function retrieveWorkout(id) {
// create exercises
// fetch exercise types
const exerciseTypes = await fetchExerciseTypes();
const exerciseTypes = await fetchAllExercises();
// TODO: This should be in its own method.
for (let i = 0; i < workoutData.exercise_instances.length; i++) {
......@@ -77,13 +77,13 @@ async function retrieveWorkout(id) {
const currentExerciseTypeId = splitUrl[splitUrl.length - 2];
let currentExerciseType = "";
for (let j = 0; j < exerciseTypes.results.length; j++) {
for (let j = 0; j < exerciseTypes.length; j++) {
const option = document.createElement("option");
option.value = exerciseTypes.results[j].id;
if (currentExerciseTypeId == exerciseTypes.results[j].id) {
currentExerciseType = exerciseTypes.results[j];
option.value = exerciseTypes[j].id;
if (currentExerciseTypeId == exerciseTypes[j].id) {
currentExerciseType = exerciseTypes[j];
}
option.innerText = exerciseTypes.results[j].name;
option.innerText = exerciseTypes[j].name;
exerciseTypeSelect.append(option);
}
......@@ -243,33 +243,10 @@ function handleCancelDuringWorkoutCreate() {
window.location.replace("workouts.html");
}
async function fetchExerciseTypes() {
const exerciseTypeResponse = await sendRequest(
"GET",
`${HOST}/api/exercises/`
);
const exerciseTypes = await exerciseTypeResponse.json();
let hasNext = !!exerciseTypes.next;
while (hasNext) {
const exerciseTypeResponsePageination = await sendRequest(
"GET",
exerciseTypes.next
);
const exerciseTypesPagination = await exerciseTypeResponsePageination.json();
exerciseTypes.results = exerciseTypes.results.concat(
exerciseTypesPagination.results
);
hasNext = !!exerciseTypesPagination.next;
}
return exerciseTypes;
}
async function createBlankExercise() {
const form = document.querySelector("#form-workout");
const exerciseTypes = await fetchExerciseTypes();
const exerciseTypes = await fetchAllExercises();
const exerciseTemplate = document.querySelector("#template-exercise");
const divExerciseContainer = exerciseTemplate.content.firstElementChild.cloneNode(
......@@ -277,14 +254,14 @@ async function createBlankExercise() {
);
const exerciseTypeSelect = divExerciseContainer.querySelector("select");
for (let i = 0; i < exerciseTypes.results.length; i++) {
for (let i = 0; i < exerciseTypes.length; i++) {
const option = document.createElement("option");
option.value = exerciseTypes.results[i].id;
option.innerText = exerciseTypes.results[i].name;
option.value = exerciseTypes[i].id;
option.innerText = exerciseTypes[i].name;
exerciseTypeSelect.append(option);
}
const currentExerciseType = exerciseTypes.results[0];
const currentExerciseType = exerciseTypes[0];
exerciseTypeSelect.value = currentExerciseType.name;
const divExercises = document.querySelector("#div-exercises");
......
......@@ -52,23 +52,6 @@ function createWorkout() {
window.location.replace("workout.html");
}
async function fetchAllExercises() {
let exerciseList = [];
const response = await sendRequest("GET", `${HOST}/api/exercises/`);
if (response.ok) {
let data = await response.json();
exerciseList = data.results;
while (data.next !== null) {
const nextResponse = await sendRequest("GET", data.next);
if (nextResponse.ok) {
data = await nextResponse.json();
exerciseList = exerciseList.concat(data.results);
}
}
}
return exerciseList;
}
async function populateExerciseFilter() {
const selectFilter = document.getElementById("filter-exercise");
const exerciseList = await fetchAllExercises();
......
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