-
Notifications
You must be signed in to change notification settings - Fork 5
/
product.es6
221 lines (190 loc) · 5.85 KB
/
product.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
(() => {
const DOM = {
$imageBig: $('#product-image-big'),
$imagesToSwitch: $('.js-image-switch'),
$fancybox: $('.fancybox'),
$addToCart: $('#btn-to-basket'),
$phone: $('#input-one-click-phone'),
$oneClick: $('#btn-one-click-order'),
$counter: $('#product-count'),
$more_text_toggle: $('.js-more-text'),
$h1: $('.product-h1'),
// Feedback DOM elements
field: '.js-modal-field',
filledRatingIcon: 'rating-icon-full',
emptyRatingIcon: 'rating-icon-empty',
$closeModalBtn: $('.js-modal-close'),
$feedbackBtn: $('.js-send-feedback'),
$feedbackDelete: $('.js-feedback-delete'),
$feedbackList: $('#feedback-list'),
$feedbackModal: $('#product-feedback-modal'),
$feedbackNameField: $('#feedback-modal-name'),
$ratingFilter: $('.js-rating-filter'),
$ratingList: $('.js-rating'),
$successModalText: $('.js-feedback-success'),
};
const productId = DOM.$addToCart.attr('data-id');
const init = () => {
setUpListeners();
changeOneClickBtnState();
publishDetail();
};
function setUpListeners() {
// Feedback events
mediator.subscribe('onFeedbackSave', feedbackSavedResponse);
mediator.subscribe('onFeedbackDelete', feedbackDeleteResponse);
mediator.subscribe('onRate', changeRateIcons, setBtnActiveState);
DOM.$imageBig.click(fancyBoxStart);
DOM.$imagesToSwitch.click(productImgSwitch);
DOM.$addToCart.click(buyProduct);
DOM.$oneClick.click(oneClick);
DOM.$feedbackBtn.click(sendFeedback);
DOM.$feedbackDelete.click(deleteFeedback);
DOM.$phone.keyup(changeOneClickBtnState);
DOM.$ratingList.on('click', 'li', event => mediator.publish('onRate', event));
DOM.$ratingFilter.on('click', '.js-filter-trigger', filterByRating);
DOM.$more_text_toggle.on('click', helpers.toggleText);
}
function getProductData() {
return {
id: productId,
name: DOM.$addToCart.data('name'),
category: DOM.$addToCart.data('category'),
quantity: parseInt(DOM.$counter.val(), 10),
brand: DOM.$addToCart.data('brand'),
};
}
/**
* Publish onProductDetail event.
*/
function publishDetail() {
const data = getProductData();
const { id } = data;
if (id) mediator.publish('onProductDetail', [data]);
}
/**
* Initialize fancyBox on specific index image.
*/
function fancyBoxStart() {
$.fancybox(
DOM.$fancybox,
{
index: DOM.$imageBig.attr('data-index'),
helpers: {
overlay: {
locked: false,
},
},
},
);
return false; // this return is required
}
/**
* Buy a product by an one click and redirect to the success page.
*/
function oneClick() {
helpers.setDisabledState(DOM.$oneClick, 'Ожидайте...');
const { id, quantity } = getProductData();
server.oneClickBuy(id, quantity, DOM.$phone.val())
.then(() => {
mediator.publish('onOneClickBuy');
// Set timeout to wait handling of onOneClickBuy
setTimeout(() => {
window.location.href = configs.hrefs.orderSuccess;
}, 100);
});
}
function changeOneClickBtnState() {
if (DOM.$oneClick.size() > 0) {
DOM.$oneClick.attr('disabled', !helpers.isPhoneValid(DOM.$phone.val()));
}
}
/**
* Switch product images.
*
* @param event - click on image preview;
*/
function productImgSwitch(event) {
const targetSrc = $(event.target).attr('src');
const dataIndex = $(event.target).attr('data-index');
if (targetSrc !== DOM.$imageBig.attr('src')) {
DOM.$imageBig.attr({
src: targetSrc,
'data-index': dataIndex,
});
}
}
function buyProduct() {
const data = getProductData();
const { id, quantity } = data;
server.addToCart(id, quantity)
.then((newData) => {
mediator.publish('onCartUpdate', newData);
mediator.publish('onProductAdd', [data]);
});
}
/**
* Send user feedback about Product to backend.
*/
function sendFeedback() {
const feedback = getFeedbackData();
server.sendFeedback(feedback)
.then(() => mediator.publish('onFeedbackSave'));
}
/**
* Send Feedback id on server to delete it from DB.
*/
function deleteFeedback() {
const $element = $(this);
const id = $element.data('id');
server.deleteFeedback(id)
.then(() => mediator.publish('onFeedbackDelete', $element));
}
function getFeedbackData() {
const getFormFieldValue = item => DOM.$feedbackModal.find(`${DOM.field}[name="${item}"]`).val();
const feedback = {
id: productId,
rating: DOM.$ratingList.find(`.${DOM.filledRatingIcon}`).size(),
};
const fields = ['name', 'dignities', 'limitations', 'general'];
fields.forEach((item) => {
feedback[item] = getFormFieldValue(item);
});
return feedback;
}
function changeRateIcons(_, event) {
$(event.target)
// change class for target
.removeClass(DOM.emptyRatingIcon)
.addClass(DOM.filledRatingIcon)
// change class for previous siblings of target
.prevAll()
.removeClass(DOM.emptyRatingIcon)
.addClass(DOM.filledRatingIcon)
// change class for next siblings of target
.end()
.nextAll()
.removeClass(DOM.filledRatingIcon)
.addClass(DOM.emptyRatingIcon);
}
function setBtnActiveState() {
DOM.$feedbackBtn.prop('disabled', false);
}
function feedbackSavedResponse() {
DOM.$feedbackBtn.addClass('hidden');
DOM.$closeModalBtn.removeClass('hidden');
DOM.$successModalText
.removeClass('hidden')
.siblings().addClass('hidden');
}
function feedbackDeleteResponse(_, element) {
$(element).parent().fadeOut('fast');
}
function filterByRating() {
const rating = $(this).data('rating');
DOM.$feedbackList
.find(`div[data-rating="${rating}"]`)
.fadeToggle('fast');
}
init();
})();