1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-20 05:39:40 +02:00
mealie/frontend/composables/recipes/use-recipe.ts
Hoa (Kyle) Trinh c24d532608
feat: Migrate to Nuxt 3 framework (#5184)
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
2025-06-19 17:09:12 +00:00

46 lines
984 B
TypeScript

import { useUserApi } from "~/composables/api";
import type { Recipe } from "~/lib/api/types/recipe";
export const useRecipe = function (slug: string, eager = true) {
const api = useUserApi();
const loading = ref(false);
const recipe = ref<Recipe | null>(null);
async function fetchRecipe() {
loading.value = true;
const { data } = await api.recipes.getOne(slug);
loading.value = false;
if (data) {
recipe.value = data;
}
}
async function deleteRecipe() {
loading.value = true;
const { data } = await api.recipes.deleteOne(slug);
loading.value = false;
return data;
}
async function updateRecipe(recipe: Recipe) {
loading.value = true;
const { data } = await api.recipes.updateOne(slug, recipe);
loading.value = false;
return data;
}
onMounted(() => {
if (eager) {
fetchRecipe();
}
});
return {
recipe,
loading,
fetchRecipe,
deleteRecipe,
updateRecipe,
};
};