mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-04-11 23:45:36 -04:00
chore: Nuxt 4 upgrade (#7426)
This commit is contained in:
65
frontend/app/composables/partials/use-store-factory.ts
Normal file
65
frontend/app/composables/partials/use-store-factory.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useReadOnlyActions, useStoreActions } from "./use-actions-factory";
|
||||
import type { BoundT } from "./types";
|
||||
import type { BaseCRUDAPI, BaseCRUDAPIReadOnly } from "~/lib/api/base/base-clients";
|
||||
import type { QueryValue } from "~/lib/api/base/route";
|
||||
|
||||
export const useData = function <T extends BoundT>(defaultObject: T) {
|
||||
const data = reactive({ ...defaultObject });
|
||||
function reset() {
|
||||
Object.assign(data, defaultObject);
|
||||
};
|
||||
|
||||
return { data, reset };
|
||||
};
|
||||
|
||||
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(`${storeKey}-store-readonly`, api, store, loading);
|
||||
const actions = {
|
||||
...storeActions,
|
||||
async refresh() {
|
||||
return await storeActions.refresh(1, -1, params);
|
||||
},
|
||||
flushStore() {
|
||||
store.value = [];
|
||||
},
|
||||
};
|
||||
|
||||
// initial hydration
|
||||
if (!loading.value && !store.value.length) {
|
||||
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(`${storeKey}-store`, api, store, loading);
|
||||
const actions = {
|
||||
...storeActions,
|
||||
async refresh() {
|
||||
return await storeActions.refresh(1, -1, params);
|
||||
},
|
||||
flushStore() {
|
||||
store.value = [];
|
||||
},
|
||||
};
|
||||
|
||||
// initial hydration
|
||||
if (!loading.value && !store.value.length) {
|
||||
actions.refresh();
|
||||
}
|
||||
|
||||
return { store, actions };
|
||||
};
|
||||
Reference in New Issue
Block a user