lmc/account/contact/contact.js

144 lines
4.1 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, apiPut, parseParams, setupHeader } from "/global.js";
import { countries } from "/account/contact/countries.js";
(function()
{
// Element names specific to this page
elements.address1 = "address1";
elements.address2 = "address2";
elements.city = "city";
elements.company = "company";
elements.country = "country";
elements.email = "email";
elements.firstName = "first-name";
elements.lastName = "last-name";
elements.phone = "phone";
elements.saveButton = "save-button";
elements.state = "state";
elements.tax = "tax";
elements.zip = "zip";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.address1 = {};
ui.address2 = {};
ui.city = {};
ui.company = {};
ui.country = {};
ui.email = {};
ui.firstName = {};
ui.lastName = {};
ui.phone = {};
ui.saveButton = {};
ui.state = {};
ui.tax = {};
ui.zip = {};
// Callback for account details API call
var displayAccount = function(response)
{
ui.company.value = response.company;
ui.email.value = response.email;
ui.firstName.value = response.first_name;
ui.lastName.value = response.last_name;
ui.address1.value = response.address_1;
ui.address2.value = response.address_2;
ui.city.value = response.city;
ui.state.value = response.state;
ui.zip.value = response.zip;
ui.country.value = response.country;
ui.tax.value = response.tax_id;
ui.phone.value = response.phone;
ui.saveButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"company": ui.company.value,
"email": ui.email.value,
"first_name": ui.firstName.value,
"last_name": ui.lastName.value,
"address_1": ui.address1.value,
"address_2": ui.address2.value,
"city": ui.city.value,
"state": ui.state.value,
"zip": ui.zip.value,
"country": ui.country.value,
"tax": ui.tax.value,
"phone": ui.phone.value
};
apiPut("/account", req, function(response)
{
location.href = "/account";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.address1 = document.getElementById(elements.address1);
ui.address2 = document.getElementById(elements.address2);
ui.city = document.getElementById(elements.city);
ui.company = document.getElementById(elements.company);
ui.country = document.getElementById(elements.country);
ui.email = document.getElementById(elements.email);
ui.firstName = document.getElementById(elements.firstName);
ui.lastName = document.getElementById(elements.lastName);
ui.phone = document.getElementById(elements.phone);
ui.saveButton = document.getElementById(elements.saveButton);
ui.state = document.getElementById(elements.state);
ui.tax = document.getElementById(elements.tax);
ui.zip = document.getElementById(elements.zip);
// Populate country selector
for (var i = 0; i < countries.length; i++) {
var option = document.createElement("option");
option.value = countries[i].code;
option.innerHTML = countries[i]["name"];
ui.country.appendChild(option);
}
// Register event handlers
ui.saveButton.addEventListener("click", handleSave);
// Get data from API
apiGet("/account", displayAccount, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();