mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 02:03:35 -04:00 
			
		
		
		
	fix: Only call store APIs once (#3306)
* move loading value to inside async function * share loading state and use it for throttling
This commit is contained in:
		| @@ -37,6 +37,7 @@ export function usePublicStoreActions<T extends BoundT>( | |||||||
|     loading.value = true; |     loading.value = true; | ||||||
|     const allItems = useAsync(async () => { |     const allItems = useAsync(async () => { | ||||||
|       const { data } = await api.getAll(page, perPage, params); |       const { data } = await api.getAll(page, perPage, params); | ||||||
|  |       loading.value = false; | ||||||
|  |  | ||||||
|       if (data && allRef) { |       if (data && allRef) { | ||||||
|         allRef.value = data.items; |         allRef.value = data.items; | ||||||
| @@ -49,7 +50,6 @@ export function usePublicStoreActions<T extends BoundT>( | |||||||
|       } |       } | ||||||
|     }, useAsyncKey()); |     }, useAsyncKey()); | ||||||
|  |  | ||||||
|     loading.value = false; |  | ||||||
|     return allItems; |     return allItems; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -88,6 +88,7 @@ export function useStoreActions<T extends BoundT>( | |||||||
|     loading.value = true; |     loading.value = true; | ||||||
|     const allItems = useAsync(async () => { |     const allItems = useAsync(async () => { | ||||||
|       const { data } = await api.getAll(page, perPage, params); |       const { data } = await api.getAll(page, perPage, params); | ||||||
|  |       loading.value = false; | ||||||
|  |  | ||||||
|       if (data && allRef) { |       if (data && allRef) { | ||||||
|         allRef.value = data.items; |         allRef.value = data.items; | ||||||
| @@ -100,7 +101,6 @@ export function useStoreActions<T extends BoundT>( | |||||||
|       } |       } | ||||||
|     }, useAsyncKey()); |     }, useAsyncKey()); | ||||||
|  |  | ||||||
|     loading.value = false; |  | ||||||
|     return allItems; |     return allItems; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import { useUserApi } from "~/composables/api"; | |||||||
| import { RecipeCategory } from "~/lib/api/types/admin"; | import { RecipeCategory } from "~/lib/api/types/admin"; | ||||||
|  |  | ||||||
| const categoryStore: Ref<RecipeCategory[]> = ref([]); | const categoryStore: Ref<RecipeCategory[]> = ref([]); | ||||||
|  | const publicStoreLoading = ref(false); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| export function useCategoryData() { | export function useCategoryData() { | ||||||
|   const data = reactive({ |   const data = reactive({ | ||||||
| @@ -27,7 +29,7 @@ export function useCategoryData() { | |||||||
|  |  | ||||||
| export function usePublicCategoryStore(groupSlug: string) { | export function usePublicCategoryStore(groupSlug: string) { | ||||||
|   const api = usePublicExploreApi(groupSlug).explore; |   const api = usePublicExploreApi(groupSlug).explore; | ||||||
|   const loading = ref(false); |   const loading = publicStoreLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...usePublicStoreActions<RecipeCategory>(api.categories, categoryStore, loading), |     ...usePublicStoreActions<RecipeCategory>(api.categories, categoryStore, loading), | ||||||
| @@ -36,7 +38,7 @@ export function usePublicCategoryStore(groupSlug: string) { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!categoryStore.value || categoryStore.value?.length === 0) { |   if (!loading.value && (!categoryStore.value || categoryStore.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -50,7 +52,7 @@ export function usePublicCategoryStore(groupSlug: string) { | |||||||
| export function useCategoryStore() { | export function useCategoryStore() { | ||||||
|   // passing the group slug switches to using the public API |   // passing the group slug switches to using the public API | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions<RecipeCategory>(api.categories, categoryStore, loading), |     ...useStoreActions<RecipeCategory>(api.categories, categoryStore, loading), | ||||||
| @@ -59,7 +61,7 @@ export function useCategoryStore() { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!categoryStore.value || categoryStore.value?.length === 0) { |   if (!loading.value && (!categoryStore.value || categoryStore.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import { useUserApi } from "~/composables/api"; | |||||||
| import { IngredientFood } from "~/lib/api/types/recipe"; | import { IngredientFood } from "~/lib/api/types/recipe"; | ||||||
|  |  | ||||||
| let foodStore: Ref<IngredientFood[] | null> = ref([]); | let foodStore: Ref<IngredientFood[] | null> = ref([]); | ||||||
|  | const publicStoreLoading = ref(false); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * useFoodData returns a template reactive object |  * useFoodData returns a template reactive object | ||||||
| @@ -34,7 +36,7 @@ export const useFoodData = function () { | |||||||
|  |  | ||||||
| export const usePublicFoodStore = function (groupSlug: string) { | export const usePublicFoodStore = function (groupSlug: string) { | ||||||
|   const api = usePublicExploreApi(groupSlug).explore; |   const api = usePublicExploreApi(groupSlug).explore; | ||||||
|   const loading = ref(false); |   const loading = publicStoreLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...usePublicStoreActions(api.foods, foodStore, loading), |     ...usePublicStoreActions(api.foods, foodStore, loading), | ||||||
| @@ -43,7 +45,7 @@ export const usePublicFoodStore = function (groupSlug: string) { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!foodStore.value || foodStore.value.length === 0) { |   if (!loading.value && (!foodStore.value || foodStore.value.length === 0)) { | ||||||
|     foodStore = actions.getAll(); |     foodStore = actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -52,7 +54,7 @@ export const usePublicFoodStore = function (groupSlug: string) { | |||||||
|  |  | ||||||
| export const useFoodStore = function () { | export const useFoodStore = function () { | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions(api.foods, foodStore, loading), |     ...useStoreActions(api.foods, foodStore, loading), | ||||||
| @@ -61,7 +63,7 @@ export const useFoodStore = function () { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!foodStore.value || foodStore.value.length === 0) { |   if (!loading.value && (!foodStore.value || foodStore.value.length === 0)) { | ||||||
|     foodStore = actions.getAll(); |     foodStore = actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import { MultiPurposeLabelOut } from "~/lib/api/types/labels"; | |||||||
| import { useUserApi } from "~/composables/api"; | import { useUserApi } from "~/composables/api"; | ||||||
|  |  | ||||||
| let labelStore: Ref<MultiPurposeLabelOut[] | null> = ref([]); | let labelStore: Ref<MultiPurposeLabelOut[] | null> = ref([]); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| export function useLabelData() { | export function useLabelData() { | ||||||
|   const data = reactive({ |   const data = reactive({ | ||||||
| @@ -28,7 +29,7 @@ export function useLabelData() { | |||||||
|  |  | ||||||
| export function useLabelStore() { | export function useLabelStore() { | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions<MultiPurposeLabelOut>(api.multiPurposeLabels, labelStore, loading), |     ...useStoreActions<MultiPurposeLabelOut>(api.multiPurposeLabels, labelStore, loading), | ||||||
| @@ -37,7 +38,7 @@ export function useLabelStore() { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!labelStore.value || labelStore.value?.length === 0) { |   if (!loading.value && (!labelStore.value || labelStore.value?.length === 0)) { | ||||||
|     labelStore = actions.getAll(); |     labelStore = actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import { useUserApi } from "~/composables/api"; | |||||||
| import { RecipeTag } from "~/lib/api/types/admin"; | import { RecipeTag } from "~/lib/api/types/admin"; | ||||||
|  |  | ||||||
| const items: Ref<RecipeTag[]> = ref([]); | const items: Ref<RecipeTag[]> = ref([]); | ||||||
|  | const publicStoreLoading = ref(false); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| export function useTagData() { | export function useTagData() { | ||||||
|   const data = reactive({ |   const data = reactive({ | ||||||
| @@ -27,7 +29,7 @@ export function useTagData() { | |||||||
|  |  | ||||||
| export function usePublicTagStore(groupSlug: string) { | export function usePublicTagStore(groupSlug: string) { | ||||||
|   const api = usePublicExploreApi(groupSlug).explore; |   const api = usePublicExploreApi(groupSlug).explore; | ||||||
|   const loading = ref(false); |   const loading = publicStoreLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...usePublicStoreActions<RecipeTag>(api.tags, items, loading), |     ...usePublicStoreActions<RecipeTag>(api.tags, items, loading), | ||||||
| @@ -36,7 +38,7 @@ export function usePublicTagStore(groupSlug: string) { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!items.value || items.value?.length === 0) { |   if (!loading.value && (!items.value || items.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -49,7 +51,7 @@ export function usePublicTagStore(groupSlug: string) { | |||||||
|  |  | ||||||
| export function useTagStore() { | export function useTagStore() { | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions<RecipeTag>(api.tags, items, loading), |     ...useStoreActions<RecipeTag>(api.tags, items, loading), | ||||||
| @@ -58,7 +60,7 @@ export function useTagStore() { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!items.value || items.value?.length === 0) { |   if (!loading.value && (!items.value || items.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import { useUserApi } from "~/composables/api"; | |||||||
| import { RecipeTool } from "~/lib/api/types/recipe"; | import { RecipeTool } from "~/lib/api/types/recipe"; | ||||||
|  |  | ||||||
| const toolStore: Ref<RecipeTool[]> = ref([]); | const toolStore: Ref<RecipeTool[]> = ref([]); | ||||||
|  | const publicStoreLoading = ref(false); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| export function useToolData() { | export function useToolData() { | ||||||
|   const data = reactive({ |   const data = reactive({ | ||||||
| @@ -29,7 +31,7 @@ export function useToolData() { | |||||||
|  |  | ||||||
| export function usePublicToolStore(groupSlug: string) { | export function usePublicToolStore(groupSlug: string) { | ||||||
|   const api = usePublicExploreApi(groupSlug).explore; |   const api = usePublicExploreApi(groupSlug).explore; | ||||||
|   const loading = ref(false); |   const loading = publicStoreLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...usePublicStoreActions<RecipeTool>(api.tools, toolStore, loading), |     ...usePublicStoreActions<RecipeTool>(api.tools, toolStore, loading), | ||||||
| @@ -38,7 +40,7 @@ export function usePublicToolStore(groupSlug: string) { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!toolStore.value || toolStore.value?.length === 0) { |   if (!loading.value && (!toolStore.value || toolStore.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -51,7 +53,7 @@ export function usePublicToolStore(groupSlug: string) { | |||||||
|  |  | ||||||
| export function useToolStore() { | export function useToolStore() { | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions<RecipeTool>(api.tools, toolStore, loading), |     ...useStoreActions<RecipeTool>(api.tools, toolStore, loading), | ||||||
| @@ -60,7 +62,7 @@ export function useToolStore() { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!toolStore.value || toolStore.value?.length === 0) { |   if (!loading.value && (!toolStore.value || toolStore.value?.length === 0)) { | ||||||
|     actions.getAll(); |     actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import { useUserApi } from "~/composables/api"; | |||||||
| import { IngredientUnit } from "~/lib/api/types/recipe"; | import { IngredientUnit } from "~/lib/api/types/recipe"; | ||||||
|  |  | ||||||
| let unitStore: Ref<IngredientUnit[] | null> = ref([]); | let unitStore: Ref<IngredientUnit[] | null> = ref([]); | ||||||
|  | const storeLoading = ref(false); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * useUnitData returns a template reactive object |  * useUnitData returns a template reactive object | ||||||
| @@ -35,7 +36,7 @@ export const useUnitData = function () { | |||||||
|  |  | ||||||
| export const useUnitStore = function () { | export const useUnitStore = function () { | ||||||
|   const api = useUserApi(); |   const api = useUserApi(); | ||||||
|   const loading = ref(false); |   const loading = storeLoading; | ||||||
|  |  | ||||||
|   const actions = { |   const actions = { | ||||||
|     ...useStoreActions<IngredientUnit>(api.units, unitStore, loading), |     ...useStoreActions<IngredientUnit>(api.units, unitStore, loading), | ||||||
| @@ -44,7 +45,7 @@ export const useUnitStore = function () { | |||||||
|     }, |     }, | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   if (!unitStore.value || unitStore.value.length === 0) { |   if (!loading.value && (!unitStore.value || unitStore.value.length === 0)) { | ||||||
|     unitStore = actions.getAll(); |     unitStore = actions.getAll(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user