@wikala/spmkt 2.25.8 → 2.27.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.
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <div class="add-to-cart" v-if="!headless">
2
+ <div class="add-to-cart">
3
3
  <template v-if="quantityEnabled && inStock" class="quantity">
4
4
  <quantity v-model="qty"></quantity>
5
5
  </template>
@@ -46,10 +46,6 @@ export default {
46
46
  default: function () {
47
47
  return {}
48
48
  }
49
- },
50
- headless: {
51
- type: Boolean,
52
- default: false
53
49
  }
54
50
  },
55
51
 
@@ -199,28 +195,6 @@ export default {
199
195
  }
200
196
  },
201
197
 
202
- initHeadless() {
203
- this.$bus.$on('externalAddToCart', async (product_id, e) => {
204
- let btn = e.target
205
- btn.classList.add('is-loading')
206
- let resp = await request({
207
- baseURL: config.BASE_URL,
208
- url: '/products/' + product_id
209
- })
210
- this.$emit('update', resp.data.product_resource)
211
- this.$nextTick(() => {
212
- if (this.product.id) {
213
- this.product_id = this.product.id
214
- if (this.product.variants.length > 0) {
215
- this.variantIds = _.pluck(this.product.variants, 'id')
216
- }
217
- }
218
- this.addToCart()
219
- btn.classList.remove('is-loading')
220
- })
221
- })
222
- },
223
-
224
198
  onSelectVariant(variant_id) {
225
199
  //console.log('receiving selectVariant ', variant_id)
226
200
  if (!variant_id) {
@@ -239,8 +213,8 @@ export default {
239
213
  },
240
214
 
241
215
  handleScroll() {
242
- if (this.$mq !== 'mobile' || this.headless) {
243
- return;
216
+ if (this.$mq !== 'mobile') {
217
+ return
244
218
  }
245
219
  const element = this.$el
246
220
  const pagetop = document.querySelector('#pagetop')
@@ -251,10 +225,10 @@ export default {
251
225
 
252
226
  if (scrollPosition > elementPosition) {
253
227
  element.classList.add('add-to-cart-fixed-footer')
254
- pagetop.classList.add('pos2');
228
+ pagetop.classList.add('pos2')
255
229
  } else {
256
- element.classList.remove('add-to-cart-fixed-footer');
257
- pagetop.classList.remove('pos2');
230
+ element.classList.remove('add-to-cart-fixed-footer')
231
+ pagetop.classList.remove('pos2')
258
232
  }
259
233
  }
260
234
 
@@ -264,16 +238,10 @@ export default {
264
238
 
265
239
  handleBusEvents() {
266
240
  this.$bus.$on('selectVariant', this.onSelectVariant)
267
-
268
- if (!this.headless) {
269
- this.$bus.$on('updateQty', (qty) => {
270
- console.log('received updateQty', qty)
271
- this.qty = qty
272
- })
273
- }
274
- if (this.headless) {
275
- this.initHeadless()
276
- }
241
+ this.$bus.$on('updateQty', (qty) => {
242
+ console.log('received updateQty', qty)
243
+ this.qty = qty
244
+ })
277
245
  }
278
246
  },
279
247
 
@@ -293,9 +261,6 @@ export default {
293
261
  })
294
262
 
295
263
  this.handleBusEvents()
296
-
297
-
298
-
299
264
  }
300
265
  }
301
266
  </script>
@@ -0,0 +1,149 @@
1
+ <template>
2
+ <div class="box-add-to-cart" @click="handleClick">
3
+ <slot></slot>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ import config from '../../config'
9
+ import _ from 'underscore'
10
+ import GoogleAnalyticsEEC from '../../utils/google-analytics-eec.js'
11
+
12
+ import { request, Cart } from '../../api'
13
+
14
+ export default {
15
+ name: 'BoxAddToCart',
16
+
17
+ props: {
18
+ productId: {
19
+ type: Number,
20
+ required: true
21
+ },
22
+ },
23
+
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)
41
+ }
42
+ //console.log('eecProduct ', eecProduct)
43
+ return eecProduct
44
+ },
45
+
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
+ },
51
+
52
+ variantsInStock() {
53
+ let vis = this.product.variants.filter(variant => {
54
+ return variant.in_stock
55
+ })
56
+ return vis.length
57
+ },
58
+
59
+ async updateCart() {
60
+ try {
61
+ let response = await Cart.update({
62
+ mode: this.mode,
63
+ id: this.product.id,
64
+ qty: this.qty
65
+ })
66
+ //console.log(response.data)
67
+ if (response.data) {
68
+ this.$bus.$emit('updateCart', response.data)
69
+ } else {
70
+ throw new Error(this.$t('cart.updateCartErrorUnknown'))
71
+ }
72
+ return response.data
73
+ } catch (error) {
74
+ if (!error.message) {
75
+ error.message = this.$t('cart.updateCartError', error.status)
76
+ }
77
+ this.$buefy.dialog.alert({
78
+ title: this.$t('global.error'),
79
+ message: error.message,
80
+ confirmText: 'OK'
81
+ })
82
+ } finally {
83
+ this.loading = false
84
+ }
85
+ },
86
+
87
+ async addToCart(event) {
88
+ this.loading = true
89
+ let cartResponse = await this.updateCart()
90
+ if (cartResponse) {
91
+ this.$emit('success')
92
+ this.$bus.$emit('addToCart', this.productDetails(), this.el$)
93
+ if (!!window.google_tag_manager) {
94
+ GoogleAnalyticsEEC.addToCart(this.eecProduct(), () => {
95
+ this.loadingAddToCart = false
96
+ })
97
+ }
98
+ }
99
+ },
100
+
101
+ async setProduct() {
102
+ this.product = {};
103
+ let resp = await request({
104
+ baseURL: config.BASE_URL,
105
+ url: '/products/' + this.product_id
106
+ })
107
+ this.product = resp.data.product_resource
108
+ },
109
+
110
+ async handleClick(e) {
111
+ let btn = e.target
112
+ btn.classList.add('is-loading')
113
+
114
+ await this.setProduct()
115
+
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();
123
+ }
124
+
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
+ return
137
+ }
138
+
139
+ this.addToCart()
140
+ btn.classList.remove('is-loading')
141
+ },
142
+
143
+ },
144
+
145
+ mounted() {
146
+ this.product_id = this.productId;
147
+ }
148
+ }
149
+ </script>
package/index.js CHANGED
@@ -52,6 +52,7 @@ import CategorySort from './components/Category/CategorySort' //t
52
52
  import LoadMore from './components/Category/LoadMore'
53
53
 
54
54
  import AddToCart from './components/Product/AddToCart' //t
55
+ import BoxAddToCart from './components/Product/BoxAddToCart' //t
55
56
  import Quantity from './components/Product/Quantity'
56
57
  import Compare from './components/Product/Compare'
57
58
  import CompareBox from './components/Product/CompareBox' //t
@@ -127,6 +128,7 @@ export {
127
128
  LoadMore,
128
129
 
129
130
  AddToCart,
131
+ BoxAddToCart,
130
132
  Quantity,
131
133
  Compare,
132
134
  CompareBox,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wikala/spmkt",
3
- "version": "2.25.8",
3
+ "version": "2.27.0",
4
4
  "private": false,
5
5
  "description": "supermarket",
6
6
  "scripts": {
package/utils/animate.js CHANGED
@@ -3,6 +3,10 @@ import 'animate.css'
3
3
  function animateCSS(element, animationName, callback) {
4
4
 
5
5
  const node = element instanceof HTMLElement ? element : document.querySelector(element)
6
+
7
+ if(!node) {
8
+ return
9
+ }
6
10
 
7
11
  node.classList.add('animate__animated', 'animate__' + animationName)
8
12