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">
const props = defineProps<{
defineProps<{
title: string;
pictureUrl: string;
videoUrl: string;

View file

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

View file

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

View file

@ -5,7 +5,7 @@
<div>
<h1 class="text-5xl font-bold prose">Eat Something New</h1>
<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
</NuxtLink>
</div>

View file

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