mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-30 18:49:41 +02:00
refactor(frontend): ♻️ rewrite search componenets to typescript
This commit is contained in:
parent
1981e191be
commit
bde885dc84
25 changed files with 826 additions and 113 deletions
51
frontend/composables/use-recipes.ts
Normal file
51
frontend/composables/use-recipes.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { useAsync, ref } from "@nuxtjs/composition-api";
|
||||
import { useAsyncKey } from "./use-utils";
|
||||
import { useApiSingleton } from "~/composables/use-api";
|
||||
import { Recipe } from "~/types/api-types/recipe";
|
||||
|
||||
export const allRecipes = ref<Recipe[] | null>([]);
|
||||
export const recentRecipes = ref<Recipe[] | null>([]);
|
||||
|
||||
export const useRecipes = (all = false, fetchRecipes = true) => {
|
||||
const api = useApiSingleton();
|
||||
|
||||
// recipes is non-reactive!!
|
||||
const { recipes, start, end } = (() => {
|
||||
if (all) {
|
||||
return {
|
||||
recipes: allRecipes,
|
||||
start: 0,
|
||||
end: 9999,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
recipes: recentRecipes,
|
||||
start: 0,
|
||||
end: 30,
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
async function refreshRecipes() {
|
||||
const { data } = await api.recipes.getAll(start, end);
|
||||
if (data) {
|
||||
recipes.value = data;
|
||||
}
|
||||
}
|
||||
|
||||
function getAllRecipes() {
|
||||
useAsync(async () => {
|
||||
await refreshRecipes();
|
||||
}, useAsyncKey());
|
||||
}
|
||||
|
||||
function assignSorted(val: Array<Recipe>) {
|
||||
recipes.value = val;
|
||||
}
|
||||
|
||||
if (fetchRecipes) {
|
||||
getAllRecipes();
|
||||
}
|
||||
|
||||
return { getAllRecipes, assignSorted };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue