| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  | import { useStoreActions } from "./partials/use-actions-factory"; | 
					
						
							|  |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { GroupRecipeActionOut, GroupRecipeActionType } from "~/lib/api/types/household"; | 
					
						
							|  |  |  | import type { RequestResponse } from "~/lib/api/types/non-generated"; | 
					
						
							|  |  |  | import type { Recipe } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | const groupRecipeActions = ref<GroupRecipeActionOut[] | null>(null); | 
					
						
							|  |  |  | const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function useGroupRecipeActionData() { | 
					
						
							|  |  |  |   const data = reactive({ | 
					
						
							|  |  |  |     id: "", | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     actionType: "link" as GroupRecipeActionType, | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  |     title: "", | 
					
						
							|  |  |  |     url: "", | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function reset() { | 
					
						
							|  |  |  |     data.id = ""; | 
					
						
							|  |  |  |     data.actionType = "link"; | 
					
						
							|  |  |  |     data.title = ""; | 
					
						
							|  |  |  |     data.url = ""; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     data, | 
					
						
							|  |  |  |     reset, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useGroupRecipeActions = function ( | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | 	orderBy: string | null = "title", | 
					
						
							|  |  |  | 	orderDirection: string | null = "asc", | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  | ) { | 
					
						
							|  |  |  |   const api = useUserApi(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function refreshGroupRecipeActions() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.groupRecipeActions.getAll(1, -1, { orderBy, orderDirection }); | 
					
						
							|  |  |  |     groupRecipeActions.value = data?.items || null; | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const recipeActions = computed<GroupRecipeActionOut[] | null>(() => { | 
					
						
							|  |  |  |     return groupRecipeActions.value; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-27 01:37:09 -06:00
										 |  |  |   function parseRecipeActionUrl(url: string, recipe: Recipe, recipeScale: number): string { | 
					
						
							|  |  |  |     const recipeServings = (recipe.recipeServings || 1) * recipeScale; | 
					
						
							|  |  |  |     const recipeYieldQuantity = (recipe.recipeYieldQuantity || 1) * recipeScale; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  |     return url | 
					
						
							|  |  |  |       .replace("${url}", window.location.href) | 
					
						
							|  |  |  |       .replace("${id}", recipe.id || "") | 
					
						
							|  |  |  |       .replace("${slug}", recipe.slug || "") | 
					
						
							| 
									
										
										
										
											2025-01-27 01:37:09 -06:00
										 |  |  |       .replace("${servings}", recipeServings.toString()) | 
					
						
							|  |  |  |       .replace("${yieldQuantity}", recipeYieldQuantity.toString()) | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       .replace("${yieldText}", recipe.recipeYield || ""); | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |   // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
 | 
					
						
							| 
									
										
										
										
											2025-01-27 01:37:09 -06:00
										 |  |  |   async function execute(action: GroupRecipeActionOut, recipe: Recipe, recipeScale: number): Promise<void | RequestResponse<unknown>> { | 
					
						
							|  |  |  |     const url = parseRecipeActionUrl(action.url, recipe, recipeScale); | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (action.actionType) { | 
					
						
							|  |  |  |       case "link": | 
					
						
							|  |  |  |         window.open(url, "_blank")?.focus(); | 
					
						
							| 
									
										
										
										
											2024-09-14 09:59:36 -05:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  |       case "post": | 
					
						
							| 
									
										
										
										
											2025-07-25 13:42:17 +02:00
										 |  |  |         return await api.groupRecipeActions.triggerAction(action.id, recipe.slug || "", recipeScale); | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  |       default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!groupRecipeActions.value && !loading.value) { | 
					
						
							|  |  |  |     refreshGroupRecipeActions(); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     ...useStoreActions<GroupRecipeActionOut>(api.groupRecipeActions, groupRecipeActions, loading), | 
					
						
							|  |  |  |     flushStore() { | 
					
						
							|  |  |  |       groupRecipeActions.value = []; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2024-05-01 02:20:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     actions, | 
					
						
							|  |  |  |     execute, | 
					
						
							|  |  |  |     recipeActions, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |