Created
August 15, 2020 15:18
-
-
Save simonjcarr/83f069b91f6c0e2c0fab42ae4cee76fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ref } from "vue"; | |
| export default function usePosts() { | |
| let post = ref({}); | |
| let posts = ref([]); | |
| let user = ref({}); | |
| function fetchPosts() { | |
| fetch(`https://jsonplaceholder.typicode.com/posts`) | |
| .then(response => response.json()) | |
| .then(data => (posts.value = data)); | |
| } | |
| function fetchPost(postId) { | |
| fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`) | |
| .then(response => response.json()) | |
| .then(data => (post.value = data)) | |
| .then(data => fetchUser(data.userId)); | |
| } | |
| function fetchUser(userId) { | |
| fetch(`https://jsonplaceholder.typicode.com/users/${userId}`) | |
| .then(response => response.json()) | |
| .then(data => (user.value = data)); | |
| } | |
| return { fetchPosts, fetchPost, fetchUser, post, posts, user }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment