| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |     <v-form ref="domUrlForm" @submit.prevent="createRecipe"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |       <div> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <v-card-title class="headline"> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |           {{ $t("recipe.create-recipe-from-an-image") }} | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         </v-card-title> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |         <v-card-text> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |           <p>{{ $t("recipe.create-recipe-from-an-image-description") }}</p> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |           <v-container class="pa-0"> | 
					
						
							|  |  |  |             <v-row> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |               <v-col cols="auto" align-self="center"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                 <AppButtonUpload | 
					
						
							|  |  |  |                   class="ml-auto" | 
					
						
							|  |  |  |                   url="none" | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                   file-name="images" | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                   accept="image/*" | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                   :text="uploadedImages.length ? $t('recipe.upload-more-images') : $t('recipe.upload-images')" | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                   :text-btn="false" | 
					
						
							|  |  |  |                   :post="false" | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                   :multiple="true" | 
					
						
							|  |  |  |                   @uploaded="uploadImages" | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                 /> | 
					
						
							|  |  |  |               </v-col> | 
					
						
							|  |  |  |               <v-spacer /> | 
					
						
							|  |  |  |             </v-row> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |             <div v-if="uploadedImages.length" class="mt-3"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |               <v-row> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                 <v-col cols="12" class="pb-0"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                   <v-card-text class="pa-0"> | 
					
						
							|  |  |  |                     <p class="mb-0"> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                       {{ $t("recipe.crop-and-rotate-the-image") }} | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                     </p> | 
					
						
							|  |  |  |                   </v-card-text> | 
					
						
							|  |  |  |                 </v-col> | 
					
						
							|  |  |  |               </v-row> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |               <v-row style="max-width: 600px"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                 <v-spacer /> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |                 <v-col v-for="(imageUrl, index) in uploadedImagesPreviewUrls" :key="index" cols="12"> | 
					
						
							|  |  |  |                   <v-row> | 
					
						
							|  |  |  |                     <v-col cols="auto" align-self="center"> | 
					
						
							|  |  |  |                       <ImageCropper | 
					
						
							|  |  |  |                         :img="imageUrl" | 
					
						
							|  |  |  |                         cropper-height="100%" | 
					
						
							|  |  |  |                         cropper-width="100%" | 
					
						
							|  |  |  |                         @save="(croppedImage) => updateUploadedImage(index, croppedImage)" | 
					
						
							|  |  |  |                       /> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                       <v-btn color="error" @click="() => clearImage(index)"> | 
					
						
							|  |  |  |                         <v-icon start> | 
					
						
							|  |  |  |                           {{ $globals.icons.close }} | 
					
						
							|  |  |  |                         </v-icon> | 
					
						
							|  |  |  |                         {{ $t("recipe.remove-image") }} | 
					
						
							|  |  |  |                       </v-btn> | 
					
						
							|  |  |  |                     </v-col> | 
					
						
							|  |  |  |                   </v-row> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |                 </v-col> | 
					
						
							|  |  |  |                 <v-spacer /> | 
					
						
							|  |  |  |               </v-row> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </v-container> | 
					
						
							|  |  |  |         </v-card-text> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |         <v-card-actions v-if="uploadedImages.length"> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |           <div> | 
					
						
							|  |  |  |             <p style="width: 250px"> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |               <BaseButton rounded block type="submit" :loading="loading" /> | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |             </p> | 
					
						
							|  |  |  |             <p> | 
					
						
							|  |  |  |               <v-checkbox | 
					
						
							|  |  |  |                 v-model="shouldTranslate" | 
					
						
							|  |  |  |                 hide-details | 
					
						
							|  |  |  |                 :label="$t('recipe.should-translate-description')" | 
					
						
							|  |  |  |                 :disabled="loading" | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             </p> | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |             <p v-if="loading" class="mb-0"> | 
					
						
							|  |  |  |               {{ | 
					
						
							|  |  |  |                 uploadedImages.length > 1 | 
					
						
							|  |  |  |                   ? $t("recipe.please-wait-images-processing") | 
					
						
							|  |  |  |                   : $t("recipe.please-wait-image-procesing") | 
					
						
							|  |  |  |               }} | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |             </p> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </v-card-actions> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </v-form> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							|  |  |  | import { alert } from "~/composables/use-toast"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { VForm } from "~/types/auto-forms"; | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | export default defineNuxtComponent({ | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |   setup() { | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       loading: false, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const i18n = useI18n(); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     const api = useUserApi(); | 
					
						
							|  |  |  |     const route = useRoute(); | 
					
						
							|  |  |  |     const router = useRouter(); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const groupSlug = computed(() => route.params.groupSlug || ""); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const domUrlForm = ref<VForm | null>(null); | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |     const uploadedImages = ref<(Blob | File)[]>([]); | 
					
						
							|  |  |  |     const uploadedImageNames = ref<string[]>([]); | 
					
						
							|  |  |  |     const uploadedImagesPreviewUrls = ref<string[]>([]); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     const shouldTranslate = ref(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |     function uploadImages(files: File[]) { | 
					
						
							|  |  |  |       uploadedImages.value = [...uploadedImages.value, ...files]; | 
					
						
							|  |  |  |       uploadedImageNames.value = [...uploadedImageNames.value, ...files.map(file => file.name)]; | 
					
						
							|  |  |  |       uploadedImagesPreviewUrls.value = [ | 
					
						
							|  |  |  |         ...uploadedImagesPreviewUrls.value, | 
					
						
							|  |  |  |         ...files.map(file => URL.createObjectURL(file)), | 
					
						
							|  |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |     function clearImage(index: number) { | 
					
						
							|  |  |  |       URL.revokeObjectURL(uploadedImagesPreviewUrls.value[index]); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       uploadedImages.value = uploadedImages.value.filter((_, i) => i !== index); | 
					
						
							|  |  |  |       uploadedImageNames.value = uploadedImageNames.value.filter((_, i) => i !== index); | 
					
						
							|  |  |  |       uploadedImagesPreviewUrls.value = uploadedImagesPreviewUrls.value.filter((_, i) => i !== index); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function createRecipe() { | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       if (uploadedImages.value.length === 0) { | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       state.loading = true; | 
					
						
							|  |  |  |       const translateLanguage = shouldTranslate.value ? i18n.locale : undefined; | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       const { data, error } = await api.recipes.createOneFromImages(uploadedImages.value, translateLanguage?.value); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |       if (error || !data) { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         alert.error(i18n.t("events.something-went-wrong")); | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |         state.loading = false; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |         router.push(`/g/${groupSlug.value}/r/${data}`); | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function updateUploadedImage(index: number, croppedImage: Blob) { | 
					
						
							|  |  |  |       uploadedImages.value[index] = croppedImage; | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...toRefs(state), | 
					
						
							|  |  |  |       domUrlForm, | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       uploadedImages, | 
					
						
							|  |  |  |       uploadedImagesPreviewUrls, | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |       shouldTranslate, | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       uploadImages, | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |       clearImage, | 
					
						
							|  |  |  |       createRecipe, | 
					
						
							| 
									
										
										
										
											2025-06-28 22:11:12 +02:00
										 |  |  |       updateUploadedImage, | 
					
						
							| 
									
										
										
										
											2024-08-17 17:07:01 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |