| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { ref, onMounted } from "@nuxtjs/composition-api"; | 
					
						
							|  |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { Recipe } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | export const useRecipe = function (slug: string, eager = true) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							|  |  |  |   const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const recipe = ref<Recipe | null>(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function fetchRecipe() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.recipes.getOne(slug); | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |     if (data) { | 
					
						
							|  |  |  |       recipe.value = data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function deleteRecipe() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.recipes.deleteOne(slug); | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function updateRecipe(recipe: Recipe) { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.recipes.updateOne(slug, recipe); | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onMounted(() => { | 
					
						
							|  |  |  |     if (eager) { | 
					
						
							|  |  |  |       fetchRecipe(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     recipe, | 
					
						
							|  |  |  |     loading, | 
					
						
							|  |  |  |     fetchRecipe, | 
					
						
							|  |  |  |     deleteRecipe, | 
					
						
							|  |  |  |     updateRecipe, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |