/src/components/Books/Box.vue

https://github.com/paweljw/bookstore-frontend · Vue · 49 lines · 45 code · 4 blank · 0 comment · 1 complexity · 0f3618f615da6893dc8ec2e1c62ef2be MD5 · raw file

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