| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="dialog" | 
					
						
							|  |  |  |       :title="$t('recipe-share.share-recipe')" | 
					
						
							|  |  |  |       :icon="$globals.icons.link" | 
					
						
							|  |  |  |     > | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       <v-card-text> | 
					
						
							|  |  |  |         <v-menu | 
					
						
							|  |  |  |           v-model="datePickerMenu" | 
					
						
							|  |  |  |           :close-on-content-click="false" | 
					
						
							|  |  |  |           transition="scale-transition" | 
					
						
							|  |  |  |           offset-y | 
					
						
							|  |  |  |           max-width="290px" | 
					
						
							|  |  |  |           min-width="auto" | 
					
						
							|  |  |  |         > | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           <template #activator="{ props }"> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |             <v-text-field | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |               v-model="expirationDateString" | 
					
						
							| 
									
										
										
										
											2022-08-10 07:12:45 +02:00
										 |  |  |               :label="$t('recipe-share.expiration-date')" | 
					
						
							|  |  |  |               :hint="$t('recipe-share.default-30-days')" | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |               persistent-hint | 
					
						
							|  |  |  |               :prepend-icon="$globals.icons.calendar" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |               v-bind="props" | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |               readonly | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |           </template> | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |           <v-date-picker | 
					
						
							|  |  |  |             v-model="expirationDate" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |             hide-header | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |             :first-day-of-week="firstDayOfWeek" | 
					
						
							|  |  |  |             :local="$i18n.locale" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |             @update:model-value="datePickerMenu = false" | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |           /> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |         </v-menu> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |       <v-card-actions class="justify-end"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <BaseButton | 
					
						
							|  |  |  |           size="small" | 
					
						
							|  |  |  |           @click="createNewToken" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           {{ $t("general.new") }} | 
					
						
							|  |  |  |         </BaseButton> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       </v-card-actions> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       <v-list-item | 
					
						
							|  |  |  |         v-for="token in tokens" | 
					
						
							|  |  |  |         :key="token.id" | 
					
						
							|  |  |  |         class="px-2" | 
					
						
							|  |  |  |         style="padding-top: 8px; padding-bottom: 8px;" | 
					
						
							|  |  |  |         @click="shareRecipe(token.id)" | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         <div class="d-flex align-center" style="width: 100%;"> | 
					
						
							|  |  |  |           <v-avatar color="grey"> | 
					
						
							|  |  |  |             <v-icon> | 
					
						
							|  |  |  |               {{ $globals.icons.link }} | 
					
						
							|  |  |  |             </v-icon> | 
					
						
							|  |  |  |           </v-avatar> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           <div class="pl-3 flex-grow-1"> | 
					
						
							|  |  |  |             <v-list-item-title> | 
					
						
							|  |  |  |               {{ $t("recipe-share.expires-at") }} | 
					
						
							|  |  |  |             </v-list-item-title> | 
					
						
							|  |  |  |             <v-list-item-subtitle> | 
					
						
							|  |  |  |               {{ $d(new Date(token.expiresAt!), "long") }} | 
					
						
							|  |  |  |             </v-list-item-subtitle> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           <v-btn | 
					
						
							|  |  |  |             icon | 
					
						
							|  |  |  |             variant="text" | 
					
						
							|  |  |  |             class="ml-2" | 
					
						
							|  |  |  |             @click.stop="deleteToken(token.id)" | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             <v-icon color="error-lighten-1"> | 
					
						
							|  |  |  |               {{ $globals.icons.delete }} | 
					
						
							|  |  |  |             </v-icon> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |           </v-btn> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           <v-btn | 
					
						
							|  |  |  |             icon | 
					
						
							|  |  |  |             variant="text" | 
					
						
							|  |  |  |             class="ml-2" | 
					
						
							|  |  |  |             @click.stop="copyTokenLink(token.id)" | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             <v-icon color="info-lighten-1"> | 
					
						
							|  |  |  |               {{ $globals.icons.contentCopy }} | 
					
						
							|  |  |  |             </v-icon> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |           </v-btn> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       </v-list-item> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2022-05-25 10:14:24 -08:00
										 |  |  | import { useClipboard, useShare, whenever } from "@vueuse/core"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { RecipeShareToken } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  | import { useHouseholdSelf } from "~/composables/use-households"; | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  | import { alert } from "~/composables/use-toast"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | export default defineNuxtComponent({ | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |   props: { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     modelValue: { | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     recipeId: { | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  |       type: String, | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |   emits: ["update:modelValue"], | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |   setup(props, context) { | 
					
						
							|  |  |  |     // V-Model Support
 | 
					
						
							|  |  |  |     const dialog = computed({ | 
					
						
							|  |  |  |       get: () => { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         return props.modelValue; | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       }, | 
					
						
							|  |  |  |       set: (val) => { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         context.emit("update:modelValue", val); | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       datePickerMenu: false, | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       expirationDate: new Date(Date.now() - new Date().getTimezoneOffset() * 60000), | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       tokens: [] as RecipeShareToken[], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const expirationDateString = computed(() => { | 
					
						
							|  |  |  |       return state.expirationDate.toISOString().substring(0, 10); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     whenever( | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       () => props.modelValue, | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       () => { | 
					
						
							|  |  |  |         // Set expiration date to today + 30 Days
 | 
					
						
							|  |  |  |         const today = new Date(); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         state.expirationDate = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000); | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |         refreshTokens(); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const i18n = useI18n(); | 
					
						
							|  |  |  |     const $auth = useMealieAuth(); | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     const { household } = useHouseholdSelf(); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const route = useRoute(); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const groupSlug = computed(() => route.params.groupSlug as string || $auth.user.value?.groupSlug || ""); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |     const firstDayOfWeek = computed(() => { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |       return household.value?.preferences?.firstDayOfWeek || 0; | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     // ============================================================
 | 
					
						
							|  |  |  |     // Token Actions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const userApi = useUserApi(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function createNewToken() { | 
					
						
							|  |  |  |       // Convert expiration date to timestamp
 | 
					
						
							|  |  |  |       const { data } = await userApi.recipes.share.createOne({ | 
					
						
							|  |  |  |         recipeId: props.recipeId, | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         expiresAt: state.expirationDate.toISOString(), | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         state.tokens.push(data); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function deleteToken(id: string) { | 
					
						
							|  |  |  |       await userApi.recipes.share.deleteOne(id); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       state.tokens = state.tokens.filter(token => token.id !== id); | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function refreshTokens() { | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |       const { data } = await userApi.recipes.share.getAll(1, -1, { recipe_id: props.recipeId }); | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							| 
									
										
										
										
											2022-08-28 20:08:33 -08:00
										 |  |  |         // @ts-expect-error - TODO: This routes doesn't have pagination, but the type are mismatched.
 | 
					
						
							|  |  |  |         state.tokens = data ?? []; | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { share, isSupported: shareIsSupported } = useShare(); | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |     const { copy, copied, isSupported } = useClipboard(); | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function getRecipeText() { | 
					
						
							|  |  |  |       return i18n.t("recipe.share-recipe-message", [props.name]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getTokenLink(token: string) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |       return `${window.location.origin}/g/${groupSlug.value}/shared/r/${token}`; | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function copyTokenLink(token: string) { | 
					
						
							| 
									
										
										
										
											2023-12-29 16:48:28 +01:00
										 |  |  |       if (isSupported.value) { | 
					
						
							|  |  |  |         await copy(getTokenLink(token)); | 
					
						
							|  |  |  |         if (copied.value) { | 
					
						
							|  |  |  |           alert.success(i18n.t("recipe-share.recipe-link-copied-message") as string); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |           alert.error(i18n.t("general.clipboard-copy-failure") as string); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         alert.error(i18n.t("general.clipboard-not-supported") as string); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function shareRecipe(token: string) { | 
					
						
							|  |  |  |       if (shareIsSupported) { | 
					
						
							|  |  |  |         share({ | 
					
						
							|  |  |  |           title: props.name, | 
					
						
							|  |  |  |           url: getTokenLink(token), | 
					
						
							|  |  |  |           text: getRecipeText() as string, | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |         await copyTokenLink(token); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...toRefs(state), | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       expirationDateString, | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       dialog, | 
					
						
							|  |  |  |       createNewToken, | 
					
						
							|  |  |  |       deleteToken, | 
					
						
							| 
									
										
										
										
											2024-03-12 17:46:34 -05:00
										 |  |  |       firstDayOfWeek, | 
					
						
							| 
									
										
										
										
											2021-12-05 11:55:46 -09:00
										 |  |  |       shareRecipe, | 
					
						
							|  |  |  |       copyTokenLink, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  | </script> |