| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  | import { useAsync, ref, Ref, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | import { useAsyncKey } from "./use-utils"; | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | import { usePublicExploreApi } from "./api/api-client"; | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  | import { useHouseholdSelf } from "./use-households"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { ReadCookBook, UpdateCookBook } from "~/lib/api/types/cookbook"; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  | let cookbookStore: Ref<ReadCookBook[] | null> | null = null; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export const useCookbook = function (publicGroupSlug: string | null = null) { | 
					
						
							| 
									
										
										
										
											2021-08-31 18:51:34 -08:00
										 |  |  |   function getOne(id: string | number) { | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2021-08-31 18:51:34 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const units = useAsync(async () => { | 
					
						
							|  |  |  |       const { data } = await api.cookbooks.getOne(id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return data; | 
					
						
							|  |  |  |     }, useAsyncKey()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return units; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { getOne }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export const usePublicCookbooks = function (groupSlug: string) { | 
					
						
							|  |  |  |   const api = usePublicExploreApi(groupSlug).explore; | 
					
						
							|  |  |  |   const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     getAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const units = useAsync(async () => { | 
					
						
							|  |  |  |         const { data } = await api.cookbooks.getAll(1, -1, { orderBy: "position", orderDirection: "asc" }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (data) { | 
					
						
							|  |  |  |           return data.items; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, useAsyncKey()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |       return units; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async refreshAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.cookbooks.getAll(1, -1, { orderBy: "position", orderDirection: "asc" }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data && data.items && cookbookStore) { | 
					
						
							|  |  |  |         cookbookStore.value = data.items; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     flushStore() { | 
					
						
							|  |  |  |       cookbookStore = null; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!cookbookStore) { | 
					
						
							|  |  |  |     cookbookStore = actions.getAll(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { cookbooks: cookbookStore, actions }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | export const useCookbooks = function () { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |   const { household } = useHouseholdSelf(); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |   const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-29 02:39:51 +01:00
										 |  |  |   const { i18n } = useContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |   const actions = { | 
					
						
							|  |  |  |     getAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const units = useAsync(async () => { | 
					
						
							| 
									
										
										
										
											2023-02-26 21:48:32 +01:00
										 |  |  |         const { data } = await api.cookbooks.getAll(1, -1, { orderBy: "position", orderDirection: "asc" }); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |         if (data) { | 
					
						
							|  |  |  |           return data.items; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       }, useAsyncKey()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |       return units; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async refreshAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							| 
									
										
										
										
											2023-02-26 21:48:32 +01:00
										 |  |  |       const { data } = await api.cookbooks.getAll(1, -1, { orderBy: "position", orderDirection: "asc" }); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  |       if (data && data.items && cookbookStore) { | 
					
						
							|  |  |  |         cookbookStore.value = data.items; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-11-05 13:57:30 -06:00
										 |  |  |     async createOne(name: string | null = null) { | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.cookbooks.createOne({ | 
					
						
							| 
									
										
										
										
											2024-11-05 13:57:30 -06:00
										 |  |  |         name: name || i18n.t("cookbook.household-cookbook-name", [household.value?.name || "", String((cookbookStore?.value?.length ?? 0) + 1)]) as string, | 
					
						
							| 
									
										
										
										
											2024-10-17 10:35:39 -05:00
										 |  |  |         position: (cookbookStore?.value?.length ?? 0) + 1, | 
					
						
							|  |  |  |         queryFilterString: "", | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       if (data && cookbookStore?.value) { | 
					
						
							| 
									
										
										
										
											2021-08-31 18:51:34 -08:00
										 |  |  |         cookbookStore.value.push(data); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							| 
									
										
										
										
											2024-02-04 17:15:25 +01:00
										 |  |  |       return data; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |     async updateOne(updateData: UpdateCookBook) { | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       if (!updateData.id) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |       const { data } = await api.cookbooks.updateOne(updateData.id, updateData); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |       if (data && cookbookStore?.value) { | 
					
						
							|  |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							| 
									
										
										
										
											2024-03-26 14:02:20 +01:00
										 |  |  |       return data; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 13:57:30 -06:00
										 |  |  |     async updateOrder(cookbooks: ReadCookBook[]) { | 
					
						
							|  |  |  |       if (!cookbooks?.length) { | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 13:57:30 -06:00
										 |  |  |       cookbooks.forEach((element, index) => { | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |         element.position = index + 1; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 13:57:30 -06:00
										 |  |  |       const { data } = await api.cookbooks.updateAll(cookbooks); | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data && cookbookStore?.value) { | 
					
						
							|  |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async deleteOne(id: string | number) { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.cookbooks.deleteOne(id); | 
					
						
							|  |  |  |       if (data && cookbookStore?.value) { | 
					
						
							|  |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     flushStore() { | 
					
						
							|  |  |  |       cookbookStore = null; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!cookbookStore) { | 
					
						
							|  |  |  |     cookbookStore = actions.getAll(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |   return { cookbooks: cookbookStore, actions }; | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | }; |