| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | import { ref, reactive, Ref } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | import { usePublicStoreActions, useStoreActions } from "../partials/use-actions-factory"; | 
					
						
							|  |  |  | import { usePublicExploreApi } from "../api/api-client"; | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { IngredientFood } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  | let foodStore: Ref<IngredientFood[] | null> = ref([]); | 
					
						
							| 
									
										
										
										
											2024-03-12 17:36:30 -05:00
										 |  |  | const publicStoreLoading = ref(false); | 
					
						
							|  |  |  | const storeLoading = ref(false); | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * useFoodData returns a template reactive object | 
					
						
							| 
									
										
										
										
											2024-05-16 17:30:01 -07:00
										 |  |  |  * for managing the creation of foods. It also provides a | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  |  * function to reset the data back to the initial state. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export const useFoodData = function () { | 
					
						
							|  |  |  |   const data: IngredientFood = reactive({ | 
					
						
							|  |  |  |     id: "", | 
					
						
							|  |  |  |     name: "", | 
					
						
							|  |  |  |     description: "", | 
					
						
							|  |  |  |     labelId: undefined, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function reset() { | 
					
						
							|  |  |  |     data.id = ""; | 
					
						
							|  |  |  |     data.name = ""; | 
					
						
							|  |  |  |     data.description = ""; | 
					
						
							|  |  |  |     data.labelId = undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     data, | 
					
						
							|  |  |  |     reset, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export const usePublicFoodStore = function (groupSlug: string) { | 
					
						
							|  |  |  |   const api = usePublicExploreApi(groupSlug).explore; | 
					
						
							| 
									
										
										
										
											2024-03-12 17:36:30 -05:00
										 |  |  |   const loading = publicStoreLoading; | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     ...usePublicStoreActions(api.foods, foodStore, loading), | 
					
						
							|  |  |  |     flushStore() { | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       foodStore = ref([]); | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 17:36:30 -05:00
										 |  |  |   if (!loading.value && (!foodStore.value || foodStore.value.length === 0)) { | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |     foodStore = actions.getAll(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { foods: foodStore, actions }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | export const useFoodStore = function () { | 
					
						
							|  |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2024-03-12 17:36:30 -05:00
										 |  |  |   const loading = storeLoading; | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     ...useStoreActions(api.foods, foodStore, loading), | 
					
						
							|  |  |  |     flushStore() { | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       foodStore.value = []; | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 17:36:30 -05:00
										 |  |  |   if (!loading.value && (!foodStore.value || foodStore.value.length === 0)) { | 
					
						
							| 
									
										
										
										
											2022-05-29 17:29:59 -08:00
										 |  |  |     foodStore = actions.getAll(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { foods: foodStore, actions }; | 
					
						
							|  |  |  | }; |