From bd618f39703103b2f6139cd49d992ff5508c7522 Mon Sep 17 00:00:00 2001 From: ingrid <ingrimeg@stud.ntnu.no> Date: Tue, 2 May 2023 13:00:52 +0200 Subject: [PATCH] Enheter tilpasser seg --- src/components/EatFridgeItemModal.vue | 43 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/components/EatFridgeItemModal.vue b/src/components/EatFridgeItemModal.vue index 27b8d9b..f788605 100644 --- a/src/components/EatFridgeItemModal.vue +++ b/src/components/EatFridgeItemModal.vue @@ -6,7 +6,7 @@ <p id="sliderDisplay">{{sliderValue}} {{this.fridgeItem.amount.unit}}</p> <label for="slider">Mengde tatt av varen:</label> - <input type = "range" id = "slider" name = "slider" min="0" :max = "this.maxValue" step="this.stepIncrentValue" v-model="sliderValue"> + <input type = "range" id = "slider" name = "slider" min="0" :max = "this.maxValue" :step = this.getStep v-model="sliderValue"> <div id="buttons"> <button @click="logFoodAsDiscarded">Ble kastet</button> @@ -36,9 +36,8 @@ export default { }, data() { return { - visible:true, - sliderValue: 0, - stepIncrentValue:'1', + visible:true, + sliderValue: 0, } }, methods:{ @@ -47,22 +46,29 @@ export default { }, logFoodAsEaten(){ const fridgeAction = "CONSUMED"; + this.updateFridgeItem(fridgeAction) + }, + logFoodAsDiscarded(){ + const fridgeAction = "DISCARDED"; + this.updateFridgeItem(fridgeAction) + }, + updateFridgeItem(fridgeAction){ const ingredientId = this.fridgeItem.ingredient_id; const request = { action: fridgeAction, ingredients: { - [ingredientId]:{ - "quantity": this.sliderValue, - "unit": this.fridgeItem.amount.unit - } + [ingredientId]:{ + "quantity": this.sliderValue, + "unit": this.fridgeItem.amount.unit + } } }; - API.updateFridge(request).catch((error)=> console.log(error)); - this.close(); - }, - logFoodAsDiscarded(){ + API.updateFridge(request) + .catch((error)=> + console.log(error) + ); this.close(); }, extendExpiration(){ @@ -89,9 +95,9 @@ export default { }, isExpired(){ let date = this.fridgeItem.exp_date; - const epDate = new Date(date); + const expirationDate = new Date(date); - const parsedDate = Date.parse(epDate) + const parsedDate = Date.parse(expirationDate) const today = new Date(); const ms = parsedDate-today; @@ -100,6 +106,15 @@ export default { return numOfDays < 1 ; }, getStep(){ + const itemQuantity = this.fridgeItem.amount.quantity.toString(); + const isDecimal = itemQuantity.split('.').length === 2 || itemQuantity==='1'; + + switch(isDecimal){ + case true: + return '0.1'; + case false: + return '1'; + } return 0.1; }, } -- GitLab