Skip to content

Commit 824603a

Browse files
fix: Stores Not Populating Sometimes (#6266)
1 parent e3f120c commit 824603a

13 files changed

+31
-27
lines changed

frontend/composables/partials/use-actions-factory.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useAsyncKey } from "../use-utils";
1+
import type { AsyncData, NuxtError } from "#app";
22
import type { BoundT } from "./types";
33
import type { BaseCRUDAPI, BaseCRUDAPIReadOnly } from "~/lib/api/base/base-clients";
44
import type { QueryValue } from "~/lib/api/base/route";
55

66
interface ReadOnlyStoreActions<T extends BoundT> {
7-
getAll(page?: number, perPage?: number, params?: any): Ref<T[] | null>;
7+
getAll(page?: number, perPage?: number, params?: any): AsyncData<T[] | null, NuxtError<unknown> | null>;
88
refresh(page?: number, perPage?: number, params?: any): Promise<void>;
99
}
1010

@@ -21,6 +21,7 @@ interface StoreActions<T extends BoundT> extends ReadOnlyStoreActions<T> {
2121
* a lot of refreshing hooks to be called on operations
2222
*/
2323
export function useReadOnlyActions<T extends BoundT>(
24+
storeKey: string,
2425
api: BaseCRUDAPIReadOnly<T>,
2526
allRef: Ref<T[] | null> | null,
2627
loading: Ref<boolean>,
@@ -29,7 +30,7 @@ export function useReadOnlyActions<T extends BoundT>(
2930
params.orderBy ??= "name";
3031
params.orderDirection ??= "asc";
3132

32-
const allItems = useAsyncData(useAsyncKey(), async () => {
33+
const allItems = useAsyncData(storeKey, async () => {
3334
loading.value = true;
3435
try {
3536
const { data } = await api.getAll(page, perPage, params);
@@ -80,6 +81,7 @@ export function useReadOnlyActions<T extends BoundT>(
8081
* a lot of refreshing hooks to be called on operations
8182
*/
8283
export function useStoreActions<T extends BoundT>(
84+
storeKey: string,
8385
api: BaseCRUDAPI<unknown, T, unknown>,
8486
allRef: Ref<T[] | null> | null,
8587
loading: Ref<boolean>,
@@ -88,7 +90,7 @@ export function useStoreActions<T extends BoundT>(
8890
params.orderBy ??= "name";
8991
params.orderDirection ??= "asc";
9092

91-
const allItems = useAsyncData(useAsyncKey(), async () => {
93+
const allItems = useAsyncData(storeKey, async () => {
9294
loading.value = true;
9395
try {
9496
const { data } = await api.getAll(page, perPage, params);

frontend/composables/partials/use-store-factory.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ export const useData = function <T extends BoundT>(defaultObject: T) {
1313
};
1414

1515
export const useReadOnlyStore = function <T extends BoundT>(
16+
storeKey: string,
1617
store: Ref<T[]>,
1718
loading: Ref<boolean>,
1819
api: BaseCRUDAPIReadOnly<T>,
1920
params = {} as Record<string, QueryValue>,
2021
) {
21-
const storeActions = useReadOnlyActions(api, store, loading);
22+
const storeActions = useReadOnlyActions(`${storeKey}-store-readonly`, api, store, loading);
2223
const actions = {
2324
...storeActions,
2425
async refresh() {
@@ -29,21 +30,22 @@ export const useReadOnlyStore = function <T extends BoundT>(
2930
},
3031
};
3132

33+
// initial hydration
3234
if (!loading.value && !store.value.length) {
33-
const result = actions.getAll(1, -1, params);
34-
store.value = result.value || [];
35+
actions.refresh();
3536
}
3637

3738
return { store, actions };
3839
};
3940

4041
export const useStore = function <T extends BoundT>(
42+
storeKey: string,
4143
store: Ref<T[]>,
4244
loading: Ref<boolean>,
4345
api: BaseCRUDAPI<unknown, T, unknown>,
4446
params = {} as Record<string, QueryValue>,
4547
) {
46-
const storeActions = useStoreActions(api, store, loading);
48+
const storeActions = useStoreActions(`${storeKey}-store`, api, store, loading);
4749
const actions = {
4850
...storeActions,
4951
async refresh() {
@@ -54,9 +56,9 @@ export const useStore = function <T extends BoundT>(
5456
},
5557
};
5658

59+
// initial hydration
5760
if (!loading.value && !store.value.length) {
58-
const result = actions.getAll(1, -1, params);
59-
store.value = result.value || [];
61+
actions.refresh();
6062
}
6163

6264
return { store, actions };

frontend/composables/store/use-category-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export const useCategoryData = function () {
1717

1818
export const useCategoryStore = function (i18n?: Composer) {
1919
const api = useUserApi(i18n);
20-
return useStore<RecipeCategory>(store, loading, api.categories);
20+
return useStore<RecipeCategory>("category", store, loading, api.categories);
2121
};
2222

2323
export const usePublicCategoryStore = function (groupSlug: string, i18n?: Composer) {
2424
const api = usePublicExploreApi(groupSlug, i18n).explore;
25-
return useReadOnlyStore<RecipeCategory>(store, publicLoading, api.categories);
25+
return useReadOnlyStore<RecipeCategory>("category", store, publicLoading, api.categories);
2626
};

frontend/composables/store/use-cookbook-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const publicLoading = ref(false);
99

1010
export const useCookbookStore = function (i18n?: Composer) {
1111
const api = useUserApi(i18n);
12-
const store = useStore<ReadCookBook>(cookbooks, loading, api.cookbooks);
12+
const store = useStore<ReadCookBook>("cookbook", cookbooks, loading, api.cookbooks);
1313

1414
const updateAll = async function (updateData: UpdateCookBook[]) {
1515
loading.value = true;
@@ -25,5 +25,5 @@ export const useCookbookStore = function (i18n?: Composer) {
2525

2626
export const usePublicCookbookStore = function (groupSlug: string, i18n?: Composer) {
2727
const api = usePublicExploreApi(groupSlug, i18n).explore;
28-
return useReadOnlyStore<ReadCookBook>(cookbooks, publicLoading, api.cookbooks);
28+
return useReadOnlyStore<ReadCookBook>("cookbook", cookbooks, publicLoading, api.cookbooks);
2929
};

frontend/composables/store/use-food-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export const useFoodData = function () {
1818

1919
export const useFoodStore = function (i18n?: Composer) {
2020
const api = useUserApi(i18n);
21-
return useStore<IngredientFood>(store, loading, api.foods);
21+
return useStore<IngredientFood>("food", store, loading, api.foods);
2222
};
2323

2424
export const usePublicFoodStore = function (groupSlug: string, i18n?: Composer) {
2525
const api = usePublicExploreApi(groupSlug, i18n).explore;
26-
return useReadOnlyStore<IngredientFood>(store, publicLoading, api.foods);
26+
return useReadOnlyStore<IngredientFood>("food", store, publicLoading, api.foods);
2727
};

frontend/composables/store/use-household-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const publicLoading = ref(false);
99

1010
export const useHouseholdStore = function (i18n?: Composer) {
1111
const api = useUserApi(i18n);
12-
return useReadOnlyStore<HouseholdSummary>(store, loading, api.households);
12+
return useReadOnlyStore<HouseholdSummary>("household", store, loading, api.households);
1313
};
1414

1515
export const usePublicHouseholdStore = function (groupSlug: string, i18n?: Composer) {
1616
const api = usePublicExploreApi(groupSlug, i18n).explore;
17-
return useReadOnlyStore<HouseholdSummary>(store, publicLoading, api.households);
17+
return useReadOnlyStore<HouseholdSummary>("household-public", store, publicLoading, api.households);
1818
};

frontend/composables/store/use-label-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export const useLabelData = function () {
1717

1818
export const useLabelStore = function (i18n?: Composer) {
1919
const api = useUserApi(i18n);
20-
return useStore<MultiPurposeLabelOut>(store, loading, api.multiPurposeLabels);
20+
return useStore<MultiPurposeLabelOut>("label", store, loading, api.multiPurposeLabels);
2121
};

frontend/composables/store/use-tag-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export const useTagData = function () {
1717

1818
export const useTagStore = function (i18n?: Composer) {
1919
const api = useUserApi(i18n);
20-
return useStore<RecipeTag>(store, loading, api.tags);
20+
return useStore<RecipeTag>("tag", store, loading, api.tags);
2121
};
2222

2323
export const usePublicTagStore = function (groupSlug: string, i18n?: Composer) {
2424
const api = usePublicExploreApi(groupSlug, i18n).explore;
25-
return useReadOnlyStore<RecipeTag>(store, publicLoading, api.tags);
25+
return useReadOnlyStore<RecipeTag>("tag", store, publicLoading, api.tags);
2626
};

frontend/composables/store/use-tool-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export const useToolData = function () {
2323

2424
export const useToolStore = function (i18n?: Composer) {
2525
const api = useUserApi(i18n);
26-
return useStore<RecipeTool>(store, loading, api.tools);
26+
return useStore<RecipeTool>("tool", store, loading, api.tools);
2727
};
2828

2929
export const usePublicToolStore = function (groupSlug: string, i18n?: Composer) {
3030
const api = usePublicExploreApi(groupSlug, i18n).explore;
31-
return useReadOnlyStore<RecipeTool>(store, publicLoading, api.tools);
31+
return useReadOnlyStore<RecipeTool>("tool", store, publicLoading, api.tools);
3232
};

frontend/composables/store/use-unit-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export const useUnitData = function () {
1818

1919
export const useUnitStore = function (i18n?: Composer) {
2020
const api = useUserApi(i18n);
21-
return useStore<IngredientUnit>(store, loading, api.units);
21+
return useStore<IngredientUnit>("unit", store, loading, api.units);
2222
};

0 commit comments

Comments
 (0)