/src/components/Books/Full.vue

https://github.com/paweljw/bookstore-frontend · Vue · 75 lines · 68 code · 7 blank · 0 comment · 1 complexity · 256bf32271a3e0e11cf0b602bbd048fe MD5 · raw file

  1. <template>
  2. <div class="col book text-center">
  3. <button class="btn btn-light btn-close" v-on:click="close">&times; close</button>
  4. <img :src="image"></img>
  5. <p>
  6. <strong>{{ book.title }}</strong>
  7. - {{ price }}
  8. </p>
  9. <button class="form-control btn btn-lg btn-primary">Add to cart</button>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'Full',
  15. props: ['author', 'book'],
  16. data () {
  17. return {
  18. image: `http://via.placeholder.com/220x320?text=${encodeURIComponent(this.book.title)}`
  19. }
  20. },
  21. created () {
  22. this.pullImage()
  23. },
  24. updated () {
  25. this.pullImage()
  26. },
  27. methods: {
  28. async pullImage () {
  29. this.image = `http://via.placeholder.com/220x320?text=${encodeURIComponent(this.book.title)}`
  30. const response = await fetch(`http://api.duckduckgo.com/?q=${encodeURIComponent(this.book.title)}&format=json&pretty=1`)
  31. const json = await response.json()
  32. if (json.Image) {
  33. this.image = json.Image
  34. }
  35. },
  36. close () {
  37. this.$emit('close')
  38. }
  39. },
  40. computed: {
  41. price () {
  42. return `${this.book.price_cents / 100} ${this.book.price_currency}`
  43. }
  44. }
  45. }
  46. </script>
  47. <style lang="scss" scoped>
  48. .book {
  49. min-width: 220px;
  50. max-width: 400px;
  51. img {
  52. object-fit: cover;
  53. width: 100%;
  54. height: 520px;
  55. }
  56. border-left: 1px #eee solid;
  57. animation: fadein 0.6s;
  58. }
  59. @keyframes fadein {
  60. from { opacity: 0; }
  61. to { opacity: 1; }
  62. }
  63. .btn-close {
  64. float: right;
  65. margin-bottom: 5px;
  66. cursor: pointer;
  67. }
  68. </style>