forked from wevote/WebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanDialog.js
More file actions
347 lines (308 loc) · 10.6 KB
/
PlanDialog.js
File metadata and controls
347 lines (308 loc) · 10.6 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// WeVoteServer/apis_v1/static/js/planDialog.js
$(function () {
let dialog;
let form;
let isMonthly = true;
const idfld = $('#id');
const couponCode = $('#coupon_code');
const premiumPlanTypeEnum = $('#premium_plan_type_enum');
const hiddenPlanComment = $('#hidden_plan_comment');
const couponAppliedMessage = $('#coupon_applied_message');
const monthlyPriceStripe = $('#monthly_price_stripe');
const annualizedMonthly = $('#annualized_monthly'); // Do not validate this
const annualPriceStripe = $('#annual_price_stripe');
const redemptions = $('#redemptions');
const featuresProvidedBitmap = $('#features_provided_bitmap');
const planCreatedAt = $('#plan_created_at');
const couponExpiresDate = $('#coupon_expires_date');
const allFields = $([]).add(idfld).add(couponCode).add(planTypeEnum).add(hiddenPlanComment).add(couponAppliedMessage)
.add(monthlyPriceStripe).add(annualPriceStripe).add(redemptions).add(featuresProvidedBitmap).add(planCreatedAt).add(couponExpiresDate);
const tips = $('.validateTips');
const spinner = $('#html5Spinner');
$('.base-expire').each(function() {
$(this).html($(this).html().substring(0, 10));
});
setup();
dialog = $('#dialog-form').dialog({
autoOpen: false,
height: 800,
width: 900,
modal: true,
buttons: {
// "Run this task now": notYet,
'Save Plan': saveNewPlan,
'Save as a new Plan': saveNewPlan,
Delete: deletePlan,
Cancel () {
dialog.dialog('close');
},
},
close () {
form[0].reset();
allFields.removeClass('ui-state-error');
},
});
form = dialog.find('form').on('submit', (event) => {
event.preventDefault();
addUser();
});
// Functions ---------------
function setup () {
console.log("SETUP");
idfld.css('width', '70').css('background', 'ghostwhite');
couponCode.css('width', '300');
hiddenPlanComment.css('width', '100%');
couponAppliedMessage.css('width', '100%');
couponExpiresDate.datepicker();
isMonthly = true;
monthlyPriceStripe.css({'width': '100', 'text-align': 'right', 'background': 'white'}).attr('disabled', false);
annualPriceStripe.css({'width': '100', 'text-align': 'right', 'background': 'ghostwhite'}).attr('disabled', true);
annualizedMonthly.css({
'width': '100',
'text-align': 'right',
'background': 'ghostwhite',
'margin-left': '22px'
}).attr('disabled', true);
planCreatedAt.css('background', 'ghostwhite');
redemptions.css('background', 'ghostwhite');
}
function updateTips (t) {
tips
.text(t)
.addClass('ui-state-highlight');
setTimeout(() => {
tips.removeClass('ui-state-highlight', 1500);
}, 500);
}
function checkLength (o, n, min, max) {
if (o.val().length > max || o.val().length < min) {
o.addClass('ui-state-error');
updateTips(`Length of ${n} must be between ${min} and ${max}.`);
return false;
} else {
return true;
}
}
function checkNumber (o, n) {
if (Number.isNaN(o.val())) {
o.addClass('ui-state-error');
updateTips(`${n} must be a number.`);
return false;
} else {
return true;
}
}
function checkNumberNonZero (o, n) {
if (Number.isNaN(o.val()) || o.val() < 1) {
o.addClass('ui-state-error');
updateTips(`${n} must be a number.`);
return false;
} else {
return true;
}
}
function checkRegexp (o, regexp, n) {
if (!(regexp.test(o.val()))) {
o.addClass('ui-state-error');
updateTips(n);
return false;
} else {
return true;
}
}
function dollarsToCents (cost) {
costString = cost + ""; // convert to a string
if (costString.length === 0) {
return '';
} else if (costString.indexOf('.') >= 0) {
const money = (costString).split(".");
dollars = money[0];
cents = money[1];
if (cents.length > 2) {
return dollars + cents.substring(0, 2);
} else if (cents.length === 0) {
return dollars + '00';
} else if (cents.length === 1) {
return dollars + cents + '0'
} else if (cents.length === 2) {
return dollars + cents;
}
}
return cost + '00';
}
function saveNewPlan () {
let valid = true;
allFields.removeClass('ui-state-error');
expires = "";
valid = valid && checkLength(couponCode, 'Coupon Code', 3, 128);
if (isMonthly) {
valid = valid && checkNumberNonZero(monthlyPriceStripe, 'Monthly Price');
} else {
valid = valid && checkNumberNonZero(annualPriceStripe, 'Annual Price');
}
if (couponExpiresDate.val().length) {
valid = valid && checkRegexp(couponExpiresDate, /^([0-9]{2}\/[0-9]{2}\/[0-9]{4})+$/, 'Expiration date must use this format: 12/25/2024');
if (valid) {
moment().tz('America/Los_Angeles').format();
expires = moment(couponExpiresDate.val(), 'MM/DD/YYYY HH:mm').format();
}
}
if (valid) {
const newPlanData = {
couponCode: couponCode.val(),
premiumPlanTypeEnum: premiumPlanTypeEnum.val(),
couponAppliedMessage: couponAppliedMessage.val(),
hiddenPlanComment: hiddenPlanComment.val(),
monthlyPriceStripe: dollarsToCents(monthlyPriceStripe.val()),
annualPriceStripe: dollarsToCents(annualPriceStripe.val()),
featuresProvidedBitmap: featuresProvidedBitmap.val(),
couponExpiresDate: expires,
}
const apiURL = `${window.location.origin}/apis/v1/createNewPlan`;
$.getJSON(apiURL, newPlanData, () => { dialog.dialog('close'); location.reload(); })
.fail((err) => {
console.log('error', err);
});
}
return valid;
}
// https://jqueryui.com/dialog/#modal-form
// Called to populate the dialog with existing values
function setInitialValues () {
idfld.val(activePlan.id);
couponCode.val(activePlan.coupon_code);
premiumPlanTypeEnum.val(activePlan.premium_plan_type_enum).change();
hiddenPlanComment.val(activePlan.hidden_plan_comment);
couponAppliedMessage.val(activePlan.coupon_applied_message);
if (isMonthly) {
monthlyPriceStripe.val(activePlan.monthly_price_stripe);
} else {
annualPriceStripe.val(activePlan.annual_price_stripe);
}
redemptions.val(activePlan.redemptions);
featuresProvidedBitmap.val(activePlan.features_provided_bitmap);
planCreatedAt.val(activePlan.plan_created_at);
}
function deleteThisPlan () {
const apiURL = `${window.location.origin}/apis/v1/deletePlan`;
const deletePlanData = {
id: idfld.val(),
};
$.getJSON(apiURL, deletePlanData, () => { dialog.dialog('close'); location.reload(); })
.fail((err) => {
console.log('error deleteThisPlan ', err);
});
}
function deletePlan () {
$("<div title='Obliviate'>This instance of the plan will be permanently deleted and cannot be recovered. Are you sure?</div>")
.dialog({
resizable: false,
height: 'auto',
width: 400,
modal: true,
buttons: {
Delete () {
deleteThisPlan();
$(this).dialog('close');
},
Cancel () {
$(this).dialog('close');
},
},
});
}
// Called to populate the dialog with new values
function setNewValues () {
planCreatedAt.val(moment().format("MM/DD/YYYY")).css('background', 'ghostwhite').attr('readonly', true);
}
function notYet () {
$("<div title='Not yet implemented'>Try back next month</div>").dialog();
return false;
}
function getLimitFromUrl () {
const newUrl = window.location.href;
if (newUrl.includes('?limit=')) {
return newUrl.slice(newUrl.lastIndexOf('=') + 1);
} else {
return 0;
}
}
// onClick Handlers
$('#pl').on('click', (event) => {
console.log("You clicked on a numbered id button: ", activePlan.id);
$('label[for=id]').text('Settings copied from id #');
$('label[for=redemptiosn]').text('Redemptions of source coupon');
$('button:contains("Save Plan")').hide();
$('button:contains("Delete")').show();
$('button:contains("Save task")').hide();
setInitialValues();
dialog.dialog('open');
});
$('#create-coupon').button().on('click', () => {
console.log("clicked on create a coupon");
// $('label[for=idfld]').hide();
// idfld.hide();
$('button:contains("Save Plan")').hide();
$('button:contains("Delete")').hide();
$('label[for=last_error], input#last_error').hide();
$('button:contains("Save as a new Plan")').show();
setNewValues();
dialog.dialog('open');
});
$('#rollupOlderVersions').change(() => {
let checked = document.getElementById('rollupOlderVersions').checked;
console.log("clicked on rollupOlderVersions: " + checked);
let table = document.getElementById('pl');
let rowLength = table.rows.length;
let hashmap = {};
// This assumes that the rows arrive in order of most recently created
for(let i=1; i<rowLength; i+=1) {
var row = table.rows[i];
let coupon = row.cells[1].innerText;
let type = row.cells[2].innerText;
let key = coupon + "~" + type;
if (checked) {
if (key in hashmap) {
$(row).css("display", "none");
} else {
hashmap[key] = true;
}
console.log("Row dump: " + coupon + "~" + type);
} else {
$(row).css("display", "");
}
}
});
$('#rollupOlderVersions').click();
$('#update').button().on('click', () => {
event.preventDefault();
const limit = spinner.val();
let newUrl = window.location.href;
if (getLimitFromUrl()) {
newUrl = newUrl.slice(0, newUrl.lastIndexOf('=') + 1) + limit;
} else {
newUrl += `?limit=${limit}`;
}
window.location.href = newUrl;
});
$('#premium_plan_type_enum').on('change', function (e) {
event.preventDefault();
const optionSelected = $("option:selected", this);
const valueSelected = this.value;
if (valueSelected.includes('_MONTHLY')) {
isMonthly = true;
monthlyPriceStripe.css('background', 'white').attr('disabled', false);
annualPriceStripe.css('background', 'ghostwhite').attr('disabled', true).val('');
} else {
isMonthly = false;
monthlyPriceStripe.css('background', 'ghostwhite').attr('disabled', true).val('');
annualPriceStripe.css('background', 'white').attr('disabled', false);
annualizedMonthly.val('');
}
});
monthlyPriceStripe.on("change paste keyup", () => {
const value = monthlyPriceStripe.val();
const annualized = Number(value.replace(/[^0-9\.]+/g,"")) * 12;
annualizedMonthly.val(annualized);
});
});