| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2022-05-25 19:33:58 -08:00
										 |  |  |   <div> | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |     <v-card-title class="headline"> {{ $t('recipe.create-recipe') }} </v-card-title> | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |     <v-card-text> | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |       {{ $t('recipe.create-a-recipe-by-providing-the-name-all-recipes-must-have-unique-names') }} | 
					
						
							| 
									
										
										
										
											2023-09-28 18:16:22 -05:00
										 |  |  |       <v-form ref="domCreateByName" @submit.prevent> | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |         <v-text-field | 
					
						
							|  |  |  |           v-model="newRecipeName" | 
					
						
							|  |  |  |           :label="$t('recipe.recipe-name')" | 
					
						
							|  |  |  |           :prepend-inner-icon="$globals.icons.primary" | 
					
						
							|  |  |  |           validate-on-blur | 
					
						
							|  |  |  |           autofocus | 
					
						
							|  |  |  |           filled | 
					
						
							|  |  |  |           clearable | 
					
						
							|  |  |  |           class="rounded-lg mt-2" | 
					
						
							|  |  |  |           rounded | 
					
						
							|  |  |  |           :rules="[validators.required]" | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |           :hint="$t('recipe.new-recipe-names-must-be-unique')" | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |           persistent-hint | 
					
						
							| 
									
										
										
										
											2023-10-07 14:23:47 -05:00
										 |  |  |           @keyup.enter="createByName(newRecipeName)" | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |         ></v-text-field> | 
					
						
							|  |  |  |       </v-form> | 
					
						
							|  |  |  |     </v-card-text> | 
					
						
							|  |  |  |     <v-card-actions class="justify-center"> | 
					
						
							|  |  |  |       <div style="width: 250px"> | 
					
						
							|  |  |  |         <BaseButton | 
					
						
							| 
									
										
										
										
											2024-08-23 10:03:10 -04:00
										 |  |  |           :disabled="newRecipeName.trim() === ''" | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |           rounded | 
					
						
							|  |  |  |           block | 
					
						
							|  |  |  |           :loading="loading" | 
					
						
							|  |  |  |           @click="createByName(newRecipeName)" | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </v-card-actions> | 
					
						
							| 
									
										
										
										
											2022-05-25 19:33:58 -08:00
										 |  |  |   </div> | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  | </template> | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | import { defineComponent, reactive, toRefs, ref, useContext, useRouter, computed, useRoute } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  | import { AxiosResponse } from "axios"; | 
					
						
							|  |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							|  |  |  | import { validators } from "~/composables/use-validators"; | 
					
						
							|  |  |  | import { VForm } from "~/types/vuetify"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   setup() { | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       error: false, | 
					
						
							|  |  |  |       loading: false, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |     const { $auth } = useContext(); | 
					
						
							|  |  |  |     const route = useRoute(); | 
					
						
							|  |  |  |     const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || ""); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |     const api = useUserApi(); | 
					
						
							|  |  |  |     const router = useRouter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function handleResponse(response: AxiosResponse<string> | null, edit = false) { | 
					
						
							|  |  |  |       if (response?.status !== 201) { | 
					
						
							|  |  |  |         state.error = true; | 
					
						
							|  |  |  |         state.loading = false; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |       router.push(`/g/${groupSlug.value}/r/${response.data}?edit=${edit.toString()}`); | 
					
						
							| 
									
										
										
										
											2022-05-25 19:08:32 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newRecipeName = ref(""); | 
					
						
							|  |  |  |     const domCreateByName = ref<VForm | null>(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function createByName(name: string) { | 
					
						
							|  |  |  |       if (!domCreateByName.value?.validate() || name === "") { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const { response } = await api.recipes.createOne({ name }); | 
					
						
							|  |  |  |       // TODO createOne claims to return a Recipe, but actually the API only returns a string
 | 
					
						
							|  |  |  |       // @ts-ignore See above
 | 
					
						
							|  |  |  |       handleResponse(response, true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       domCreateByName, | 
					
						
							|  |  |  |       newRecipeName, | 
					
						
							|  |  |  |       createByName, | 
					
						
							|  |  |  |       ...toRefs(state), | 
					
						
							|  |  |  |       validators, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |