diff --git a/src/components/DosageCalculator.vue b/src/components/DosageCalculator.vue index c050628..1660413 100644 --- a/src/components/DosageCalculator.vue +++ b/src/components/DosageCalculator.vue @@ -5,16 +5,20 @@ Du må først fylle ut IK og IF verdier før du kan kalkulere dosen.

-

- Din anbefalte dose: {{ recommendedDose }} -

+

Blodsukkermål: {{ bloodsugarGoal }}

+

+

Din anbefalte dose: {{ roundToNearestHalf(recommendedDose) }}

+

Bakgrunnstall: {{ recommendedDose }}

@@ -23,17 +27,42 @@ name: 'DosageCalculator', data() { return { - bloodsugar: null, - carbohydrates: null + measuredBloodsugar: null, + plannedCarbohydratesIntake: null, + isTrainingMode: false } }, computed: { recommendedDose: function () { - const result = this.bloodsugar + this.carbohydrates + const { IKValue, IFValue } = this.$store.state + const {measuredBloodsugar, plannedCarbohydratesIntake, bloodsugarGoal} = this + const result = plannedCarbohydratesIntake / IKValue + (measuredBloodsugar - bloodsugarGoal) / IFValue return result ? result : 0 }, + bloodsugarGoal: function () { + const defaultBloodSugarGoal = 5 + const trainingBloodsugarGoal = 8 + return this.isTrainingMode ? trainingBloodsugarGoal : defaultBloodSugarGoal + }, disableInputFields: function () { return !(this.$store.state.IKValue && this.$store.state.IFValue) + }, + shouldShowCalculation: function () { + return this.measuredBloodsugar !== null || this.plannedCarbohydratesIntake !== null + } + }, + methods: { + roundToNearestHalf: function (num) { + const decimals = num % 1 + let adjustment = 0.0 + if (decimals >= 0.3 && decimals <= 0.7) { + adjustment = 0.5 + } else if (decimals > 0.7) { + adjustment = 1.0 + } else if (decimals < 0.3) { + adjustment = 0.0 + } + return (num - decimals) + adjustment } } } diff --git a/src/components/UserSettings.vue b/src/components/UserSettings.vue index d569b02..6ded9f8 100644 --- a/src/components/UserSettings.vue +++ b/src/components/UserSettings.vue @@ -1,16 +1,15 @@ @@ -23,8 +22,9 @@ return { IKValue, IFValue } }, methods: { - handleSubmit: function () { + changeListener: function () { const { IKValue, IFValue } = this + console.log('Handle submit was called'); this.$store.commit('updateIKAndIFValues', { IKValue, IFValue }) } } diff --git a/src/index.js b/src/index.js index 45d431c..62b3aab 100644 --- a/src/index.js +++ b/src/index.js @@ -7,9 +7,8 @@ Vue.config.productionTip = false const store = new Vuex.Store({ state: { - count: 0, - IKValue: parseInt(window.localStorage.getItem("IKValue"), 10), - IFValue: parseInt(window.localStorage.getItem("IFValue"), 10) + IKValue: parseFloat(window.localStorage.getItem("IKValue")) || 0, + IFValue: parseFloat(window.localStorage.getItem("IFValue")) || 0, }, mutations: { updateIKAndIFValues(state, { IKValue, IFValue }) {