| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div v-if="valueNotNull || edit"> | 
					
						
							|  |  |  |     <v-card class="mt-2"> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       <v-card-title class="pt-2 pb-0"> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |         {{ $t("recipe.nutrition") }} | 
					
						
							|  |  |  |       </v-card-title> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       <v-divider class="mx-2 my-1"></v-divider> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       <v-card-text v-if="edit"> | 
					
						
							|  |  |  |         <div v-for="(item, key, index) in value" :key="index"> | 
					
						
							|  |  |  |           <v-text-field | 
					
						
							| 
									
										
										
										
											2024-06-18 11:08:22 +02:00
										 |  |  | dense :value="value[key]" :label="labels[key].label" :suffix="labels[key].suffix" type="number" | 
					
						
							|  |  |  |             autocomplete="off" @input="updateValue(key, $event)"></v-text-field> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |         </div> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       <v-list v-if="showViewer" dense class="mt-0 pt-0"> | 
					
						
							| 
									
										
										
										
											2024-02-13 18:28:25 +01:00
										 |  |  |         <v-list-item v-for="(item, key, index) in renderedList" :key="index" style="min-height: 25px" dense> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |           <v-list-item-content> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |             <v-list-item-title class="pl-4 caption flex row"> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |               <div>{{ item.label }}</div> | 
					
						
							| 
									
										
										
										
											2024-02-13 18:28:25 +01:00
										 |  |  |               <div class="ml-auto mr-1">{{ item.value }}</div> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |               <div>{{ item.suffix }}</div> | 
					
						
							|  |  |  |             </v-list-item-title> | 
					
						
							|  |  |  |           </v-list-item-content> | 
					
						
							|  |  |  |         </v-list-item> | 
					
						
							|  |  |  |       </v-list> | 
					
						
							|  |  |  |     </v-card> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2024-06-18 11:08:22 +02:00
										 |  |  | import { computed, defineComponent } from "@nuxtjs/composition-api"; | 
					
						
							|  |  |  | import { useNutritionLabels } from "~/composables/recipes"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { Nutrition } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2024-06-18 11:08:22 +02:00
										 |  |  | import { NutritionLabelType } from "~/composables/recipes/use-recipe-nutrition"; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   props: { | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |     value: { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       type: Object as () => Nutrition, | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     edit: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |   setup(props, context) { | 
					
						
							| 
									
										
										
										
											2024-06-18 11:08:22 +02:00
										 |  |  |     const { labels } = useNutritionLabels(); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const valueNotNull = computed(() => { | 
					
						
							| 
									
										
										
										
											2022-06-25 12:19:04 -08:00
										 |  |  |       let key: keyof Nutrition; | 
					
						
							|  |  |  |       for (key in props.value) { | 
					
						
							|  |  |  |         if (props.value[key] !== null) { | 
					
						
							|  |  |  |           return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const showViewer = computed(() => !props.edit && valueNotNull.value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function updateValue(key: number | string, event: Event) { | 
					
						
							|  |  |  |       context.emit("input", { ...props.value, [key]: event }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-13 18:28:25 +01:00
										 |  |  |     // Build a new list that only contains nutritional information that has a value
 | 
					
						
							|  |  |  |     const renderedList = computed(() => { | 
					
						
							|  |  |  |       return Object.entries(labels).reduce((item: NutritionLabelType, [key, label]) => { | 
					
						
							|  |  |  |         if (props.value[key]?.trim()) { | 
					
						
							|  |  |  |           item[key] = { | 
					
						
							|  |  |  |             ...label, | 
					
						
							|  |  |  |             value: props.value[key], | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return item; | 
					
						
							|  |  |  |       }, {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     return { | 
					
						
							|  |  |  |       labels, | 
					
						
							|  |  |  |       valueNotNull, | 
					
						
							|  |  |  |       showViewer, | 
					
						
							| 
									
										
										
										
											2022-06-25 12:19:04 -08:00
										 |  |  |       updateValue, | 
					
						
							| 
									
										
										
										
											2024-02-13 18:28:25 +01:00
										 |  |  |       renderedList, | 
					
						
							| 
									
										
										
										
											2022-06-25 12:19:04 -08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style lang="scss" scoped></style> |