| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							|  |  |  |     <RecipeIngredients | 
					
						
							|  |  |  |       :value="recipe.recipeIngredient" | 
					
						
							|  |  |  |       :scale="scale" | 
					
						
							|  |  |  |       :disable-amount="recipe.settings.disableAmount" | 
					
						
							| 
									
										
										
										
											2024-11-11 12:21:44 +01:00
										 |  |  |       :is-cook-mode="isCookMode" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     /> | 
					
						
							|  |  |  |     <div v-if="!isEditMode && recipe.tools && recipe.tools.length > 0"> | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |       <h2 class="mb-2 mt-4">{{ $t('tool.required-tools') }}</h2> | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       <v-list-item v-for="(tool, index) in recipe.tools" :key="index" dense> | 
					
						
							|  |  |  |         <v-checkbox | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |           v-model="recipeTools[index].onHand" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |           hide-details | 
					
						
							|  |  |  |           class="pt-0 my-auto py-auto" | 
					
						
							|  |  |  |           color="secondary" | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |           @change="updateTool(index)" | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |         > | 
					
						
							|  |  |  |         </v-checkbox> | 
					
						
							|  |  |  |         <v-list-item-content> | 
					
						
							|  |  |  |           {{ tool.name }} | 
					
						
							|  |  |  |         </v-list-item-content> | 
					
						
							|  |  |  |       </v-list-item> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | import { computed, defineComponent } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | import { useLoggedInState } from "~/composables/use-logged-in-state"; | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  | import { usePageState, usePageUser } from "~/composables/recipe-page/shared-state"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import { useToolStore } from "~/composables/store"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { NoUndefinedField } from "~/lib/api/types/non-generated"; | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | import { Recipe, RecipeTool } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | import RecipeIngredients from "~/components/Domain/Recipe/RecipeIngredients.vue"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | interface RecipeToolWithOnHand extends RecipeTool { | 
					
						
							|  |  |  |   onHand: boolean; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   components: { | 
					
						
							|  |  |  |     RecipeIngredients, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   props: { | 
					
						
							|  |  |  |     recipe: { | 
					
						
							|  |  |  |       type: Object as () => NoUndefinedField<Recipe>, | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     scale: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-11-11 12:21:44 +01:00
										 |  |  |     isCookMode: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   setup(props) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const { isOwnGroup } = useLoggedInState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const toolStore = isOwnGroup.value ? useToolStore() : null; | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |     const { user } = usePageUser(); | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     const { isEditMode } = usePageState(props.recipe.slug); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const recipeTools = computed(() => { | 
					
						
							|  |  |  |       if (!(user.householdSlug && toolStore)) { | 
					
						
							|  |  |  |         return props.recipe.tools.map((tool) => ({ ...tool, onHand: false }) as RecipeToolWithOnHand); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return props.recipe.tools.map((tool) => { | 
					
						
							|  |  |  |           const onHand = tool.householdsWithTool?.includes(user.householdSlug) || false; | 
					
						
							|  |  |  |           return { ...tool, onHand } as RecipeToolWithOnHand; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |     function updateTool(index: number) { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       if (user.id && user.householdSlug && toolStore) { | 
					
						
							|  |  |  |         const tool = recipeTools.value[index]; | 
					
						
							|  |  |  |         if (tool.onHand && !tool.householdsWithTool?.includes(user.householdSlug)) { | 
					
						
							|  |  |  |           if (!tool.householdsWithTool) { | 
					
						
							|  |  |  |             tool.householdsWithTool = [user.householdSlug]; | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             tool.householdsWithTool.push(user.householdSlug); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } else if (!tool.onHand && tool.householdsWithTool?.includes(user.householdSlug)) { | 
					
						
							|  |  |  |           tool.householdsWithTool = tool.householdsWithTool.filter((household) => household !== user.householdSlug); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         toolStore.actions.updateOne(tool); | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         console.log("no user, skipping server update"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     return { | 
					
						
							|  |  |  |       toolStore, | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       recipeTools, | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |       isEditMode, | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |       updateTool, | 
					
						
							| 
									
										
										
										
											2022-08-27 10:44:58 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |