@wikala/spmkt 2.31.1 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/MIGRATION_GUIDE.md +261 -0
  2. package/api/product.js +1 -0
  3. package/api/request.js +9 -4
  4. package/api/user.js +1 -1
  5. package/components/Account/AccountDashboard.vue +101 -101
  6. package/components/Account/AccountStore.js +57 -31
  7. package/components/Account/AffiliateReport.vue +56 -60
  8. package/components/Account/{OrderDetail.vue → Orders/OrderDetail.vue} +44 -30
  9. package/components/Account/Orders/Orders.vue +124 -0
  10. package/components/Account/{AccountSettings.vue → Settings/AccountSettings.vue} +4 -4
  11. package/components/Account/Settings/ChangeEmail.vue +136 -0
  12. package/components/Account/Settings/ChangeName.vue +82 -0
  13. package/components/Account/Settings/ChangePassword.vue +91 -0
  14. package/components/Address/Address.vue +38 -57
  15. package/components/Address/AddressBook.vue +77 -57
  16. package/components/Address/AddressSelect.vue +94 -87
  17. package/components/Address/Edit.vue +253 -265
  18. package/components/Address/PostalCode.vue +22 -21
  19. package/components/AgeGate.vue +11 -5
  20. package/components/Auth/Login.vue +62 -44
  21. package/components/Auth/Register.vue +82 -60
  22. package/components/Auth/ResetPassword.vue +45 -30
  23. package/components/Cart/AgeVerification/AgeChecker.vue +3 -3
  24. package/components/Cart/CartBadge.vue +15 -4
  25. package/components/Cart/CartBox.vue +19 -11
  26. package/components/Cart/CartItem.vue +3 -3
  27. package/components/Cart/CartMode.vue +6 -5
  28. package/components/Cart/CartPreview.vue +24 -18
  29. package/components/Cart/CreditCard/Everyware.vue +10 -10
  30. package/components/Cart/CreditCard/ExtendedCreditCard.vue +5 -5
  31. package/components/Cart/CreditCard/SquarePay.vue +18 -16
  32. package/components/Cart/CreditCard/StandardCreditCard.vue +9 -9
  33. package/components/Cart/Delivery/Delivery.vue +16 -12
  34. package/components/Cart/Delivery/DeliveryAddress.vue +1 -1
  35. package/components/Cart/Delivery/DeliveryMethods.vue +1 -1
  36. package/components/Cart/Delivery/DeliveryOptions.vue +15 -9
  37. package/components/Cart/Delivery/DeliveryTypes.vue +8 -5
  38. package/components/Cart/Delivery/ReservationSlots.vue +14 -8
  39. package/components/Cart/Disclaimer.vue +1 -1
  40. package/components/Cart/OrderConfirmation.vue +1 -1
  41. package/components/Cart/Payment/Payment.vue +37 -33
  42. package/components/Cart/Payment/PaymentMethods.vue +6 -4
  43. package/components/Cart/ReviewAndSubmit.vue +9 -7
  44. package/components/Cart/ShoppingCart.vue +42 -31
  45. package/components/Cart/Store/actions.js +1 -1
  46. package/components/Cart/Store/getters.js +2 -1
  47. package/components/Cart/Store/index.js +1 -1
  48. package/components/Cart/Store/mutations.js +7 -5
  49. package/components/Cart/UserIdentity.vue +1 -1
  50. package/components/Category/CategoryFilter.vue +128 -114
  51. package/components/Category/CategorySort.vue +44 -32
  52. package/components/Category/LoadMore.vue +39 -23
  53. package/components/DeliverTo.vue +10 -15
  54. package/components/DeliveryNotice.vue +72 -68
  55. package/components/DobInput.vue +23 -23
  56. package/components/Gmaps/GmapsAutocomplete.vue +139 -78
  57. package/components/Gmaps/GmapsLocations.vue +1 -1
  58. package/components/Gmaps/GmapsSingleLocation.vue +121 -87
  59. package/components/ModalContainer/ModalContainer.vue +12 -9
  60. package/components/ModalContainer/index.js +61 -18
  61. package/components/Product/AddToCart.vue +237 -221
  62. package/components/Product/BoxAddToCart.vue +114 -86
  63. package/components/Product/Compare.vue +68 -48
  64. package/components/Product/CompareBox.vue +128 -123
  65. package/components/Product/Lookup.vue +92 -65
  66. package/components/Product/Photo.vue +105 -66
  67. package/components/Product/Price.vue +64 -43
  68. package/components/Product/ProductAutocomplete.vue +52 -57
  69. package/components/Product/ProductBox.vue +61 -38
  70. package/components/Product/ProductImages.vue +83 -86
  71. package/components/Product/Quantity.vue +115 -53
  72. package/components/Product/QuickView.vue +52 -45
  73. package/components/Product/RelatedProducts.vue +88 -23
  74. package/components/Product/SKU.vue +27 -8
  75. package/components/Product/Variants.vue +335 -341
  76. package/components/Product/reviews-questions/ProductQuestions.vue +226 -198
  77. package/components/Product/reviews-questions/ProductReviews.vue +122 -107
  78. package/components/Product/reviews-questions/Review.vue +54 -59
  79. package/components/Product/reviews-questions/ReviewForm.vue +222 -170
  80. package/components/Product/reviews-questions/ReviewImages.vue +197 -216
  81. package/components/Product/reviews-questions/Vote.vue +83 -57
  82. package/components/QuantityInput.vue +18 -8
  83. package/components/RangeInput.vue +15 -8
  84. package/components/Recaptcha.vue +8 -1
  85. package/components/SelectShippingCountry.vue +81 -62
  86. package/components/TimedOffer.vue +59 -74
  87. package/composables/globals.js +53 -0
  88. package/config.js +5 -2
  89. package/index.js +41 -10
  90. package/install.js +75 -0
  91. package/locales/en.json +2 -1
  92. package/locales/sr.json +2 -1
  93. package/package.json +43 -30
  94. package/utils/directives.js +28 -45
  95. package/utils/event-bus.js +56 -0
  96. package/utils/filters.js +24 -7
  97. package/utils/{gmaps.js → google/gmaps.js} +43 -10
  98. package/utils/{google-analytics-eec.js → google/google-analytics-eec.js} +7 -7
  99. package/babel.config.js +0 -5
  100. package/components/Account/ChangeEmail.vue +0 -133
  101. package/components/Account/ChangeName.vue +0 -92
  102. package/components/Account/ChangePassword.vue +0 -94
  103. package/components/Account/CreditCards.vue +0 -18
  104. package/components/Account/Orders.vue +0 -110
  105. package/components/Account/Settings.vue +0 -18
  106. package/components/Cart/Delivery/DeliveryAddressGuest.spec.js +0 -128
  107. package/components/Cart/Delivery/DeliveryTypes.spec.js +0 -83
  108. package/components/Cart/PCIBadge/SecurityMetrics.vue +0 -11
  109. package/components/Instagram.vue +0 -49
  110. package/components/ProductImageModal.vue +0 -41
  111. package/jest.config.js +0 -16
  112. package/test/e2e/.babelrc.js +0 -8
  113. package/test/e2e/.eslintrc.js +0 -11
  114. package/test/e2e/accAuth.spec.js +0 -64
  115. package/test/e2e/ageGate.spec.js +0 -26
  116. package/test/e2e/checkout.spec.js +0 -106
  117. package/test/e2e/compare.spec.js +0 -52
  118. package/test/e2e/config/data.js +0 -21
  119. package/test/e2e/config/routes.js +0 -11
  120. package/test/e2e/region.spec.js +0 -46
  121. package/test/e2e/search.spec.js +0 -32
  122. package/tests/vueBuefy.js +0 -4
  123. package/tsconfig.json +0 -8
@@ -5,11 +5,12 @@
5
5
  </template>
6
6
 
7
7
  <script>
8
+ import { ref, onMounted } from 'vue'
9
+ import { useBuefy, useT } from '../../composables/globals'
8
10
  import config from '../../config'
9
- import _ from 'underscore'
10
- import GoogleAnalyticsEEC from '../../utils/google-analytics-eec.js'
11
-
11
+ import GoogleAnalyticsEEC from '../../utils/google/google-analytics-eec.js'
12
12
  import { request, Cart } from '../../api'
13
+ import { eventBus } from '../../utils/event-bus'
13
14
 
14
15
  export default {
15
16
  name: 'BoxAddToCart',
@@ -21,129 +22,156 @@ export default {
21
22
  },
22
23
  },
23
24
 
24
- data() {
25
- return {
26
- loading: false,
27
- product: {},
28
- qty: 1,
29
- }
30
- },
31
-
32
- methods: {
33
- eecProduct() {
34
- let eecProduct = {
35
- item_id: this.product.id,
36
- item_name: this.product.name,
37
- price: this.product.price,
38
- item_brand: this.product.brand ? this.product.brand.name : '',
39
- item_category: this.product?.category?.path.startsWith('/') ? this.product?.category?.path.substr(1) : this.product?.category?.path,
40
- quantity: parseInt(this.qty, 10)
25
+ emits: ['success'],
26
+
27
+ setup(props, { emit }) {
28
+ const $buefy = useBuefy()
29
+ const { t: $t } = useT()
30
+
31
+ // Reactive state
32
+ const loading = ref(false)
33
+ const product = ref({})
34
+ const qty = ref(1)
35
+ const product_id = ref(props.productId)
36
+
37
+ // Methods
38
+ const eecProduct = () => {
39
+ const eecProduct = {
40
+ item_id: product.value.id,
41
+ item_name: product.value.name,
42
+ price: product.value.price,
43
+ item_brand: product.value.brand ? product.value.brand.name : '',
44
+ item_category: product.value?.category?.path.startsWith('/')
45
+ ? product.value?.category?.path.substr(1)
46
+ : product.value?.category?.path,
47
+ quantity: parseInt(qty.value, 10)
41
48
  }
42
- //console.log('eecProduct ', eecProduct)
43
49
  return eecProduct
44
- },
50
+ }
45
51
 
46
- productDetails() {
47
- let product = this.eecProduct()
48
- product.image_url = this.product.images && this.product.images[0] ? this.product.images[0].url : ''
49
- return product
50
- },
52
+ const productDetails = () => {
53
+ const prod = eecProduct()
54
+ prod.image_url = product.value.images && product.value.images[0]
55
+ ? product.value.images[0].url
56
+ : ''
57
+ return prod
58
+ }
51
59
 
52
- variantsInStock() {
53
- let vis = this.product.variants.filter(variant => {
60
+ const variantsInStock = () => {
61
+ const vis = product.value.variants?.filter(variant => {
54
62
  return variant.in_stock
55
- })
63
+ }) || []
56
64
  return vis.length
57
- },
65
+ }
58
66
 
59
- async updateCart() {
67
+ const updateCart = async () => {
60
68
  try {
61
- let response = await Cart.update({
62
- mode: this.mode,
63
- id: this.product.id,
64
- qty: this.qty
69
+ const response = await Cart.update({
70
+ mode: 'add',
71
+ id: product.value.id,
72
+ qty: qty.value
65
73
  })
66
- //console.log(response.data)
74
+
67
75
  if (response.data) {
68
- this.$bus.$emit('updateCart', response.data)
76
+ eventBus.emit('updateCart', response.data)
69
77
  } else {
70
- throw new Error(this.$t('cart.updateCartErrorUnknown'))
78
+ throw new Error($t('cart.updateCartErrorUnknown'))
71
79
  }
72
80
  return response.data
73
81
  } catch (error) {
74
82
  if (!error.message) {
75
- error.message = this.$t('cart.updateCartError', error.status)
83
+ error.message = $t('cart.updateCartError', error.status)
76
84
  }
77
- this.$buefy.dialog.alert({
78
- title: this.$t('global.error'),
85
+ $buefy.dialog.alert({
86
+ title: $t('global.error'),
79
87
  message: error.message,
80
88
  confirmText: 'OK'
81
89
  })
82
90
  } finally {
83
- this.loading = false
91
+ loading.value = false
84
92
  }
85
- },
93
+ }
86
94
 
87
- async addToCart(event) {
88
- this.loading = true
89
- let cartResponse = await this.updateCart()
95
+ const addToCart = async () => {
96
+ loading.value = true
97
+ const cartResponse = await updateCart()
90
98
  if (cartResponse) {
91
- this.$emit('success')
92
- this.$bus.$emit('addToCart', this.productDetails(), this.el$)
99
+ emit('success')
100
+ eventBus.emit('addToCart', productDetails())
93
101
  if (!!window.google_tag_manager) {
94
- GoogleAnalyticsEEC.addToCart(this.eecProduct(), () => {
95
- this.loadingAddToCart = false
102
+ GoogleAnalyticsEEC.addToCart(eecProduct(), () => {
103
+ loading.value = false
96
104
  })
97
105
  }
98
106
  }
99
- },
107
+ }
100
108
 
101
- async setProduct() {
102
- this.product = {};
103
- let resp = await request({
109
+ const setProduct = async () => {
110
+ product.value = {}
111
+ try {
112
+ const resp = await request({
113
+ // The storefront's products/{id} route; the API one is admin-only
104
114
  baseURL: config.BASE_URL,
105
- url: '/products/' + this.product_id
115
+ url: '/products/' + product_id.value
106
116
  })
107
- this.product = resp.data.product_resource
108
- },
117
+ product.value = resp.data.product_resource || {}
118
+ } catch (e) {
119
+ // The request helper rejects on HTTP errors and timeouts. Leave the product empty
120
+ // so handleClick's product id check stops instead of throwing
121
+ console.error('Cannot get product', e)
122
+ }
123
+ }
109
124
 
110
- async handleClick(e) {
111
- let btn = e.target
112
- btn.classList.add('is-loading')
113
-
114
- await this.setProduct()
125
+ const handleClick = async (e) => {
126
+ const btn = e.target
127
+ btn.classList.add('is-loading')
115
128
 
116
- if (!this.product.id) {
117
- return
118
- }
119
-
120
- if(this.variantsInStock() === 1) {
121
- this.product_id = this.product.variants.find(variant => variant.in_stock).id
122
- await this.setProduct();
129
+ try {
130
+ await setProduct()
131
+
132
+ if (!product.value.id) {
133
+ return
123
134
  }
124
135
 
125
- if(this.variantsInStock() > 1) {
126
- this.$buefy.dialog.confirm({
127
- title: 'Proizvod sa varijantama',
128
- message: 'Otvorićemo stranicu proizvoda da biste odabrali varijantu.',
129
- confirmText: 'Ok',
130
- type: 'is-success',
131
- onConfirm: () => {
132
- window.location.href = '/'+this.product.slug;
133
- }
134
- })
135
- btn.classList.remove('is-loading')
136
+ if (variantsInStock() === 1) {
137
+ product_id.value = product.value.variants.find(variant => variant.in_stock).id
138
+ await setProduct()
139
+ if (!product.value.id) {
136
140
  return
141
+ }
142
+ }
143
+
144
+ if (variantsInStock() > 1) {
145
+ $buefy.dialog.confirm({
146
+ title: 'Proizvod sa varijantama',
147
+ message: 'Otvorićemo stranicu proizvoda da biste odabrali varijantu.',
148
+ confirmText: 'Ok',
149
+ type: 'is-success',
150
+ ariaRole: 'alertdialog',
151
+ ariaModal: true,
152
+ onConfirm: () => {
153
+ window.location.href = '/' + product.value.slug
154
+ }
155
+ })
156
+ return
137
157
  }
138
158
 
139
- this.addToCart()
159
+ await addToCart()
160
+ } finally {
140
161
  btn.classList.remove('is-loading')
141
- },
162
+ }
163
+ }
142
164
 
143
- },
165
+ onMounted(() => {
166
+ product_id.value = props.productId
167
+ })
144
168
 
145
- mounted() {
146
- this.product_id = this.productId;
169
+ return {
170
+ loading,
171
+ product,
172
+ qty,
173
+ handleClick
174
+ }
147
175
  }
148
176
  }
149
177
  </script>
@@ -19,7 +19,7 @@
19
19
  <tr v-for="(attributes, i) in itemGeneral" :key="i">
20
20
  <td>{{ attributes[0].name }}</td>
21
21
  <td v-for="(attrs, i) in attributes" :key="i">
22
- <div v-if="attrs.options">{{ attrs.options | string }}</div>
22
+ <div v-if="attrs.options">{{ stringFilter(attrs.options) }}</div>
23
23
  <p v-else>{{ attrs.value }}</p>
24
24
  </td>
25
25
  </tr>
@@ -29,7 +29,7 @@
29
29
  <tr v-for="(attributes, i) in itemHeating" :key="i">
30
30
  <td>{{ attributes[0].name }}</td>
31
31
  <td v-for="(attrs, i) in attributes" :key="i">
32
- <div v-if="attrs.options">{{ attrs.options | string }}</div>
32
+ <div v-if="attrs.options">{{ stringFilter(attrs.options) }}</div>
33
33
  <p v-else>{{ attrs.value }}</p>
34
34
  </td>
35
35
  </tr>
@@ -55,54 +55,74 @@
55
55
  </div>
56
56
  </template>
57
57
 
58
- <script>
59
- import Lookup from '../../components/Product/Lookup'
58
+ <script setup>
59
+ import { ref, onMounted, getCurrentInstance } from 'vue'
60
+ import Lookup from './Lookup.vue'
60
61
  import scrollMonitor from 'scrollmonitor'
61
62
 
62
- export default {
63
- name: 'ComparePage',
64
- components: { Lookup },
65
- data() {
66
- return {
67
- items: [],
68
- itemGeneral: {},
69
- itemHeating: {},
70
- itemBattery: {},
71
- itemDimensions: {}
72
- }
73
- },
74
- filters: {
75
- string(value) {
76
- if (!value) return ''
77
- value = value.toString()
78
- return value
79
- }
80
- },
81
- beforeMount() {
82
- this.items = window.compare.product
83
- this.itemGeneral = window.compare.General
84
- this.itemHeating = window.compare.Heating
85
- this.itemDimensions = window.compare.Dimensions
86
- this.itemBattery = window.compare.Battery
87
- },
88
- mounted() {
89
- window.addEventListener(
90
- 'load',
91
- () => {
92
- const elToWatch = document.querySelectorAll('.fixedContent')
93
-
94
- const watcher = scrollMonitor.create(elToWatch)
95
- watcher.lock()
96
-
97
- watcher.exitViewport(function () {
98
- elToWatch.forEach((el) => el.classList.add('scrolled'))
99
- })
100
- watcher.enterViewport(function () {
101
- elToWatch.forEach((el) => el.classList.remove('scrolled'))
102
- })
103
- },
104
- false
105
- )
63
+ defineOptions({ name: 'ComparePage' })
64
+
65
+ const props = defineProps({
66
+ compareData: {
67
+ type: Object,
68
+ default: () => ({
69
+ product: [],
70
+ General: {},
71
+ Heating: {},
72
+ Dimensions: {},
73
+ Battery: {}
74
+ })
106
75
  }
76
+ })
77
+
78
+ // Get global properties
79
+ const instance = getCurrentInstance()
80
+
81
+ // Reactive state
82
+ const items = ref([])
83
+ const itemGeneral = ref({})
84
+ const itemHeating = ref({})
85
+ const itemBattery = ref({})
86
+ const itemDimensions = ref({})
87
+
88
+ // Methods
89
+ const stringFilter = (value) => {
90
+ if (!value) return ''
91
+ value = value.toString()
92
+ return value
107
93
  }
94
+
95
+ const initScrollMonitor = () => {
96
+ const elToWatch = document.querySelectorAll('.fixedContent')
97
+
98
+ const watcher = scrollMonitor.create(elToWatch)
99
+ watcher.lock()
100
+
101
+ watcher.exitViewport(function () {
102
+ elToWatch.forEach((el) => el.classList.add('scrolled'))
103
+ })
104
+ watcher.enterViewport(function () {
105
+ elToWatch.forEach((el) => el.classList.remove('scrolled'))
106
+ })
107
+ }
108
+
109
+ // Lifecycle hooks
110
+ onMounted(() => {
111
+ // Check if compareData prop is provided, otherwise use window.compare
112
+ if (props.compareData && props.compareData.product.length > 0) {
113
+ items.value = props.compareData.product
114
+ itemGeneral.value = props.compareData.General
115
+ itemHeating.value = props.compareData.Heating
116
+ itemDimensions.value = props.compareData.Dimensions
117
+ itemBattery.value = props.compareData.Battery
118
+ } else if (window.compare) {
119
+ items.value = window.compare.product
120
+ itemGeneral.value = window.compare.General
121
+ itemHeating.value = window.compare.Heating
122
+ itemDimensions.value = window.compare.Dimensions
123
+ itemBattery.value = window.compare.Battery
124
+ }
125
+
126
+ window.addEventListener('load', initScrollMonitor, false)
127
+ })
108
128
  </script>
@@ -11,147 +11,152 @@
11
11
  <img :src="product.image" :alt="product.name" />
12
12
  </div>
13
13
  </div>
14
- <button name="compare_button" :disabled="products.length < 2" :class="['button', 'is-primary', { 'is-small': $mq === 'mobile' }]" @click="compare">
14
+ <button name="compare_button" :disabled="products.length < 2" :class="['button', 'is-primary', { 'is-small': mq === 'mobile' }]" @click="compare">
15
15
  {{ $t('product.compare') }}
16
16
  </button>
17
- <button name="clear_compare" :class="['button', 'btn-close', { 'is-small': $mq === 'mobile' }]" @click="hideCompareBox"> &times; </button>
17
+ <button name="clear_compare" :class="['button', 'btn-close', { 'is-small': mq === 'mobile' }]" @click="hideCompareBox"> &times; </button>
18
18
  </div>
19
19
  </div>
20
20
  </template>
21
21
 
22
- <script>
22
+ <script setup>
23
+ import { ref, onMounted, onBeforeUnmount } from 'vue'
24
+ import { useBuefy, useMq, useT } from '../../composables/globals'
23
25
  import _ from 'underscore'
24
26
  import Cookie from 'js-cookie'
27
+ import { eventBus } from '../../utils/event-bus'
25
28
 
26
- import { Compare } from '../../api'
29
+ defineOptions({ name: 'CompareBox' })
27
30
 
28
- export default {
29
- name: 'CompareBox',
30
- data() {
31
- return {
32
- attTemplateId: 0,
33
- products: [],
34
- viewProducts: true
35
- }
36
- },
37
-
38
- methods: {
39
- initCompareList() {
40
- let cCompareList = ''
41
- try {
42
- cCompareList = JSON.parse(Cookie.get('compareList'))
43
- } catch (e) {
44
- console.log('Cannot parse compareList', Cookie.get('compareList'))
45
- }
46
- if (cCompareList) {
47
- this.products = _.compact(cCompareList)
48
- this.products.forEach((product) => {
49
- this.setCompareIconStatus(product.id, 'on')
50
- })
51
- this.setAttTemplateId()
52
- }
53
- },
31
+ const $buefy = useBuefy()
32
+ const { t: $t } = useT()
33
+ const mq = useMq()
54
34
 
55
- setAttTemplateId() {
56
- if (this.products.length > 0) {
57
- this.attTemplateId = this.products[0]['att_template_id']
58
- }
59
- },
60
-
61
- setProducts(products) {
62
- this.products = products
63
- this.setAttTemplateId()
64
- Cookie.set('compareList', JSON.stringify(this.products), { expires: 1 })
65
- },
66
-
67
- addProduct(product) {
68
- if (this.products.length >= 4) {
69
- this.$buefy.dialog.alert({
70
- title: this.$t('product.compareErrorDialogTitle'),
71
- message: this.$t('product.compareErrorMaxProducts'),
72
- canCancel: ['button', 'escape'],
73
- confirmText: this.$t('product.compareErrorDialogConfirm'),
74
- cancelText: this.$t('global.cancel'),
75
- onConfirm: () => {
76
- this.compare()
77
- }
78
- })
79
- return false
35
+ // Reactive state
36
+ const attTemplateId = ref(0)
37
+ const products = ref([])
38
+ const viewProducts = ref(true)
39
+
40
+ // Methods
41
+ const setCompareIconStatus = (product_id, status) => {
42
+ let productIconEl = document.getElementById('prod-compare-' + product_id)
43
+ if (!productIconEl) {
44
+ return false
45
+ }
46
+ if (status == 'on') {
47
+ productIconEl.classList.add('has-text-info')
48
+ } else {
49
+ productIconEl.classList.remove('has-text-info')
50
+ }
51
+ }
52
+
53
+ const initCompareList = () => {
54
+ let cCompareList = ''
55
+ try {
56
+ cCompareList = JSON.parse(Cookie.get('compareList'))
57
+ } catch (e) {
58
+ console.log('Cannot parse compareList', Cookie.get('compareList'))
59
+ }
60
+ if (cCompareList) {
61
+ products.value = _.compact(cCompareList)
62
+ products.value.forEach((product) => {
63
+ setCompareIconStatus(product.id, 'on')
64
+ })
65
+ setAttTemplateId()
66
+ }
67
+ }
68
+
69
+ const setAttTemplateId = () => {
70
+ if (products.value.length > 0) {
71
+ attTemplateId.value = products.value[0]['att_template_id']
72
+ }
73
+ }
74
+
75
+ const setProducts = (newProducts) => {
76
+ products.value = newProducts
77
+ setAttTemplateId()
78
+ Cookie.set('compareList', JSON.stringify(products.value), { expires: 1 })
79
+ }
80
+
81
+ const addProduct = (product) => {
82
+ if (products.value.length >= 4) {
83
+ $buefy.dialog.alert({
84
+ title: $t('product.compareErrorDialogTitle'),
85
+ message: $t('product.compareErrorMaxProducts'),
86
+ canCancel: ['button', 'escape'],
87
+ confirmText: $t('product.compareErrorDialogConfirm'),
88
+ cancelText: $t('global.cancel'),
89
+ onConfirm: () => {
90
+ compare()
80
91
  }
81
- this.products.push(product)
82
- this.setCompareIconStatus(product.id, 'on')
83
- this.setAttTemplateId()
84
- Cookie.set('compareList', JSON.stringify(this.products), { expires: 1 })
85
- },
86
-
87
- compare() {
88
- const productList = this.products.map((el) => el.id)
89
- window.location.href = `/compare-products?products=${productList}`
90
- },
91
-
92
- removeProd(id) {
93
- this.setProducts(_.reject(this.products, (product) => product.id == id))
94
- this.setCompareIconStatus(id, 'off')
95
- },
96
-
97
- clear() {
98
- this.products.forEach((product) => {
99
- this.setCompareIconStatus(product.id, 'off')
100
- })
101
- this.setProducts([])
102
- },
92
+ })
93
+ return false
94
+ }
95
+ products.value.push(product)
96
+ setCompareIconStatus(product.id, 'on')
97
+ setAttTemplateId()
98
+ Cookie.set('compareList', JSON.stringify(products.value), { expires: 1 })
99
+ }
100
+
101
+ const compare = () => {
102
+ const productList = products.value.map((el) => el.id)
103
+ window.location.href = `/compare-products?products=${productList}`
104
+ }
105
+
106
+ const removeProd = (id) => {
107
+ setProducts(_.reject(products.value, (product) => product.id == id))
108
+ setCompareIconStatus(id, 'off')
109
+ }
110
+
111
+ const clear = () => {
112
+ products.value.forEach((product) => {
113
+ setCompareIconStatus(product.id, 'off')
114
+ })
115
+ setProducts([])
116
+ }
117
+
118
+ const hideCompareBox = () => {
119
+ $buefy.dialog.alert({
120
+ title: $t('product.compareCloseDialogTitle'),
121
+ message: $t('product.compareCloseDialogMsg'),
122
+ canCancel: ['button', 'escape'],
123
+ confirmText: $t('product.compareClear'),
124
+ cancelText: $t('global.cancel'),
125
+ onConfirm: () => {
126
+ clear()
127
+ }
128
+ })
129
+ }
103
130
 
104
- hideCompareBox() {
105
- this.$buefy.dialog.alert({
106
- title: this.$t('product.compareCloseDialogTitle'),
107
- message: this.$t('product.compareCloseDialogMsg'),
131
+ const handleCompareProduct = (productData) => {
132
+ if (_.findWhere(products.value, { id: productData.id })) {
133
+ removeProd(productData.id)
134
+ } else {
135
+ if (products.value.length > 0 && productData.att_template_id !== attTemplateId.value) {
136
+ $buefy.dialog.alert({
137
+ title: $t('global.error'),
138
+ message: $t('product.compareErrorDifferentCategories'),
108
139
  canCancel: ['button', 'escape'],
109
- confirmText: this.$t('product.compareClear'),
110
- cancelText: this.$t('global.cancel'),
140
+ confirmText: $t('product.compareClear'),
141
+ cancelText: $t('global.cancel'),
111
142
  onConfirm: () => {
112
- this.clear()
143
+ clear()
144
+ addProduct(productData)
113
145
  }
114
146
  })
115
- //this.viewProducts = !this.viewProducts
116
- },
117
-
118
- setCompareIconStatus(product_id, status) {
119
- let productIconEl = document.getElementById('prod-compare-' + product_id)
120
- if (!productIconEl) {
121
- return false
122
- }
123
- if (status == 'on') {
124
- productIconEl.classList.add('has-text-info')
125
- } else {
126
- productIconEl.classList.remove('has-text-info')
127
- }
147
+ return false
128
148
  }
129
- },
130
-
131
- mounted() {
132
- this.initCompareList()
133
-
134
- this.$bus.$on('compareProduct', (productData) => {
135
- if (_.findWhere(this.products, { id: productData.id })) {
136
- this.removeProd(productData.id)
137
- } else {
138
- if (this.products.length > 0 && productData.att_template_id !== this.attTemplateId) {
139
- this.$buefy.dialog.alert({
140
- title: this.$t('global.error'),
141
- message: this.$t('product.compareErrorDifferentCategories'),
142
- canCancel: ['button', 'escape'],
143
- confirmText: this.$t('product.compareClear'),
144
- cancelText: this.$t('global.cancel'),
145
- onConfirm: () => {
146
- this.clear()
147
- this.addProduct(productData)
148
- }
149
- })
150
- return false
151
- }
152
- this.addProduct(productData)
153
- }
154
- })
149
+ addProduct(productData)
155
150
  }
156
151
  }
152
+
153
+ // Lifecycle hooks
154
+ onMounted(() => {
155
+ initCompareList()
156
+ eventBus.on('compareProduct', handleCompareProduct)
157
+ })
158
+
159
+ onBeforeUnmount(() => {
160
+ eventBus.off('compareProduct', handleCompareProduct)
161
+ })
157
162
  </script>