| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | import { useAsync, useRouter, ref } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useAsyncKey } from "../use-utils"; | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | import { usePublicExploreApi } from "~/composables/api/api-client"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2023-05-30 02:56:20 +02:00
										 |  |  | import { Recipe } from "~/lib/api/types/recipe"; | 
					
						
							|  |  |  | import { RecipeSearchQuery } from "~/lib/api/user/recipes/recipe"; | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  | export const allRecipes = ref<Recipe[]>([]); | 
					
						
							|  |  |  | export const recentRecipes = ref<Recipe[]>([]); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 09:47:54 -05:00
										 |  |  | function getParams( | 
					
						
							|  |  |  |   orderBy: string | null = null, | 
					
						
							|  |  |  |   orderDirection = "desc", | 
					
						
							|  |  |  |   query: RecipeSearchQuery | null = null, | 
					
						
							|  |  |  |   queryFilter: string | null = null | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     orderBy, | 
					
						
							|  |  |  |     orderDirection, | 
					
						
							|  |  |  |     paginationSeed: query?._searchSeed, // propagate searchSeed to stabilize random order pagination
 | 
					
						
							|  |  |  |     searchSeed: query?._searchSeed, // unused, but pass it along for completeness of data
 | 
					
						
							|  |  |  |     search: query?.search, | 
					
						
							|  |  |  |     cookbook: query?.cookbook, | 
					
						
							|  |  |  |     households: query?.households, | 
					
						
							|  |  |  |     categories: query?.categories, | 
					
						
							|  |  |  |     requireAllCategories: query?.requireAllCategories, | 
					
						
							|  |  |  |     tags: query?.tags, | 
					
						
							|  |  |  |     requireAllTags: query?.requireAllTags, | 
					
						
							|  |  |  |     tools: query?.tools, | 
					
						
							|  |  |  |     requireAllTools: query?.requireAllTools, | 
					
						
							|  |  |  |     foods: query?.foods, | 
					
						
							|  |  |  |     requireAllFoods: query?.requireAllFoods, | 
					
						
							|  |  |  |     queryFilter, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export const useLazyRecipes = function (publicGroupSlug: string | null = null) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  |   const router = useRouter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |   // passing the group slug switches to using the public API
 | 
					
						
							|  |  |  |   const api = publicGroupSlug ? usePublicExploreApi(publicGroupSlug).explore : useUserApi(); | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const recipes = ref<Recipe[]>([]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-10 11:59:30 -05:00
										 |  |  |   async function fetchMore( | 
					
						
							|  |  |  |     page: number, | 
					
						
							|  |  |  |     perPage: number, | 
					
						
							|  |  |  |     orderBy: string | null = null, | 
					
						
							|  |  |  |     orderDirection = "desc", | 
					
						
							| 
									
										
										
										
											2023-02-26 20:20:26 +01:00
										 |  |  |     query: RecipeSearchQuery | null = null, | 
					
						
							| 
									
										
										
										
											2022-11-30 23:59:30 -06:00
										 |  |  |     queryFilter: string | null = null, | 
					
						
							| 
									
										
										
										
											2022-09-10 11:59:30 -05:00
										 |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 09:47:54 -05:00
										 |  |  |     const { data, error } = await api.recipes.getAll( | 
					
						
							|  |  |  |       page, | 
					
						
							|  |  |  |       perPage, | 
					
						
							|  |  |  |       getParams(orderBy, orderDirection, query, queryFilter), | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:07:02 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (error?.response?.status === 404) { | 
					
						
							|  |  |  |       router.push("/login"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  |     return data ? data.items : []; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function appendRecipes(val: Array<Recipe>) { | 
					
						
							|  |  |  |     val.forEach((recipe) => { | 
					
						
							|  |  |  |       recipes.value.push(recipe); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  |   function assignSorted(val: Array<Recipe>) { | 
					
						
							|  |  |  |     recipes.value = val; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  |   function removeRecipe(slug: string) { | 
					
						
							|  |  |  |     for (let i = 0; i < recipes?.value?.length; i++) { | 
					
						
							|  |  |  |       if (recipes?.value[i].slug === slug) { | 
					
						
							|  |  |  |         recipes?.value.splice(i, 1); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  |   function replaceRecipes(val: Array<Recipe>) { | 
					
						
							|  |  |  |     recipes.value = val; | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 09:47:54 -05:00
										 |  |  |   async function getRandom(query: RecipeSearchQuery | null = null, queryFilter: string | null = null) { | 
					
						
							|  |  |  |     const { data } = await api.recipes.getAll(1, 1, getParams("random", "desc", query, queryFilter)); | 
					
						
							|  |  |  |     if (data?.items.length) { | 
					
						
							|  |  |  |       return data.items[0]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  |   return { | 
					
						
							|  |  |  |     recipes, | 
					
						
							|  |  |  |     fetchMore, | 
					
						
							| 
									
										
										
										
											2022-08-20 13:59:49 -05:00
										 |  |  |     appendRecipes, | 
					
						
							|  |  |  |     assignSorted, | 
					
						
							|  |  |  |     removeRecipe, | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |     replaceRecipes, | 
					
						
							| 
									
										
										
										
											2024-10-29 09:47:54 -05:00
										 |  |  |     getRandom, | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export const useRecipes = ( | 
					
						
							| 
									
										
										
										
											2024-08-31 21:54:10 -05:00
										 |  |  |   all = false, | 
					
						
							|  |  |  |   fetchRecipes = true, | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |   loadFood = false, | 
					
						
							| 
									
										
										
										
											2024-08-31 21:54:10 -05:00
										 |  |  |   queryFilter: string | null = null, | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |   publicGroupSlug: string | null = null | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  |   const api = publicGroupSlug ? usePublicExploreApi(publicGroupSlug).explore : useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // recipes is non-reactive!!
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |   const { recipes, page, perPage } = (() => { | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     if (all) { | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         recipes: allRecipes, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |         page: 1, | 
					
						
							|  |  |  |         perPage: -1, | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |       }; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         recipes: recentRecipes, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |         page: 1, | 
					
						
							|  |  |  |         perPage: 30, | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |       }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   })(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function refreshRecipes() { | 
					
						
							| 
									
										
										
										
											2024-08-31 21:54:10 -05:00
										 |  |  |     const { data } = await api.recipes.getAll(page, perPage, { loadFood, orderBy: "created_at", queryFilter }); | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     if (data) { | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |       recipes.value = data.items; | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function getAllRecipes() { | 
					
						
							|  |  |  |     useAsync(async () => { | 
					
						
							|  |  |  |       await refreshRecipes(); | 
					
						
							|  |  |  |     }, useAsyncKey()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function assignSorted(val: Array<Recipe>) { | 
					
						
							|  |  |  |     recipes.value = val; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (fetchRecipes) { | 
					
						
							|  |  |  |     getAllRecipes(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 19:41:41 -08:00
										 |  |  |   return { getAllRecipes, assignSorted, refreshRecipes }; | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | }; |