fix: client side navigation

This commit is contained in:
Ruidy 2024-04-27 12:18:47 +02:00
parent 44beb6f61b
commit bd5fe9be95
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
5 changed files with 24 additions and 17 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
const props = defineProps<{ defineProps<{
title: string; title: string;
pictureUrl: string; pictureUrl: string;
videoUrl: string; videoUrl: string;

View file

@ -7,22 +7,28 @@ type Recipe = {
}; };
export default async function () { export default async function () {
const { data, error } = await useAsyncData("random", () => { const { data, pending, error } = await useAsyncData(
const config = useRuntimeConfig(); "random",
return $fetch(`${config.apiUrl}random.php`); () => {
}); const config = useRuntimeConfig();
return $fetch(`${config.apiUrl}random.php`);
},
{ lazy: true },
);
const tmp = computed(() => data.value?.meals?.[0]);
const tmp = data.value?.meals?.[0];
const recipe = reactive<Recipe>({ const recipe = reactive<Recipe>({
title: tmp?.strMeal, title: tmp.value.strMeal,
pictureUrl: tmp?.strMealThumb, pictureUrl: tmp.value.strMealThumb,
videoUrl: tmp?.strYoutube, videoUrl: tmp.value.strYoutube,
category: tmp?.strCategory, category: tmp.value.strCategory,
origin: tmp?.strArea, origin: tmp.value.strArea,
}); });
return { return {
recipe, recipe,
pending,
error, error,
}; };
} }

View file

@ -16,4 +16,4 @@ export default defineNuxtConfig({
public: {}, public: {},
}, },
ssr: true, ssr: true,
}); });

View file

@ -5,7 +5,7 @@
<div> <div>
<h1 class="text-5xl font-bold prose">Eat Something New</h1> <h1 class="text-5xl font-bold prose">Eat Something New</h1>
<p class="py-6 prose">Generate a random recipe.</p> <p class="py-6 prose">Generate a random recipe.</p>
<NuxtLink to="/random" class="btn btn-primary"> <NuxtLink to="/random" class="btn btn-primary" external>
Random Recipe Now Random Recipe Now
</NuxtLink> </NuxtLink>
</div> </div>

View file

@ -1,15 +1,16 @@
<script setup lang="ts"> <script setup lang="ts">
const { recipe, error } = await useRecipe(); const { recipe, pending, error } = await useRecipe();
</script> </script>
<template> <template>
<div v-if="error">Failed: {{ error }}</div> <div v-if="pending">Loading</div>
<div v-else-if="error">Failed: {{ error }}</div>
<section v-else> <section v-else>
<div class="card w-96 bg-base-100 shadow-xl"> <div class="card w-96 bg-base-100 shadow-xl">
<RecipeCard <RecipeCard
:title="recipe.title" :title="recipe.title"
:pictureUrl="recipe.pictureUrl" :picture-url="recipe.pictureUrl"
:videoUrl="recipe.videoUrl" :video-url="recipe.videoUrl"
:category="recipe.category" :category="recipe.category"
:origin="recipe.origin" :origin="recipe.origin"
/> />