mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
fix: refactor and fixes
This commit is contained in:
parent
fb84b4bfc6
commit
ba78374f81
4 changed files with 38 additions and 30 deletions
|
|
@ -2,36 +2,28 @@
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const categoryName = route.params.name as string;
|
const categoryName = route.params.name as string;
|
||||||
|
|
||||||
const { data: categories } = useCategories();
|
const { data: recipes, status } = await useCategoryRecipes(categoryName);
|
||||||
const { data: recipes, status } = useCategoryRecipes(categoryName);
|
|
||||||
|
|
||||||
const category = computed(() =>
|
if (!recipes.value) {
|
||||||
categories.value?.find((c) => c.name === categoryName)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!category.value) {
|
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
message: 'Category not found',
|
statusMessage: "Category not found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<div
|
<div class="hero h-[40vh] bg-cover bg-center relative">
|
||||||
class="hero h-[40vh] bg-cover bg-center relative"
|
|
||||||
:style="`background-image: url(${category.value.picture})`"
|
|
||||||
>
|
|
||||||
<div class="hero-overlay bg-opacity-60"></div>
|
<div class="hero-overlay bg-opacity-60"></div>
|
||||||
<div class="hero-content text-center text-neutral-content">
|
<div class="hero-content text-center text-neutral-content">
|
||||||
<h1 class="text-5xl font-bold">{{ category.value.name }}</h1>
|
<h1 class="text-5xl font-bold">{{ categoryName }}</h1>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container mx-auto px-4 py-8">
|
<div class="container mx-auto px-4 py-8">
|
||||||
<div class="prose max-w-none mb-12">
|
<div class="prose max-w-none mb-12">
|
||||||
<p>{{ category.value.description }}</p>
|
<!-- <p>{{ category!.description }}</p> -->
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="status === 'pending'" class="flex justify-center my-8">
|
<div v-if="status === 'pending'" class="flex justify-center my-8">
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { data: categories, status, error } = useCategories();
|
const { data: categories, status, error } = useCategories();
|
||||||
|
|
||||||
console.log(categories.value);
|
|
||||||
|
|
||||||
if (error.value) {
|
if (error.value) {
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
|
|
@ -36,7 +34,7 @@ if (error.value) {
|
||||||
</p>
|
</p>
|
||||||
<div class="card-actions justify-end">
|
<div class="card-actions justify-end">
|
||||||
<nuxt-link
|
<nuxt-link
|
||||||
:to="`/category/${category.name}`"
|
:to="`/categories/${category.name}`"
|
||||||
class="btn btn-primary"
|
class="btn btn-primary"
|
||||||
>
|
>
|
||||||
View Recipes
|
View Recipes
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,10 @@ import { z } from "zod";
|
||||||
import { publicProcedure, router } from "../trpc";
|
import { publicProcedure, router } from "../trpc";
|
||||||
import type { Meal } from "~/types/recipe";
|
import type { Meal } from "~/types/recipe";
|
||||||
import { parseRecipeData } from "~/utils/recipes";
|
import { parseRecipeData } from "~/utils/recipes";
|
||||||
|
import { categoryRecipesResponseSchema } from "~/types/category";
|
||||||
|
|
||||||
const { apiUrl } = useRuntimeConfig();
|
const { apiUrl } = useRuntimeConfig();
|
||||||
|
|
||||||
type Category = {
|
|
||||||
idCategory: string;
|
|
||||||
strCategory: string;
|
|
||||||
strCategoryThumb: string;
|
|
||||||
strCategoryDescription: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const recipeRouter = router({
|
export const recipeRouter = router({
|
||||||
recipesByCategory: publicProcedure
|
recipesByCategory: publicProcedure
|
||||||
.input(z.string())
|
.input(z.string())
|
||||||
|
|
@ -20,12 +14,16 @@ export const recipeRouter = router({
|
||||||
new URL(`filter.php?c=${input}`, apiUrl).href,
|
new URL(`filter.php?c=${input}`, apiUrl).href,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!data?.meals) {
|
const result = categoryRecipesResponseSchema.safeParse(data);
|
||||||
return [];
|
|
||||||
|
if (!result.success) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 404,
|
||||||
|
statusMessage: "Recipes for category not found",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const recipes = parseRecipeData(data);
|
return result.data.recipes;
|
||||||
return recipes;
|
|
||||||
}),
|
}),
|
||||||
recipeGet: publicProcedure
|
recipeGet: publicProcedure
|
||||||
.input(
|
.input(
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,23 @@ export const categoriesResponseSchema = z.object({
|
||||||
|
|
||||||
export type Category = z.infer<typeof categorySchema>;
|
export type Category = z.infer<typeof categorySchema>;
|
||||||
export type CategoriesResponse = z.infer<typeof categoriesResponseSchema>;
|
export type CategoriesResponse = z.infer<typeof categoriesResponseSchema>;
|
||||||
|
|
||||||
|
export const categoryRecipeSchema = z
|
||||||
|
.object({
|
||||||
|
strMeal: z.string(),
|
||||||
|
strMealThumb: z.string().url(),
|
||||||
|
idMeal: z.string(),
|
||||||
|
})
|
||||||
|
.transform((meal) => ({
|
||||||
|
title: meal.strMeal,
|
||||||
|
pictureUrl: meal.strMealThumb,
|
||||||
|
id: meal.idMeal,
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const categoryRecipesResponseSchema = z
|
||||||
|
.object({
|
||||||
|
meals: z.array(categoryRecipeSchema),
|
||||||
|
})
|
||||||
|
.transform((data) => ({
|
||||||
|
recipes: data.meals,
|
||||||
|
}));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue