Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

PostDetail.vue

42 lines1.1 KB

1<script setup lang="ts">
2import { useQuery } from '@tanstack/vue-query'
3import { computed } from 'vue'
4import { authorsQueries } from './services/authors/queries'
5import { postsQueries } from './services/posts/queries'
6​
7const props = defineProps<{ postId: number }>()
8defineEmits<{ setPostId: [id: number] }>()
9​
10const {
11 isPending,
12 isError,
13 isFetching,
14 data: post,
15 error,
16} = useQuery(() => postsQueries.detail(props.postId))
17​
18const authorId = computed(() => post.value?.userId)
19​
20const { data: author } = useQuery(() => authorsQueries.detail(authorId.value))
21</script>
22​
23<template>
24 <h1>Post {{ postId }}</h1>
25 <a href="#" @click="$emit('setPostId', -1)"> Back </a>
26 <div v-if="isPending" class="update">Loading...</div>
27 <div v-else-if="isError">An error has occurred: {{ error }}</div>
28 <div v-else-if="post">
29 <h1>{{ post.title }} <span v-if="author">by {{ author.name }}</span></h1>
30 <div>
31 <p>{{ post.body }}</p>
32 </div>
33 <div v-if="isFetching" class="update">Background Updating...</div>
34 </div>
35</template>
36​
37<style scoped>
38.update {
39 font-weight: bold;
40 color: green;
42</style>