Commit ef6553fa authored by Tomas Vaagen Gule's avatar Tomas Vaagen Gule
Browse files

Duplicates

parent 6e079231
Pipeline #171861 passed with stages
in 2 minutes and 14 seconds
......@@ -4,6 +4,14 @@ let deleteButton;
let editButton;
let oldFormData;
const formInputs = [
"name",
"description",
"duration",
"calories",
"muscleGroup",
"unit",
];
class MuscleGroup {
constructor(type) {
this.isValidType = false;
......@@ -32,25 +40,8 @@ class MuscleGroup {
function handleCancelButtonDuringEdit() {
setReadOnly(true, "#form-exercise");
document.querySelector("select").setAttribute("disabled", "");
okButton.className += " hide";
deleteButton.className += " hide";
cancelButton.className += " hide";
editButton.className = editButton.className.replace(" hide", "");
cancelButton.removeEventListener("click", handleCancelButtonDuringEdit);
let form = document.querySelector("#form-exercise");
if (oldFormData.has("name")) form.name.value = oldFormData.get("name");
if (oldFormData.has("description"))
form.description.value = oldFormData.get("description");
if (oldFormData.has("duration"))
form.duration.value = oldFormData.get("duration");
if (oldFormData.has("calories"))
form.calories.value = oldFormData.get("calories");
if (oldFormData.has("muscleGroup"))
form.muscleGroup.value = oldFormData.get("muscleGroup");
if (oldFormData.has("unit")) form.unit.value = oldFormData.get("unit");
showEditAndHideOthers();
addOldFormDataToForm();
deleteOldFormData();
}
......@@ -165,13 +156,7 @@ async function updateExercise(id) {
// duplicate code from handleCancelButtonDuringEdit
// you should refactor this
setReadOnly(true, "#form-exercise");
okButton.className += " hide";
deleteButton.className += " hide";
cancelButton.className += " hide";
editButton.className = editButton.className.replace(" hide", "");
cancelButton.removeEventListener("click", handleCancelButtonDuringEdit);
showEditAndHideOthers();
deleteOldFormData();
}
}
......@@ -185,6 +170,21 @@ function deleteOldFormData() {
oldFormData.delete("unit");
}
function addOldFormDataToForm() {
let form = document.querySelector("#form-exercise");
formInputs.forEach((input) => {
if (oldFormData.has(input)) form.description.value = oldFormData.get(input);
});
}
function showEditAndHideOthers() {
okButton.className += " hide";
deleteButton.className += " hide";
cancelButton.className += " hide";
editButton.className = editButton.className.replace(" hide", "");
cancelButton.removeEventListener("click", handleCancelButtonDuringEdit);
}
window.addEventListener("DOMContentLoaded", async () => {
cancelButton = document.querySelector("#btn-cancel-exercise");
okButton = document.querySelector("#btn-ok-exercise");
......
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