Files
zulip/static/js/billing/helpers.js
Anders Kaseorg 28f3dfa284 js: Automatically convert var to let and const in most files.
This commit was originally automatically generated using `tools/lint
--only=eslint --fix`.  It was then modified by tabbott to contain only
changes to a set of files that are unlikely to result in significant
merge conflicts with any open pull request, excluding about 20 files.
His plan is to merge the remaining changes with more precise care,
potentially involving merging parts of conflicting pull requests
before running the `eslint --fix` operation.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-11-03 12:42:39 -08:00

108 lines
3.2 KiB
JavaScript

exports.create_ajax_request = function (url, form_name, stripe_token = null) {
const form = $("#" + form_name + "-form");
const form_loading_indicator = "#" + form_name + "_loading_indicator";
const form_input_section = "#" + form_name + "-input-section";
const form_success = "#" + form_name + "-success";
const form_error = "#" + form_name + "-error";
const form_loading = "#" + form_name + "-loading";
const numeric_inputs = ["licenses"];
loading.make_indicator($(form_loading_indicator),
{text: 'Processing ...', abs_positioned: true});
$(form_input_section).hide();
$(form_error).hide();
$(form_loading).show();
const data = {};
if (stripe_token) {
data.stripe_token = JSON.stringify(stripe_token.id);
}
form.serializeArray().forEach(function (item) {
if (_.contains(numeric_inputs, item.name)) {
data[item.name] = item.value;
} else {
data[item.name] = JSON.stringify(item.value);
}
});
$.post({
url: url,
data: data,
success: function () {
$(form_loading).hide();
$(form_error).hide();
$(form_success).show();
if (_.contains(["autopay", "invoice"], form_name)) {
if ("pushState" in history) {
history.pushState("", document.title, location.pathname + location.search);
} else {
location.hash = "";
}
}
location.reload();
},
error: function (xhr) {
$(form_loading).hide();
$(form_error).show().text(JSON.parse(xhr.responseText).msg);
$(form_input_section).show();
},
});
};
exports.format_money = function (cents) {
// allow for small floating point errors
cents = Math.ceil(cents - 0.001);
let precision;
if (cents % 100 === 0) {
precision = 0;
} else {
precision = 2;
}
// TODO: Add commas for thousands, millions, etc.
return (cents / 100).toFixed(precision);
};
exports.update_charged_amount = function (prices, schedule) {
$("#charged_amount").text(
exports.format_money(page_params.seat_count * prices[schedule])
);
};
exports.show_license_section = function (license) {
$("#license-automatic-section").hide();
$("#license-manual-section").hide();
$("#automatic_license_count").prop('disabled', true);
$("#manual_license_count").prop('disabled', true);
const section_id = "#license-" + license + "-section";
$(section_id).show();
const input_id = "#" + license + "_license_count";
$(input_id).prop("disabled", false);
};
exports.set_tab = function (page) {
const hash = location.hash;
if (hash) {
$('#' + page + '-tabs.nav a[href="' + hash + '"]').tab('show');
$('html').scrollTop(0);
}
$('#' + page + '-tabs.nav-tabs a').on("click", function () {
location.hash = this.hash;
});
window.onhashchange = function () {
$('#' + page + '-tabs.nav a[href="' + location.hash + '"]').tab('show');
$('html').scrollTop(0);
};
};
exports.is_valid_input = function (elem) {
return elem[0].checkValidity();
};
window.helpers = exports;