mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
Nutrition
This commit is contained in:
parent
a8d4c43467
commit
1c31e9c90e
1 changed files with 33 additions and 47 deletions
|
@ -45,62 +45,48 @@
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
import { useNutritionLabels } from "~/composables/recipes";
|
import { useNutritionLabels } from "~/composables/recipes";
|
||||||
import type { Nutrition } from "~/lib/api/types/recipe";
|
import type { Nutrition } from "~/lib/api/types/recipe";
|
||||||
import type { NutritionLabelType } from "~/composables/recipes/use-recipe-nutrition";
|
import type { NutritionLabelType } from "~/composables/recipes/use-recipe-nutrition";
|
||||||
|
|
||||||
export default defineNuxtComponent({
|
interface Props {
|
||||||
props: {
|
edit?: boolean;
|
||||||
modelValue: {
|
}
|
||||||
type: Object as () => Nutrition,
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
required: true,
|
edit: true,
|
||||||
},
|
});
|
||||||
edit: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
emits: ["update:modelValue"],
|
|
||||||
setup(props, context) {
|
|
||||||
const { labels } = useNutritionLabels();
|
|
||||||
const valueNotNull = computed(() => {
|
|
||||||
let key: keyof Nutrition;
|
|
||||||
for (key in props.modelValue) {
|
|
||||||
if (props.modelValue[key] !== null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
const showViewer = computed(() => !props.edit && valueNotNull.value);
|
const modelValue = defineModel<Nutrition>({ required: true });
|
||||||
|
|
||||||
function updateValue(key: number | string, event: Event) {
|
const { labels } = useNutritionLabels();
|
||||||
context.emit("update:modelValue", { ...props.modelValue, [key]: event });
|
const valueNotNull = computed(() => {
|
||||||
|
let key: keyof Nutrition;
|
||||||
|
for (key in modelValue.value) {
|
||||||
|
if (modelValue.value[key] !== null) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
// Build a new list that only contains nutritional information that has a value
|
const showViewer = computed(() => !props.edit && valueNotNull.value);
|
||||||
const renderedList = computed(() => {
|
|
||||||
return Object.entries(labels).reduce((item: NutritionLabelType, [key, label]) => {
|
|
||||||
if (props.modelValue[key]?.trim()) {
|
|
||||||
item[key] = {
|
|
||||||
...label,
|
|
||||||
value: props.modelValue[key],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
}, {});
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
function updateValue(key: number | string, event: Event) {
|
||||||
labels,
|
modelValue.value = { ...modelValue.value, [key]: event };
|
||||||
valueNotNull,
|
}
|
||||||
showViewer,
|
|
||||||
updateValue,
|
// Build a new list that only contains nutritional information that has a value
|
||||||
renderedList,
|
const renderedList = computed(() => {
|
||||||
};
|
return Object.entries(labels).reduce((item: NutritionLabelType, [key, label]) => {
|
||||||
},
|
if (modelValue.value[key]?.trim()) {
|
||||||
|
item[key] = {
|
||||||
|
...label,
|
||||||
|
value: modelValue.value[key],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}, {});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue