| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | import { useAsync, ref } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useAsyncKey } from "../use-utils"; | 
					
						
							|  |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | import { Recipe } from "~/types/api-types/recipe"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | const rand = (n: number) => Math.floor(Math.random() * n); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | function swap(t: Array<unknown>, i: number, j: number) { | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   const q = t[i]; | 
					
						
							|  |  |  |   t[i] = t[j]; | 
					
						
							|  |  |  |   t[j] = q; | 
					
						
							|  |  |  |   return t; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useSorter = () => { | 
					
						
							|  |  |  |   function sortAToZ(list: Array<Recipe>) { | 
					
						
							|  |  |  |     list.sort((a, b) => { | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |       const textA: string = a.name?.toUpperCase() ?? ""; | 
					
						
							|  |  |  |       const textB: string = b.name?.toUpperCase() ?? ""; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       return textA < textB ? -1 : textA > textB ? 1 : 0; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   function sortByCreated(list: Array<Recipe>) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     list.sort((a, b) => ((a.dateAdded ?? "") > (b.dateAdded ?? "") ? -1 : 1)); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							|  |  |  |   function sortByUpdated(list: Array<Recipe>) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     list.sort((a, b) => ((a.dateUpdated ?? "") > (b.dateUpdated ?? "") ? -1 : 1)); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							|  |  |  |   function sortByRating(list: Array<Recipe>) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     list.sort((a, b) => ((a.rating ?? 0) > (b.rating ?? 0) ? -1 : 1)); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function randomRecipe(list: Array<Recipe>): Recipe { | 
					
						
							|  |  |  |     return list[Math.floor(Math.random() * list.length)]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function shuffle(list: Array<Recipe>) { | 
					
						
							|  |  |  |     let last = list.length; | 
					
						
							|  |  |  |     let n; | 
					
						
							|  |  |  |     while (last > 0) { | 
					
						
							|  |  |  |       n = rand(last); | 
					
						
							|  |  |  |       swap(list, n, --last); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     sortAToZ, | 
					
						
							|  |  |  |     sortByCreated, | 
					
						
							|  |  |  |     sortByUpdated, | 
					
						
							|  |  |  |     sortByRating, | 
					
						
							|  |  |  |     randomRecipe, | 
					
						
							|  |  |  |     shuffle, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  | export const useLazyRecipes = function () { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |   const recipes = ref<Recipe[]>([]); | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |   async function fetchMore(page: number, perPage: number, orderBy: string | null = null, orderDirection = "desc") { | 
					
						
							|  |  |  |     const { data } = await api.recipes.getAll(page, perPage, { orderBy, orderDirection }); | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  |     if (data) { | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |       data.items.forEach((recipe) => { | 
					
						
							| 
									
										
										
										
											2021-10-03 14:07:18 -08:00
										 |  |  |         recipes.value?.push(recipe); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     recipes, | 
					
						
							|  |  |  |     fetchMore, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  | export const useRecipes = (all = false, fetchRecipes = true) => { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = 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() { | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |     const { data } = await api.recipes.getAll(page, perPage, { loadFood: true, orderBy: "created_at" }); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }; |