| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div class="text-center"> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     <BaseDialog | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |       ref="domConfirmDelete" | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |       :title="$t('recipe.delete-recipe')" | 
					
						
							|  |  |  |       color="error" | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |       :icon="$globals.icons.alertCircle" | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       @confirm="deleteRecipe()" | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							|  |  |  |         {{ $t("recipe.delete-confirmation") }} | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     <BaseDialog | 
					
						
							|  |  |  |       ref="domMealplanDialog" | 
					
						
							|  |  |  |       title="Add Recipe to Mealplan" | 
					
						
							|  |  |  |       color="primary" | 
					
						
							|  |  |  |       :icon="$globals.icons.calendar" | 
					
						
							|  |  |  |       @confirm="addRecipeToPlan()" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							|  |  |  |         <v-menu | 
					
						
							|  |  |  |           v-model="pickerMenu" | 
					
						
							|  |  |  |           :close-on-content-click="false" | 
					
						
							|  |  |  |           transition="scale-transition" | 
					
						
							|  |  |  |           offset-y | 
					
						
							|  |  |  |           max-width="290px" | 
					
						
							|  |  |  |           min-width="auto" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           <template #activator="{ on, attrs }"> | 
					
						
							|  |  |  |             <v-text-field | 
					
						
							|  |  |  |               v-model="newMealdate" | 
					
						
							|  |  |  |               label="Date" | 
					
						
							|  |  |  |               hint="MM/DD/YYYY format" | 
					
						
							|  |  |  |               persistent-hint | 
					
						
							|  |  |  |               :prepend-icon="$globals.icons.calendar" | 
					
						
							|  |  |  |               v-bind="attrs" | 
					
						
							|  |  |  |               readonly | 
					
						
							|  |  |  |               v-on="on" | 
					
						
							|  |  |  |             ></v-text-field> | 
					
						
							|  |  |  |           </template> | 
					
						
							|  |  |  |           <v-date-picker v-model="newMealdate" no-title @input="pickerMenu = false"></v-date-picker> | 
					
						
							|  |  |  |         </v-menu> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							| 
									
										
										
										
											2021-06-10 18:32:03 -08:00
										 |  |  |     <v-menu | 
					
						
							|  |  |  |       offset-y | 
					
						
							|  |  |  |       left | 
					
						
							|  |  |  |       :bottom="!menuTop" | 
					
						
							|  |  |  |       :nudge-bottom="!menuTop ? '5' : '0'" | 
					
						
							|  |  |  |       :top="menuTop" | 
					
						
							|  |  |  |       :nudge-top="menuTop ? '5' : '0'" | 
					
						
							|  |  |  |       allow-overflow | 
					
						
							| 
									
										
										
										
											2021-06-12 22:23:23 -08:00
										 |  |  |       close-delay="125" | 
					
						
							|  |  |  |       open-on-hover | 
					
						
							| 
									
										
										
										
											2021-06-21 16:25:37 -07:00
										 |  |  |       content-class="d-print-none" | 
					
						
							| 
									
										
										
										
											2021-06-10 18:32:03 -08:00
										 |  |  |     > | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       <template #activator="{ on, attrs }"> | 
					
						
							| 
									
										
										
										
											2021-06-12 22:23:23 -08:00
										 |  |  |         <v-btn :fab="fab" :small="fab" :color="color" :icon="!fab" dark v-bind="attrs" v-on="on" @click.prevent> | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |           <v-icon>{{ icon }}</v-icon> | 
					
						
							| 
									
										
										
										
											2021-06-12 22:23:23 -08:00
										 |  |  |         </v-btn> | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |       </template> | 
					
						
							|  |  |  |       <v-list dense> | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |         <v-list-item v-for="(item, index) in menuItems" :key="index" @click="contextMenuEventHandler(item.event)"> | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |           <v-list-item-icon> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |             <v-icon :color="item.color" v-text="item.icon"></v-icon> | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |           </v-list-item-icon> | 
					
						
							|  |  |  |           <v-list-item-title>{{ item.title }}</v-list-item-title> | 
					
						
							|  |  |  |         </v-list-item> | 
					
						
							|  |  |  |       </v-list> | 
					
						
							|  |  |  |     </v-menu> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { defineComponent, reactive, ref, toRefs, useContext, useRouter } from "@nuxtjs/composition-api"; | 
					
						
							|  |  |  | import { useClipboard, useShare } from "@vueuse/core"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  | import { alert } from "~/composables/use-toast"; | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface ContextMenuIncludes { | 
					
						
							|  |  |  |   delete: boolean; | 
					
						
							|  |  |  |   edit: boolean; | 
					
						
							|  |  |  |   download: boolean; | 
					
						
							|  |  |  |   mealplanner: boolean; | 
					
						
							|  |  |  |   print: boolean; | 
					
						
							|  |  |  |   share: boolean; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface ContextMenuItem { | 
					
						
							|  |  |  |   title: string; | 
					
						
							|  |  |  |   icon: string; | 
					
						
							|  |  |  |   color: string; | 
					
						
							|  |  |  |   event: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |   props: { | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     useItems: { | 
					
						
							|  |  |  |       type: Object as () => ContextMenuIncludes, | 
					
						
							|  |  |  |       default: () => ({ | 
					
						
							|  |  |  |         delete: true, | 
					
						
							|  |  |  |         edit: true, | 
					
						
							|  |  |  |         download: true, | 
					
						
							|  |  |  |         mealplanner: true, | 
					
						
							|  |  |  |         print: true, | 
					
						
							|  |  |  |         share: true, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     // Append items are added at the end of the useItems list
 | 
					
						
							|  |  |  |     appendItems: { | 
					
						
							|  |  |  |       type: Array as () => ContextMenuItem[], | 
					
						
							|  |  |  |       default: () => [], | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     // Append items are added at the beginning of the useItems list
 | 
					
						
							|  |  |  |     leadingItems: { | 
					
						
							|  |  |  |       type: Array as () => ContextMenuItem[], | 
					
						
							|  |  |  |       default: () => [], | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-06-10 18:32:03 -08:00
										 |  |  |     menuTop: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     fab: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     color: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "primary", | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |     slug: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       required: true, | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     menuIcon: { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-06-16 18:55:32 -08:00
										 |  |  |       default: null, | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-06-07 20:54:34 +02:00
										 |  |  |     name: { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       required: true, | 
					
						
							| 
									
										
										
										
											2021-06-07 20:54:34 +02:00
										 |  |  |       type: String, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     recipeId: { | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |       type: Number, | 
					
						
							| 
									
										
										
										
											2021-06-10 18:32:03 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |   setup(props, context) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       loading: false, | 
					
						
							|  |  |  |       menuItems: [] as ContextMenuItem[], | 
					
						
							|  |  |  |       newMealdate: "", | 
					
						
							|  |  |  |       pickerMenu: false, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							|  |  |  |     const { i18n, $globals } = useContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // ===========================================================================
 | 
					
						
							|  |  |  |     // Context Menu Setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const defaultItems: { [key: string]: ContextMenuItem } = { | 
					
						
							|  |  |  |       edit: { | 
					
						
							|  |  |  |         title: i18n.t("general.edit") as string, | 
					
						
							|  |  |  |         icon: $globals.icons.edit, | 
					
						
							|  |  |  |         color: "primary", | 
					
						
							|  |  |  |         event: "edit", | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       delete: { | 
					
						
							|  |  |  |         title: i18n.t("general.delete") as string, | 
					
						
							|  |  |  |         icon: $globals.icons.delete, | 
					
						
							|  |  |  |         color: "error", | 
					
						
							|  |  |  |         event: "delete", | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       download: { | 
					
						
							|  |  |  |         title: i18n.t("general.download") as string, | 
					
						
							|  |  |  |         icon: $globals.icons.download, | 
					
						
							|  |  |  |         color: "primary", | 
					
						
							|  |  |  |         event: "download", | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       mealplanner: { | 
					
						
							|  |  |  |         title: "Add to Plan", | 
					
						
							|  |  |  |         icon: $globals.icons.calendar, | 
					
						
							|  |  |  |         color: "primary", | 
					
						
							|  |  |  |         event: "mealplanner", | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       print: { | 
					
						
							|  |  |  |         title: i18n.t("general.print") as string, | 
					
						
							|  |  |  |         icon: $globals.icons.printer, | 
					
						
							|  |  |  |         color: "primary", | 
					
						
							|  |  |  |         event: "print", | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       share: { | 
					
						
							|  |  |  |         title: i18n.t("general.share") as string, | 
					
						
							|  |  |  |         icon: $globals.icons.shareVariant, | 
					
						
							|  |  |  |         color: "primary", | 
					
						
							|  |  |  |         event: "share", | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Get Default Menu Items Specified in Props
 | 
					
						
							|  |  |  |     for (const [key, value] of Object.entries(props.useItems)) { | 
					
						
							|  |  |  |       if (value) { | 
					
						
							|  |  |  |         const item = defaultItems[key]; | 
					
						
							|  |  |  |         if (item) { | 
					
						
							|  |  |  |           state.menuItems.push(item); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-21 16:25:37 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Add leading and Apppending Items
 | 
					
						
							|  |  |  |     state.menuItems = [...state.menuItems, ...props.leadingItems, ...props.appendItems]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const icon = props.menuIcon || $globals.icons.dotsVertical; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getRecipeUrl() { | 
					
						
							|  |  |  |       return `${window.location.origin}/recipe/${props.slug}`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getRecipeText() { | 
					
						
							|  |  |  |       return i18n.t("recipe.share-recipe-message", [props.name]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // ===========================================================================
 | 
					
						
							|  |  |  |     // Context Menu Event Handler
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const router = useRouter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const domConfirmDelete = ref(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function deleteRecipe() { | 
					
						
							|  |  |  |       await api.recipes.deleteOne(props.slug); | 
					
						
							|  |  |  |       context.emit("delete", props.slug); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function handleDownloadEvent() { | 
					
						
							|  |  |  |       const { data } = await api.recipes.getZipToken(props.slug); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         window.open(api.recipes.getZipRedirectUrl(props.slug, data.token)); | 
					
						
							| 
									
										
										
										
											2021-06-21 16:25:37 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { share, isSupported: shareIsSupported } = useShare(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const source = ref(""); | 
					
						
							|  |  |  |     const { copy } = useClipboard({ source }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function handleShareEvent() { | 
					
						
							|  |  |  |       if (shareIsSupported) { | 
					
						
							|  |  |  |         share({ | 
					
						
							|  |  |  |           title: props.name, | 
					
						
							|  |  |  |           url: getRecipeUrl(), | 
					
						
							|  |  |  |           text: getRecipeText() as string, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         await copy(getRecipeUrl()); | 
					
						
							|  |  |  |         alert.success("Recipe link copied to clipboard"); | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 21:29:15 -08:00
										 |  |  |     const domMealplanDialog = ref(null); | 
					
						
							|  |  |  |     async function addRecipeToPlan() { | 
					
						
							|  |  |  |       const { response } = await api.mealplans.createOne({ | 
					
						
							|  |  |  |         date: state.newMealdate, | 
					
						
							|  |  |  |         entryType: "dinner", | 
					
						
							|  |  |  |         title: "", | 
					
						
							|  |  |  |         text: "", | 
					
						
							|  |  |  |         recipeId: props.recipeId, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (response?.status === 201) { | 
					
						
							|  |  |  |         alert.success("Recipe added to mealplan"); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         alert.error("Failed to add recipe to mealplan"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Note: Print is handled as an event in the parent component
 | 
					
						
							|  |  |  |     const eventHandlers: { [key: string]: Function } = { | 
					
						
							|  |  |  |       // @ts-ignore - Doens't know about open()
 | 
					
						
							|  |  |  |       delete: () => domConfirmDelete?.value?.open(), | 
					
						
							|  |  |  |       edit: () => router.push(`/recipe/${props.slug}` + "?edit=true"), | 
					
						
							|  |  |  |       download: handleDownloadEvent, | 
					
						
							|  |  |  |       // @ts-ignore - Doens't know about open()
 | 
					
						
							|  |  |  |       mealplanner: () => domMealplanDialog?.value?.open(), | 
					
						
							|  |  |  |       share: handleShareEvent, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function contextMenuEventHandler(eventKey: string) { | 
					
						
							|  |  |  |       const handler = eventHandlers[eventKey]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (handler && typeof handler === "function") { | 
					
						
							|  |  |  |         handler(); | 
					
						
							|  |  |  |         state.loading = false; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context.emit(eventKey); | 
					
						
							|  |  |  |       state.loading = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...toRefs(state), | 
					
						
							|  |  |  |       contextMenuEventHandler, | 
					
						
							|  |  |  |       deleteRecipe, | 
					
						
							|  |  |  |       addRecipeToPlan, | 
					
						
							|  |  |  |       domConfirmDelete, | 
					
						
							|  |  |  |       domMealplanDialog, | 
					
						
							|  |  |  |       icon, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-01 20:46:02 -08:00
										 |  |  | </script> |