Skip to content

Commit

Permalink
se agrega soporte a localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagogak committed Dec 4, 2024
1 parent c9d629e commit 3c91efa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
17 changes: 15 additions & 2 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ h1 {
padding: 15px 0;
}

.submit {
background-color: greenyellow;
.submit, .clear, .print {
margin: 25px auto;
width: 200px;
font-weight: 300;
Expand All @@ -37,6 +36,20 @@ h1 {
border-radius: 30px;
}

.submit {
background-color: greenyellow;
}

.print {
background-color: blue;
color: white;
}

.clear {
background-color: red;
color: white;
}

#resultados {
display: block;
width: 75%;
Expand Down
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ <h1>Calculadora de Cuotas de Crédito</h1>
</div>
<button class="submit" type="submit">Simular Crédito</button>
</form>
<button class="print" id="print">Imprimir Historial</button>
<button class="clear" id="clear">Borrar Historial</button>
<div id="resultados"></div>
</main>
<script type="module" src="js/calcuotas.js" defer></script>
Expand Down
55 changes: 36 additions & 19 deletions js/calcuotas.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@

import RegistroCalculo from './registrocalculo.js';
const calcButton = document.getElementById("calcular");
const clearButton = document.getElementById("clear");
const printButton = document.getElementById("print");

//variable para guardar el historial de cálculos
let historial = [];
const crearTablaRegistros = () => {

const crearTablaRegistros = (arrH) => {
const historial = window.localStorage;
console.log(historial);

const resultados = document.getElementById('resultados');
resultados.innerHTML = '';
arrH.forEach((h,idx) => {
for (let i = 0; i < localStorage.length; i++) {
const idx = localStorage.key(i);
const entry = JSON.parse(localStorage.getItem(idx));
const h = Object.assign(new RegistroCalculo, entry);
console.log(h);
const creditoHTML = `
<div class="registro">
<h2>Registro ${idx+1}:</h2>
<div class="infoLeft">
<p>Monto: ${h.formatoMoneda(h.monto)}</p>
<p>Tasa: ${h.tasa}%</p>
<p>Cuotas: ${h.cuotas}</p>
</div>
<div class="infoRight">
<p>Pago Mensual: ${h.formatoMoneda(h.pagoMensual)}</p>
<p>Total Interéses: ${h.imprimirTotalInteres()}</p>
<p>Cuotas:<br> ${h.imprimirDetallePagos(true)}</p>
</div>
<h2>Registro ${parseInt(idx)}:</h2>
<div class="infoLeft">
<p>Monto: ${h.formatoMoneda(h.monto)}</p>
<p>Tasa: ${h.tasa}%</p>
<p>Cuotas: ${h.cuotas}</p>
</div>
<div class="infoRight">
<p>Pago Mensual: ${h.formatoMoneda(h.pagoMensual)}</p>
<p>Total Interéses: ${h.imprimirTotalInteres()}</p>
<p>Cuotas:<br> ${h.imprimirDetallePagos(true)}</p>
</div>
</div>
`;
resultados.insertAdjacentHTML( 'beforeend', creditoHTML );
});
}
}

calcButton.addEventListener('submit', (e) => {
Expand All @@ -44,7 +51,17 @@ calcButton.addEventListener('submit', (e) => {

const credito = new RegistroCalculo(capital, interes, cuotas);
credito.calcularTodo();
historial.push(credito);
console.log(historial);
crearTablaRegistros(historial);
localStorage.setItem(localStorage.length,JSON.stringify(credito));
crearTablaRegistros();
});

clearButton.addEventListener('click', (e) => {
e.preventDefault();
localStorage.clear();
crearTablaRegistros();
});

printButton.addEventListener('click', (e) => {
e.preventDefault();
crearTablaRegistros();
});
6 changes: 3 additions & 3 deletions js/registrocalculo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default class RegistroCalculo {
constructor(monto = 0, tasa = 0, cuotas = 1) {
this.monto = monto;
this.tasa = tasa;
this.cuotas = cuotas;
this.monto = parseInt(monto);
this.tasa = parseInt(tasa);
this.cuotas = parseInt(cuotas);
this.pagoMensual = 0;
this.pagos = [];
}
Expand Down

0 comments on commit 3c91efa

Please sign in to comment.