lmc/account/creditcard/creditcard.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-03-13 23:01:39 -04:00
/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.ccCurrent = "cc-current";
elements.ccNew = "cc-new";
elements.expiryCurrent = "expiry-current";
elements.expiryMonth = "expiry-month";
elements.expiryYear = "expiry-year";
elements.updateButton = "update-button";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.ccCurrent = {};
ui.ccNew = {};
ui.expiryCurrent = {};
ui.expiryMonth = {};
ui.expiryYear = {};
ui.updateButton = {};
// Callback for account details API call
var displayCC = function(response)
{
if (!response.credit_card)
return;
ui.ccCurrent.innerHTML = response.credit_card.last_four;
ui.expiryCurrent.innerHTML = response.credit_card.expiry;
};
// Click handler for update button
var handleUpdate = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"card_number": ui.ccNew.value,
"expiry_month": parseInt(ui.expiryMonth.value),
"expiry_year": parseInt(ui.expiryYear.value)
};
apiPost("/account/credit-card", req, function(response)
{
location.href = "/account";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.ccCurrent = document.getElementById(elements.ccCurrent);
ui.ccNew = document.getElementById(elements.ccNew);
ui.expiryCurrent = document.getElementById(elements.expiryCurrent);
ui.expiryMonth = document.getElementById(elements.expiryMonth);
ui.expiryYear = document.getElementById(elements.expiryYear);
ui.updateButton = document.getElementById(elements.updateButton);
// Populate expiry selectors
var now = new Date();
ui.expiryMonth.value = now.getMonth() + 1;
for (var i = 0; i < 25; i++) {
var year = document.createElement("option");
year.value = now.getFullYear() + i;
year.innerHTML = now.getFullYear() + i;
ui.expiryYear.appendChild(year);
}
// Register event handlers
ui.updateButton.addEventListener("click", handleUpdate);
// Get data from API
apiGet("/account", displayCC, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();