| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							|  |  |  |     <h2 class="mb-4">{{ $t("recipe.ingredients") }}</h2> | 
					
						
							|  |  |  |     <draggable | 
					
						
							|  |  |  |       v-if="recipe.recipeIngredient.length > 0" | 
					
						
							|  |  |  |       v-model="recipe.recipeIngredient" | 
					
						
							|  |  |  |       handle=".handle" | 
					
						
							| 
									
										
										
										
											2024-10-24 11:24:42 -05:00
										 |  |  |       delay="250" | 
					
						
							| 
									
										
										
										
											2024-10-25 14:49:07 -05:00
										 |  |  |       :delay-on-touch-only="true" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       v-bind="{ | 
					
						
							|  |  |  |         animation: 200, | 
					
						
							| 
									
										
										
										
											2024-06-21 16:42:05 -05:00
										 |  |  |         group: 'recipe-ingredients', | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |         disabled: false, | 
					
						
							|  |  |  |         ghostClass: 'ghost', | 
					
						
							|  |  |  |       }" | 
					
						
							|  |  |  |       @start="drag = true" | 
					
						
							|  |  |  |       @end="drag = false" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <TransitionGroup type="transition" :name="!drag ? 'flip-list' : ''"> | 
					
						
							|  |  |  |         <RecipeIngredientEditor | 
					
						
							|  |  |  |           v-for="(ingredient, index) in recipe.recipeIngredient" | 
					
						
							|  |  |  |           :key="ingredient.referenceId" | 
					
						
							|  |  |  |           v-model="recipe.recipeIngredient[index]" | 
					
						
							|  |  |  |           class="list-group-item" | 
					
						
							|  |  |  |           :disable-amount="recipe.settings.disableAmount" | 
					
						
							|  |  |  |           @delete="recipe.recipeIngredient.splice(index, 1)" | 
					
						
							| 
									
										
										
										
											2024-06-18 06:45:12 -07:00
										 |  |  |           @insert-above="insertNewIngredient(index)" | 
					
						
							|  |  |  |           @insert-below="insertNewIngredient(index+1)" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |         /> | 
					
						
							|  |  |  |       </TransitionGroup> | 
					
						
							|  |  |  |     </draggable> | 
					
						
							|  |  |  |     <v-skeleton-loader v-else boilerplate elevation="2" type="list-item"> </v-skeleton-loader> | 
					
						
							| 
									
										
										
										
											2023-11-24 10:40:35 +01:00
										 |  |  |     <div class="d-flex flex-wrap justify-center justify-sm-end mt-3"> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       <v-tooltip top color="accent"> | 
					
						
							|  |  |  |         <template #activator="{ on, attrs }"> | 
					
						
							|  |  |  |           <span v-on="on"> | 
					
						
							|  |  |  |             <BaseButton | 
					
						
							| 
									
										
										
										
											2023-10-07 21:36:47 +02:00
										 |  |  |               class="mb-1" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |               :disabled="recipe.settings.disableAmount || hasFoodOrUnit" | 
					
						
							|  |  |  |               color="accent" | 
					
						
							| 
									
										
										
										
											2023-11-07 18:15:07 -05:00
										 |  |  |               :to="`/g/${groupSlug}/r/${recipe.slug}/ingredient-parser`" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |               v-bind="attrs" | 
					
						
							|  |  |  |             > | 
					
						
							|  |  |  |               <template #icon> | 
					
						
							|  |  |  |                 {{ $globals.icons.foods }} | 
					
						
							|  |  |  |               </template> | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |               {{ $t('recipe.parse') }} | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |             </BaseButton> | 
					
						
							|  |  |  |           </span> | 
					
						
							|  |  |  |         </template> | 
					
						
							|  |  |  |         <span>{{ parserToolTip }}</span> | 
					
						
							|  |  |  |       </v-tooltip> | 
					
						
							| 
									
										
										
										
											2023-10-07 21:36:47 +02:00
										 |  |  |       <RecipeDialogBulkAdd class="mx-1 mb-1" @bulk-data="addIngredient" /> | 
					
						
							| 
									
										
										
										
											2024-03-10 03:26:55 +11:00
										 |  |  |       <BaseButton class="mb-1" @click="addIngredient" > {{ $t("general.add") }} </BaseButton> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import draggable from "vuedraggable"; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | import { computed, defineComponent, ref, useContext, useRoute } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import { usePageState, usePageUser } from "~/composables/recipe-page/shared-state"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { NoUndefinedField } from "~/lib/api/types/non-generated"; | 
					
						
							|  |  |  | import { Recipe } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import RecipeIngredientEditor from "~/components/Domain/Recipe/RecipeIngredientEditor.vue"; | 
					
						
							|  |  |  | import RecipeDialogBulkAdd from "~/components/Domain/Recipe/RecipeDialogBulkAdd.vue"; | 
					
						
							|  |  |  | import { uuid4 } from "~/composables/use-utils"; | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   components: { | 
					
						
							|  |  |  |     draggable, | 
					
						
							|  |  |  |     RecipeDialogBulkAdd, | 
					
						
							|  |  |  |     RecipeIngredientEditor, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   props: { | 
					
						
							|  |  |  |     recipe: { | 
					
						
							|  |  |  |       type: Object as () => NoUndefinedField<Recipe>, | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   setup(props) { | 
					
						
							|  |  |  |     const { user } = usePageUser(); | 
					
						
							|  |  |  |     const { imageKey } = usePageState(props.recipe.slug); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const { $auth, i18n } = useContext(); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const drag = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const route = useRoute(); | 
					
						
							|  |  |  |     const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || ""); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     const hasFoodOrUnit = computed(() => { | 
					
						
							|  |  |  |       if (!props.recipe) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (props.recipe.recipeIngredient) { | 
					
						
							|  |  |  |         for (const ingredient of props.recipe.recipeIngredient) { | 
					
						
							|  |  |  |           if (ingredient.food || ingredient.unit) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const parserToolTip = computed(() => { | 
					
						
							|  |  |  |       if (props.recipe.settings.disableAmount) { | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |         return i18n.t("recipe.enable-ingredient-amounts-to-use-this-feature"); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       } else if (hasFoodOrUnit.value) { | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |         return i18n.t("recipe.recipes-with-units-or-foods-defined-cannot-be-parsed"); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |       return i18n.t("recipe.parse-ingredients"); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function addIngredient(ingredients: Array<string> | null = null) { | 
					
						
							|  |  |  |       if (ingredients?.length) { | 
					
						
							|  |  |  |         const newIngredients = ingredients.map((x) => { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             referenceId: uuid4(), | 
					
						
							|  |  |  |             title: "", | 
					
						
							|  |  |  |             note: x, | 
					
						
							|  |  |  |             unit: undefined, | 
					
						
							|  |  |  |             food: undefined, | 
					
						
							|  |  |  |             disableAmount: true, | 
					
						
							|  |  |  |             quantity: 1, | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (newIngredients) { | 
					
						
							|  |  |  |           // @ts-expect-error - prop can be null-type by NoUndefinedField type forces it to be set
 | 
					
						
							|  |  |  |           props.recipe.recipeIngredient.push(...newIngredients); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         props.recipe.recipeIngredient.push({ | 
					
						
							|  |  |  |           referenceId: uuid4(), | 
					
						
							|  |  |  |           title: "", | 
					
						
							|  |  |  |           note: "", | 
					
						
							|  |  |  |           // @ts-expect-error - prop can be null-type by NoUndefinedField type forces it to be set
 | 
					
						
							|  |  |  |           unit: undefined, | 
					
						
							|  |  |  |           // @ts-expect-error - prop can be null-type by NoUndefinedField type forces it to be set
 | 
					
						
							|  |  |  |           food: undefined, | 
					
						
							|  |  |  |           disableAmount: true, | 
					
						
							|  |  |  |           quantity: 1, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 06:45:12 -07:00
										 |  |  |     function insertNewIngredient(dest: number) { | 
					
						
							|  |  |  |       props.recipe.recipeIngredient.splice(dest, 0, { | 
					
						
							|  |  |  |             referenceId: uuid4(), | 
					
						
							|  |  |  |             title: "", | 
					
						
							|  |  |  |             note: "", | 
					
						
							|  |  |  |             // @ts-expect-error - prop can be null-type by NoUndefinedField type forces it to be set
 | 
					
						
							|  |  |  |             unit: undefined, | 
					
						
							|  |  |  |             // @ts-expect-error - prop can be null-type by NoUndefinedField type forces it to be set
 | 
					
						
							|  |  |  |             food: undefined, | 
					
						
							|  |  |  |             disableAmount: true, | 
					
						
							|  |  |  |             quantity: 1, | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     return { | 
					
						
							|  |  |  |       user, | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |       groupSlug, | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       addIngredient, | 
					
						
							|  |  |  |       parserToolTip, | 
					
						
							|  |  |  |       hasFoodOrUnit, | 
					
						
							|  |  |  |       imageKey, | 
					
						
							|  |  |  |       drag, | 
					
						
							| 
									
										
										
										
											2024-06-18 06:45:12 -07:00
										 |  |  |       insertNewIngredient, | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |