Blame view

frontend/web/js/product.js 3.97 KB
ec957aa4   Volodymyr   product modal js
1
2
  var phoneInput = document.querySelector('#cardProduct [type="tel"]');
  var counter = document.querySelector('#cardProduct .product-counter__input');
79137940   Volodymyr   fix product modal
3
  var price = 0;
e582a8c8   Volodymyr   feedback message ...
4
5
6
7
8
9
10
11
12
  $('#order-product').click(function (e){
      e.preventDefault();
      var message='Сума: ';
      message += $('#cardProduct .product-counter__value').text();
      message +='\nКод товара: ' + $('#product-code').val();
      message +='\nКількість: ' + counter.querySelector('input').value;
      console.log(message + phoneInput.value);
      var phone = '+38' + phoneInput.value;
      if(phone.length===17){
f3a5cf05   Volodymyr   feedback message ...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
          $.ajax({
              type: "POST",
              url: '/site/feedback',
              data:
                  {'Feedback': {
                          'phone': phone,
                          'message': message,
                          'topic': 'product',
                          'name': phone,
                          'returnUrl': '/'
                      }
                  },
              success: function(data) {
                  if (data.success) {
6d2f2cf1   Volodymyr   success on produc...
27
28
29
30
                      $('#cardProduct')
                          .modal('hide');
                      $('#success-modal')
                          .modal('show');
f3a5cf05   Volodymyr   feedback message ...
31
32
33
34
35
36
37
38
                  } else {
                      alert(data.error)
                  }
              },
              error: function() {
                  alert('Achtung!');
              }
          });
e582a8c8   Volodymyr   feedback message ...
39
40
41
          console.log('good')
      }
  })
ec957aa4   Volodymyr   product modal js
42
43
44
  if (phoneInput) {
      phoneInput.addEventListener('input', phoneHandleInput);
  }
7445c339   Volodymyr   product modal js
45
  $('#cardProduct').on('show.bs.modal', function (e) {
79137940   Volodymyr   fix product modal
46
47
48
49
50
51
      var button = $(e.relatedTarget);
      price = button.data('price');
      $('#cardProduct .product-counter__value').data('price', price);
      var itemPrice = counter.parentElement.querySelector('.product-counter__value');
      itemPrice.innerHTML = `${price} ${itemPrice.dataset.currency}`;
      counter.querySelector('input').value = 1;
e582a8c8   Volodymyr   feedback message ...
52
53
54
      var sku = button.data('sku');
      $('#product-code').val(sku);
      $('#cardProduct .product-code').text('Код товара ' + sku);
7445c339   Volodymyr   product modal js
55
  })
e582a8c8   Volodymyr   feedback message ...
56
  
79137940   Volodymyr   fix product modal
57
58
  if(counter){
    counterFunc(counter);
ec957aa4   Volodymyr   product modal js
59
60
  }
  
ec957aa4   Volodymyr   product modal js
61
62
63
64
65
66
67
  function phoneHandleInput(e) {
      e.target.value = phoneMasks(e.target.value);
  }
  
  function phoneMasks(phone) {
      return phone.replace(/\D/g, '')
          .replace(/^(\d)/, '($1')
e582a8c8   Volodymyr   feedback message ...
68
          .replace(/^(\(\d{3})(\d)/, '$1)$2')
ec957aa4   Volodymyr   product modal js
69
70
71
72
73
74
          .replace(/(\d{3})(\d{1,7})/, '$1-$2')
          .replace(/(-\d{2})(\d{1,7})/, '$1-$2')
          .replace(/(-\d{2})\d+?$/, '$1');
  }
  
  function counterFunc(counter) {
79137940   Volodymyr   fix product modal
75
76
      var itemPrice = counter.parentElement.querySelector('.product-counter__value');
      itemPrice.innerHTML = `${price} ${itemPrice.dataset.currency}`;
ec957aa4   Volodymyr   product modal js
77
78
79
80
81
82
83
84
85
86
87
88
89
  
      counter.querySelector('input').value = 1;
      counter.addEventListener('input', counterHandleInput);
      counter.addEventListener('change', counterHandleChange);
      counter.querySelector('.product-counter__increment').addEventListener('click', increment);
      counter.querySelector('.product-counter__decrement').addEventListener('click', decrement);
  
      function counterHandleInput(e) {
          e.target.value = e.target.value.replace(/\D/g, '');
          updateSummary();
      }
  
      function counterHandleChange(e) {
79137940   Volodymyr   fix product modal
90
          var value = e.target.value.replace(/\D/g, '');
ec957aa4   Volodymyr   product modal js
91
92
93
94
95
96
97
98
          if (value == 0) {
              e.target.value = 1;
          }
          updateSummary();
      }
  
      function increment(e) {
          e.preventDefault();
79137940   Volodymyr   fix product modal
99
          var value = Number(counter.querySelector('input').value);
ec957aa4   Volodymyr   product modal js
100
          counter.querySelector('input').value = value + 1;
ec957aa4   Volodymyr   product modal js
101
102
103
104
105
          updateSummary();
      }
  
      function decrement(e) {
          e.preventDefault();
79137940   Volodymyr   fix product modal
106
          var value = Number(counter.querySelector('input').value);
ec957aa4   Volodymyr   product modal js
107
108
109
110
111
112
113
          if (value > 1) {
              counter.querySelector('input').value = value - 1;
              updateSummary();
          }
      }
  
      function updateSummary() {
79137940   Volodymyr   fix product modal
114
          var amount = counter.querySelector('input').value;
ec957aa4   Volodymyr   product modal js
115
          if (amount >= 1) {
79137940   Volodymyr   fix product modal
116
117
118
              var oldSum = Number(price);
              var newSum = String(oldSum * amount);
              itemPrice.innerHTML = `${newSum} ${itemPrice.dataset.currency}`;
ec957aa4   Volodymyr   product modal js
119
120
121
          }
      }
  }