Skip to content

Instantly share code, notes, and snippets.

@wanderindev
Created June 29, 2022 03:42
Show Gist options
  • Select an option

  • Save wanderindev/1d70a07f24cb1f8b6db69fc11ec8782f to your computer and use it in GitHub Desktop.

Select an option

Save wanderindev/1d70a07f24cb1f8b6db69fc11ec8782f to your computer and use it in GitHub Desktop.
((app, $, Cleave) => {
// Builds the endpoint from the calculator's name.
app.getEndpoint = calculator => {
return calculator.replace(/ /g, '-').normalize("NFD")
.replace(/[\u0300-\u036f]/g, "").toLowerCase();
};
// Gets the form data.
app.getData = calculator => {
let data = {};
app.calcInfo.forms[calculator].forEach(item => {
if (item.type.endsWith('Radio')) {
data[item.id] = $('input:radio[name="' + item.id + '"]:checked').val();
} else {
let val = $('#' + item.id).val();
if (val){
data[item.id] = val;
}
}
});
app.showExtraPmtInfo = calculator === 'Calculadora de Préstamos' && data.extra_pmt > 0;
return data;
};
// Applies Cleave formatting to results.
app.formatResults = () => {
$('.money').toArray().forEach(el => {
new Cleave(el, {
numeral: true,
numeralThousandsGroupStyle: 'thousand',
//prefix: '$ ',
numeralDecimalScale: 2
});
});
};
. . .
// Gets the results from the backend.
app.calculate = calculator => {
let baseUrl = 'https://api.calcfina.com/';
//let baseUrl = 'http://localhost:5001/';
let endpoint = app.getEndpoint(calculator);
let data = app.getData(calculator);
$.ajax({
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
contentType: 'application/json',
url: baseUrl + endpoint,
data: JSON.stringify(data),
success: results => {
app.results = results;
app.calculator = calculator;
$('.results-wrapper').removeClass('is-invisible');
app.showResults();
// noinspection JSUnresolvedVariable
if (app.calcInfo.charts[app.calculator]) {
$('#charts-tab').removeClass('is-invisible');
app.showCharts();
}
if (app.results.table) {
$('#table-tab').removeClass('is-invisible');
app.showTable();
}
},
dataType: 'json',
error: e => {
console.log(e);
}
});
};
})(window.app = window.app || {}, jQuery, Cleave);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment