mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-10 04:15:38 -05:00
fix: Stores Not Populating Sometimes (#6266)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { useAsyncKey } from "../use-utils";
|
||||
import type { AsyncData, NuxtError } from "#app";
|
||||
import type { BoundT } from "./types";
|
||||
import type { BaseCRUDAPI, BaseCRUDAPIReadOnly } from "~/lib/api/base/base-clients";
|
||||
import type { QueryValue } from "~/lib/api/base/route";
|
||||
|
||||
interface ReadOnlyStoreActions<T extends BoundT> {
|
||||
getAll(page?: number, perPage?: number, params?: any): Ref<T[] | null>;
|
||||
getAll(page?: number, perPage?: number, params?: any): AsyncData<T[] | null, NuxtError<unknown> | null>;
|
||||
refresh(page?: number, perPage?: number, params?: any): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ interface StoreActions<T extends BoundT> extends ReadOnlyStoreActions<T> {
|
||||
* a lot of refreshing hooks to be called on operations
|
||||
*/
|
||||
export function useReadOnlyActions<T extends BoundT>(
|
||||
storeKey: string,
|
||||
api: BaseCRUDAPIReadOnly<T>,
|
||||
allRef: Ref<T[] | null> | null,
|
||||
loading: Ref<boolean>,
|
||||
@@ -29,7 +30,7 @@ export function useReadOnlyActions<T extends BoundT>(
|
||||
params.orderBy ??= "name";
|
||||
params.orderDirection ??= "asc";
|
||||
|
||||
const allItems = useAsyncData(useAsyncKey(), async () => {
|
||||
const allItems = useAsyncData(storeKey, async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const { data } = await api.getAll(page, perPage, params);
|
||||
@@ -80,6 +81,7 @@ export function useReadOnlyActions<T extends BoundT>(
|
||||
* a lot of refreshing hooks to be called on operations
|
||||
*/
|
||||
export function useStoreActions<T extends BoundT>(
|
||||
storeKey: string,
|
||||
api: BaseCRUDAPI<unknown, T, unknown>,
|
||||
allRef: Ref<T[] | null> | null,
|
||||
loading: Ref<boolean>,
|
||||
@@ -88,7 +90,7 @@ export function useStoreActions<T extends BoundT>(
|
||||
params.orderBy ??= "name";
|
||||
params.orderDirection ??= "asc";
|
||||
|
||||
const allItems = useAsyncData(useAsyncKey(), async () => {
|
||||
const allItems = useAsyncData(storeKey, async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const { data } = await api.getAll(page, perPage, params);
|
||||
|
||||
@@ -13,12 +13,13 @@ export const useData = function <T extends BoundT>(defaultObject: T) {
|
||||
};
|
||||
|
||||
export const useReadOnlyStore = function <T extends BoundT>(
|
||||
storeKey: string,
|
||||
store: Ref<T[]>,
|
||||
loading: Ref<boolean>,
|
||||
api: BaseCRUDAPIReadOnly<T>,
|
||||
params = {} as Record<string, QueryValue>,
|
||||
) {
|
||||
const storeActions = useReadOnlyActions(api, store, loading);
|
||||
const storeActions = useReadOnlyActions(`${storeKey}-store-readonly`, api, store, loading);
|
||||
const actions = {
|
||||
...storeActions,
|
||||
async refresh() {
|
||||
@@ -29,21 +30,22 @@ export const useReadOnlyStore = function <T extends BoundT>(
|
||||
},
|
||||
};
|
||||
|
||||
// initial hydration
|
||||
if (!loading.value && !store.value.length) {
|
||||
const result = actions.getAll(1, -1, params);
|
||||
store.value = result.value || [];
|
||||
actions.refresh();
|
||||
}
|
||||
|
||||
return { store, actions };
|
||||
};
|
||||
|
||||
export const useStore = function <T extends BoundT>(
|
||||
storeKey: string,
|
||||
store: Ref<T[]>,
|
||||
loading: Ref<boolean>,
|
||||
api: BaseCRUDAPI<unknown, T, unknown>,
|
||||
params = {} as Record<string, QueryValue>,
|
||||
) {
|
||||
const storeActions = useStoreActions(api, store, loading);
|
||||
const storeActions = useStoreActions(`${storeKey}-store`, api, store, loading);
|
||||
const actions = {
|
||||
...storeActions,
|
||||
async refresh() {
|
||||
@@ -54,9 +56,9 @@ export const useStore = function <T extends BoundT>(
|
||||
},
|
||||
};
|
||||
|
||||
// initial hydration
|
||||
if (!loading.value && !store.value.length) {
|
||||
const result = actions.getAll(1, -1, params);
|
||||
store.value = result.value || [];
|
||||
actions.refresh();
|
||||
}
|
||||
|
||||
return { store, actions };
|
||||
|
||||
Reference in New Issue
Block a user