/src/components/Authors/Box.vue

https://github.com/paweljw/bookstore-frontend · Vue · 44 lines · 41 code · 3 blank · 0 comment · 1 complexity · 2037ea603b4a3aa6a2e8329282884765 MD5 · raw file

  1. <template>
  2. <div class="col author text-center">
  3. <router-link :to="{ name: 'Author', params: { id: author.id } }">
  4. <img :src="image"></img>
  5. <p>{{ author.name }}</p>
  6. </router-link>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'Box',
  12. props: ['author'],
  13. data () {
  14. return {
  15. image: `http://via.placeholder.com/200x200?text=${encodeURIComponent(this.author.name)}`
  16. }
  17. },
  18. created () {
  19. this.pullImage()
  20. },
  21. methods: {
  22. async pullImage () {
  23. const response = await fetch(`http://api.duckduckgo.com/?q=${encodeURIComponent(this.author.name)}&format=json&pretty=1`)
  24. const json = await response.json()
  25. if (json.Image) {
  26. this.image = json.Image
  27. }
  28. }
  29. }
  30. }
  31. </script>
  32. <style lang="scss" scoped>
  33. .author {
  34. min-width: 260px;
  35. img {
  36. object-fit: cover;
  37. width: 100%;
  38. height: 260px;
  39. }
  40. }
  41. </style>