| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | import { useAsync, ref, reactive } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | import { useApiSingleton } from "~/composables/use-api"; | 
					
						
							|  |  |  | import { Recipe } from "~/types/api-types/recipe"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useRecipeContext = function () { | 
					
						
							|  |  |  |   const api = useApiSingleton(); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |   const loading = ref(false); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function getBySlug(slug: string) { | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     loading.value = true; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     const recipe = useAsync(async () => { | 
					
						
							|  |  |  |       const { data } = await api.recipes.getOne(slug); | 
					
						
							|  |  |  |       return data; | 
					
						
							|  |  |  |     }, slug); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     loading.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     return recipe; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |   async function deleteRecipe(slug: string) { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     const { data } = await api.recipes.deleteOne(slug); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     loading.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function updateRecipe(slug: string, recipe: Recipe) { | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     loading.value = true; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     const { data } = await api.recipes.updateOne(slug, recipe); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     loading.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |   return { loading, getBySlug, deleteRecipe, updateRecipe }; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | }; |