ProductSelector.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template lang="pug">
  2. .product-selector
  3. modal(name="product-selector" transition="nice-modal-fade" height="500" @closed="handleClosed")
  4. input.variant-searcher(type="search" placeholder="Buscar una variante de producto" v-model="search")
  5. .product-variants
  6. product-variant(v-for="variant in variants" :key="variant.id" :data="variant")
  7. </template>
  8. <script>
  9. import { mapGetters, mapActions } from 'vuex'
  10. import Fuse from 'fuse.js'
  11. import ProductVariant from '@/components/ProductVariant'
  12. export default {
  13. components: {
  14. 'product-variant': ProductVariant
  15. },
  16. computed: mapGetters({
  17. variants: 'getVariants'
  18. }),
  19. methods: {
  20. initFuse() {
  21. if (this.fuse) {
  22. this.fuse.setCollection(this.variants)
  23. return
  24. }
  25. this.fuse = new Fuse(this.variants, {
  26. shouldSort: true,
  27. threshold: 0.4,
  28. location: 0,
  29. distance: 100,
  30. maxPatternLength: 32,
  31. minMatchCharLength: 1,
  32. keys: [
  33. 'name',
  34. 'display_name',
  35. 'ean13',
  36. ]
  37. })
  38. },
  39. fuzzySearch() {
  40. this.results = this.fuse.search(this.search)
  41. },
  42. handleClosed() {
  43. this.search = ''
  44. },
  45. ...mapActions([
  46. 'filterVariants'
  47. ])
  48. },
  49. watch: {
  50. variants() {
  51. this.initFuse()
  52. },
  53. search() {
  54. this.fuzzySearch()
  55. },
  56. results() {
  57. this.filterVariants(this.results)
  58. }
  59. },
  60. data() {
  61. return {
  62. search: '',
  63. results: [],
  64. fuse: null,
  65. }
  66. },
  67. mounted() {
  68. this.$store.watch(state => {
  69. return state.products.selectedProduct
  70. }, value => {
  71. if (value) {
  72. this.$modal.show("product-selector")
  73. } else {
  74. this.$modal.hide("product-selector")
  75. }
  76. })
  77. }
  78. }
  79. </script>
  80. <style lang="sass">
  81. .product-selector
  82. .variant-searcher
  83. width: calc(100% - 30px)
  84. height: 35px
  85. text-align: center
  86. margin: 10px 15px
  87. border-radius: 0
  88. font:
  89. size: 10pt
  90. style: normal
  91. weight: bold
  92. .product-variants
  93. width: 100%;
  94. height: calc(100% - 70px);
  95. overflow-y: auto;
  96. </style>