mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 02:03:35 -04:00 
			
		
		
		
	refactor(frontend): ♻️ rewrite search componenets to typescript
This commit is contained in:
		
							
								
								
									
										51
									
								
								frontend/composables/use-recipes.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								frontend/composables/use-recipes.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| import { useAsync, ref } from "@nuxtjs/composition-api"; | ||||
| import { useAsyncKey } from "./use-utils"; | ||||
| import { useApiSingleton } from "~/composables/use-api"; | ||||
| import { Recipe } from "~/types/api-types/recipe"; | ||||
|  | ||||
| export const allRecipes = ref<Recipe[] | null>([]); | ||||
| export const recentRecipes = ref<Recipe[] | null>([]); | ||||
|  | ||||
| export const useRecipes = (all = false, fetchRecipes = true) => { | ||||
|   const api = useApiSingleton(); | ||||
|  | ||||
|   // recipes is non-reactive!! | ||||
|   const { recipes, start, end } = (() => { | ||||
|     if (all) { | ||||
|       return { | ||||
|         recipes: allRecipes, | ||||
|         start: 0, | ||||
|         end: 9999, | ||||
|       }; | ||||
|     } else { | ||||
|       return { | ||||
|         recipes: recentRecipes, | ||||
|         start: 0, | ||||
|         end: 30, | ||||
|       }; | ||||
|     } | ||||
|   })(); | ||||
|  | ||||
|   async function refreshRecipes() { | ||||
|     const { data } = await api.recipes.getAll(start, end); | ||||
|     if (data) { | ||||
|       recipes.value = data; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   function getAllRecipes() { | ||||
|     useAsync(async () => { | ||||
|       await refreshRecipes(); | ||||
|     }, useAsyncKey()); | ||||
|   } | ||||
|  | ||||
|   function assignSorted(val: Array<Recipe>) { | ||||
|     recipes.value = val; | ||||
|   } | ||||
|  | ||||
|   if (fetchRecipes) { | ||||
|     getAllRecipes(); | ||||
|   } | ||||
|  | ||||
|   return { getAllRecipes, assignSorted }; | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user