Convert nutrition input from KJ to calories with preference

This commit is contained in:
xeals 2021-10-25 17:29:40 +11:00
parent 3d830a4449
commit c9ff0543e3
No known key found for this signature in database
GPG Key ID: A498C7AF27EC6B5C
3 changed files with 20 additions and 2 deletions

View File

@ -32,6 +32,9 @@
window.CUSTOM_LOCALE = '{{ request.LANGUAGE_CODE }}'
window.RECIPE_ID = {{ recipe.pk }}
window.DEFAULT_UNIT = '{{request.user.userpreference.default_unit}}'
window.USER_PREF = {
'use_kj': {% if request.user.userpreference.use_kj %} true {% else %} false {% endif %},
}
</script>

View File

@ -481,7 +481,7 @@ import {BootstrapVue} from 'bootstrap-vue'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import draggable from 'vuedraggable'
import {ApiMixin, resolveDjangoUrl, ResolveUrlMixin, StandardToasts} from "@/utils/utils";
import {ApiMixin, resolveDjangoUrl, ResolveUrlMixin, StandardToasts, convertEnergyToCalories} from "@/utils/utils";
import Multiselect from "vue-multiselect";
import {ApiApiFactory} from "@/utils/openapi/api";
import LoadingSpinner from "@/components/LoadingSpinner";
@ -605,11 +605,13 @@ export default {
updateRecipe: function (view_after) {
let apiFactory = new ApiApiFactory()
this.normalizeEnergy()
this.sortSteps()
for (let s of this.recipe.steps) {
this.sortIngredients(s)
}
apiFactory.updateRecipe(this.recipe_id, this.recipe,
{}).then((response) => {
console.log(response)
@ -812,6 +814,11 @@ export default {
el.select();
document.execCommand('copy');
document.body.removeChild(el);
},
normalizeEnergy: function () {
if (this.recipe.nutrition && this.recipe.nutrition.calories) {
this.recipe.nutrition.calories = convertEnergyToCalories(this.recipe.nutrition.calories)
}
}
}

View File

@ -167,6 +167,14 @@ export function calculateEnergy(amount, factor) {
}
}
export function convertEnergyToCalories(amount) {
if (getUserPreference('use_kj')) {
return amount / KILOJOULES_PER_CALORIE
} else {
return amount
}
}
/*
* Utility functions to use OpenAPIs generically
* */