forked from Superbalist/spreeza-magento_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
321 lines (270 loc) · 9.55 KB
/
app.js
File metadata and controls
321 lines (270 loc) · 9.55 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
(function () {
'use_strict';
return {
// Properties
defaultState: 'loading',
magentoApiEndpoint: '',
resources: {
PROFILE_URI: '%@/zendesk/api/customers/%@/%@/%@',
ORDER_URI: '%@/zendesk/api/orders/%@'
},
requests: {
'getProfile': function (email, numRecentOrders, fieldOrderId) {
return this._getRequest(helpers.fmt(this.resources.PROFILE_URI, this.magentoApiEndpoint, email, numRecentOrders, fieldOrderId));
},
'getOrder': function (orderId) {
return this._getRequest(helpers.fmt(this.resources.ORDER_URI, this.magentoApiEndpoint, orderId));
},
'userInfo': {
url: '/api/v2/users/me.json'
}
},
events: {
'app.created' : 'init',
'*.changed' : 'handleChanged',
'getProfile.done' : 'handleProfile',
'getProfile.fail' : 'handleProfileFail',
'getOrder.done' : 'handleOrder',
'getOrder.fail' : 'handleFail',
'click .toggle-div' : 'toggleDiv',
'click .change-order' : 'handleOrderChanged',
'click .change-rma' : 'handleRmaChanged',
'userInfo.done' : 'onUserInfoDone'
},
onUserInfoDone: function (data) {
this.locale = data.user.locale;
},
localizeDate: function (date, params) {
if (!date) {
return date;
}
var dateObj = new Date(date);
// special fix for safari which does not know about ISO
if (dateObj.toString() == 'Invalid Date') {
var parts = date.split(' ');
var els = parts[0].split('-').concat(parts[1].split(':'));
dateObj = new Date(els[0], els[1] - 1, els[2], els[3], els[4], els[5]);
}
var options = _.extend({
year: "numeric",
month: "numeric",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
}, params || {});
return dateObj.toLocaleDateString(this.locale, options);
},
handleChanged: _.debounce(function (e) {
if (e.propertyName === helpers.fmt("ticket.custom_field_%@", this.settings.order_id_field_id)) {
this.orderId = e.newValue;
if (this.profileData) {
this._appendTicketOrder();
} else {
this.queryOrder();
}
} else if (e.propertyName === "ticket.requester.id") {
this.queryCustomer();
}
}, 500),
handleOrderChanged: function (e) {
var me = this.$(e.target);
this.orderId = String(me.data("value"));
if (this.profileData) {
this._appendTicketOrder();
this.highlightSelectedTr(me);
}
return false;
},
handleRmaChanged: function (e) {
var me = this.$(e.target);
this.rmaId = String(me.data("value"));
if (this.profileData.ticketOrder.productReturns) {
this._appendTicketOrderRma();
this.highlightSelectedTr(me);
}
return false;
},
highlightSelectedTr: function(target) {
target.closest("table").find("tr").removeClass("active");
target.closest("tr").addClass("active");
},
handleProfile: function (data) {
var ordersLength = 0;
// Check that the response was successful
if (_.has(data, 'success') && data.success === false) {
// Allow failures if there's an order to be fetched
if (_.isEmpty(this.orderId) === false) {
this.queryOrder();
} else {
this.showError(this.I18n.t('global.error.title'), data.message);
}
return;
}
// We'll do a little transformation on the data and store locally.
this.profileData = data;
this.profileData.settings = this.settings;
this.profileData.addresses = this._cleanupLineBreaks(this.profileData.addresses);
// See if we should show all orders or only recent orders.
/*
ordersLength = this.profileData.orders.length;
if (ordersLength > 3) {
this.profileData.recentOrders = this.profileData.orders.slice(ordersLength - 3, ordersLength).reverse();
} else {
this.profileData.recentOrders = this.profileData.orders.reverse();
}*/
this.profileData.recentOrders = this.profileData.orders;
if (_.isEmpty(this.orderId) === true) {
this.orderId = this.profileData.recentOrders[0].id;
}
// Got the profile data, populate interface
//this.profileData.created = this.localizeDate(this.profileData.created);
this.switchTo('profile', this.profileData);
this._appendTicketOrder();
},
handleOrder: function (data) {
// Check that the response was successfuly
if (_.isEmpty(data.id)) {
this.showError(this.I18n.t('global.error.title'), data.message || this.I18n.t('order.error.message'));
return;
}
this.switchTo('order', {order: data});
},
handleFail: function () {
this.showError(this.I18n.t('global.error.title'), this.I18n.t('global.error.server'));
},
handleProfileFail: function (resp) {
if (resp.status === 404) {
// Allow failures if there's an order to be fetched
if (_.isEmpty(this.orderId) === false) {
this.queryOrder();
} else {
this.showError(this.I18n.t('global.error.title'), this.I18n.t('global.error.noprofile'));
}
} else {
this.handleFail();
}
},
init: function (data) {
this.ajax('userInfo').done(function () {
this.magentoApiEndpoint = this._checkMagentoApiEndpoint(this.settings.url);
// Get order id field
if (this.settings.order_id_field_id) {
this.orderId = this.ticket().customField('custom_field_' + this.settings.order_id_field_id);
}
if (this.currentLocation() === 'ticket_sidebar') {
this.queryCustomer();
}
}.bind(this));
},
queryCustomer: function () {
this.switchTo('requesting');
this.ajax('getProfile', this.ticket().requester().email(), this.settings.number_of_recent_orders, this.orderId);
},
queryOrder: function () {
this.switchTo('requesting');
this.ajax('getOrder', this.orderId);
},
showError: function (title, msg) {
this.switchTo('error', {
title: title || this.I18n.t('global.error.title'),
message: msg || this.I18n.t('global.error.message')
});
},
toggleDiv: function (e) {
var me = this.$(e.target);
var i = me.find('i');
me.parent().next('div').toggleClass('hide');
if (i.attr('class') == 'icon-chevron-down') {
i.removeClass('icon-chevron-down');
i.addClass('icon-chevron-up');
} else {
i.removeClass('icon-chevron-up');
i.addClass('icon-chevron-down');
}
return false;
},
// Helpers
_checkMagentoApiEndpoint: function (url) {
// First, lets make sure there is no trailing slash, we'll add one later.
if (url.slice(-1) === '/') {
url = url.slice(0, -1);
}
// Test whether we have a front-controller reference here.
if (url.indexOf('index.php') === -1) {
return url + '/index.php';
}
return url;
},
// Format the line breaks for web
_cleanupLineBreaks: function (toBeCleaned) {
var cleaned = toBeCleaned;
_.each(cleaned, function (value, key) {
cleaned[key] = _.escape(value).replace(/(\n)+/g, '<br>');
});
return cleaned;
},
_getRequest: function (resource) {
return {
headers: {
'Authorization': 'Token token="' + this.settings.access_token + '"'
},
url: resource,
method: 'GET',
dataType: 'json'
};
},
_appendTicketOrder: function () {
var orderId = this.orderId,
orderTemplate = "";
// If there is an order ID custom field setup, look to see if the order ID exists in the profile data
if (orderId) {
orderTemplate += "<hr />";
this.profileData.ticketOrder = _.find(this.profileData.orders, function (order) {
return (order.id === orderId);
});
if (this.profileData.ticketOrder) {
this.profileData.ticketOrder.store = this.profileData.ticketOrder.store.replace(/\n/g, '<br>');
//this.profileData.ticketOrder.created = this.localizeDate(this.profileData.ticketOrder.created);
orderTemplate += this.renderTemplate('order', {
order: this.profileData.ticketOrder
});
} else {
orderTemplate += this.renderTemplate('error', {
title: this.I18n.t('global.error.title'),
message: this.I18n.t('order.error.message')
});
}
}
this.$('.order').html(orderTemplate);
if (this.profileData.ticketOrder) {
this.highlightSelectedTr(this.$('#recent-order-' + this.orderId));
}
},
_appendTicketOrderRma: function () {
var rmaId = this.rmaId,
selectedRma = null,
rmaTemplate = "";
if (rmaId) {
rmaTemplate += "<hr />";
selectedRma = _.find(this.profileData.ticketOrder.productReturns, function (rma) {
return (rma.rma_id === rmaId);
});
if (selectedRma) {
rmaTemplate += this.renderTemplate('rma', {
rma: selectedRma
});
} else {
rmaTemplate += this.renderTemplate('error', {
title: this.I18n.t('global.error.title'),
message: this.I18n.t('order.rma.error.message')
});
}
}
this.$('.rma').html(rmaTemplate);
if (selectedRma) {
this.highlightSelectedTr(this.$('#rma-' + this.rmaId));
}
}
};
}());