File size: 1,763 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<script lang="ts">
import { computed, defineComponent } from 'vue'
import { useQuery } from '@tanstack/vue-query'

import { Author, Post } from './types'

const fetchPost = async (id: number): Promise<Post> =>
  await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`).then(
    (response) => response.json(),
  )

const fetchAuthor = async (id: number): Promise<Author> =>
  await fetch(`https://jsonplaceholder.typicode.com/users/${id}`).then(
    (response) => response.json(),
  )

export default defineComponent({
  name: 'PostDetails',
  props: {
    postId: {
      type: Number,
      required: true,
    },
  },
  emits: ['setPostId'],
  setup(props) {
    const {
      isPending,
      isError,
      isFetching,
      data: post,
      error,
    } = useQuery({
      queryKey: ['post', props.postId],
      queryFn: () => fetchPost(props.postId),
    })

    const authorId = computed(() => post.value?.userId)

    const { data: author } = useQuery({
      queryKey: ['author', authorId],
      queryFn: ({ queryKey: [, id] }) => fetchAuthor(id),
      enabled: computed(() => !!authorId.value),
    })

    return { isPending, isError, isFetching, post, error, author }
  },
})
</script>

<template>
  <h1>Post {{ postId }}</h1>
  <a @click="$emit('setPostId', -1)" href="#"> Back </a>
  <div v-if="isPending" class="update">Loading...</div>
  <div v-else-if="isError">An error has occurred: {{ error }}</div>
  <div v-else-if="post">
    <h1>
      {{ post.title }} <span v-if="author">by {{ author.name }}</span>
    </h1>
    <div>
      <p>{{ post.body }}</p>
    </div>
    <div v-if="isFetching" class="update">Background Updating...</div>
  </div>
</template>

<style scoped>
.update {
  font-weight: bold;
  color: green;
}
</style>