-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.js
202 lines (179 loc) · 8.38 KB
/
UI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
let loanContainer = document.querySelector('.loan-container')
let addEntryButton = document.getElementById('add-entry')
let updateLoansBtn = document.getElementById('update-loans')
const loanData = JSON.parse(localStorage.getItem("LPMdata")) || [];
let currentData = {}
//Initial confirmation, populates data to local storage for future use
function confirmLoan(buttonEl){
const target = buttonEl.parentElement.id;
const loanName = document.getElementById(`name-${target}`).value;
const balance = document.getElementById(`balance-${target}`).value;
const min_Payment = document.getElementById(`minPayment-${target}`).value;
const rate = document.getElementById(`rate-${target}`).value;
const dataArrIndex = loanData.findIndex((item)=>item.id === target)
let El = document.getElementById('add-entry')
El.disabled = false;
El.style.cursor= "pointer"
//create the data object
const dataObj = {
id:target,
loanName: loanName || target.split('-')[1],
balance:balance || 0,
rate:rate || 0,
minPayment: min_Payment || 0,
daysElapsed: null
}
//save the data object
if(dataArrIndex === -1){
loanData.push(dataObj)
}else{
loanData[dataArrIndex] = dataObj
}
localStorage.setItem("LPMdata", JSON.stringify(loanData))
//Initial HTML to populate the container with
let container = document.getElementById(target)
container.outerHTML = `
<div class="loan" id="${dataObj.id}">
<div class="dropdown">
<button class="arrow" onclick=dropDown(this)></button>
<ul class="dropdown-content">
<li><button class="edit-btn menu-item" onclick="editEntry(this)">Edit</button></li>
<li><button class="delete-btn menu-item" onclick="deleteEntry(this)">Delete</button></li>
</ul>
</div>
<span><strong>Loan Name:</strong></span>
<p>${dataObj.loanName}</p>
<span><strong>Balance:</strong></span>
<p>$${dataObj.balance}</p>
<span><strong>Interest Rate: </strong></span>
<p>${dataObj.rate}%</p>
<span><strong>Minimum Payment: </strong></span>
<p>$${dataObj.minPayment}</p>
<span class="enterDate">
<span class ="tooltip">Use a different payment date for this loan</span>
<strong>Separate Date:</strong>
</span>
<label for="toggle-${dataObj.id.split("-")[1]}" class="toggle-container">
<input type="checkbox" id ="toggle-${dataObj.id.split("-")[1]}" onclick="toggleModal(this)">
<span class="slider"></span>
</label>
<form id="modal-${dataObj.id.split("-")[1]}" class="separateDate">
<input type="date" required>
</form>
</div>`
}
//Display user input as inline text
function updateLoanContainer(){
loanContainer.innerHTML="";
loanData.forEach(
({id, loanName, balance, rate, minPayment}) => {
loanContainer.innerHTML += `
<div class="loan" id="${id}">
<div class="dropdown">
<button class="arrow" onclick=dropDown(this)></button>
<ul class="dropdown-content">
<li><button class="edit-btn menu-item" onclick="editEntry(this)">Edit</button></li>
<li><button class="delete-btn menu-item" onclick="deleteEntry(this)">Delete</button></li>
</ul>
</div>
<span><strong>Loan Name:</strong></span>
<p>${loanName}</p>
<span><strong>Balance:</strong></span>
<p>$${balance}</p>
<span><strong>Interest Rate: </strong></span>
<p>${rate}%</p>
<span><strong>Minimum Payment: </strong></span>
<p>$${minPayment}</p>
<span class="enterDate">
<span class ="tooltip">Use a different payment date for this loan</span>
<strong>Separate Date:</strong>
</span>
<label for="toggle-${id.split("-")[1]}" class="toggle-container">
<input type="checkbox" id ="toggle-${id.split("-")[1]}" onclick="toggleModal(this)">
<span class="slider"></span>
</label>
<form id="modal-${id.split("-")[1]}" class="separateDate">
<input type="date" onchange="updateDateField(this)" required>
</form>
</div>`
});
loanContainer.innerHTML+=`<button id="add-entry" onclick="addEntry()" >+</button>`
}
//Adds new form with input fields
function addEntry(){
const addEntryButton = document.getElementById('add-entry');
let number = Date.now();
const HTMLString =
`<div class="loan" id="group-${number}">
<button class="cancel-btn" onclick="cancelEntry(this)">
<svg clip-rule="evenodd" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" height="22px" width="22px" viewBox="1 1 22 22">
<path d="m12.002 2.005c5.518 0 9.998 4.48 9.998 9.997 0 5.518-4.48 9.998-9.998 9.998-5.517 0-9.997-4.48-9.997-9.998 0-5.517 4.48-9.997 9.997-9.997zm0 8.933-2.721-2.722c-.146-.146-.339-.219-.531-.219-.404 0-.75.324-.75.749 0 .193.073.384.219.531l2.722 2.722-2.728 2.728c-.147.147-.22.34-.22.531 0 .427.35.75.751.75.192 0 .384-.073.53-.219l2.728-2.728 2.729 2.728c.146.146.338.219.53.219.401 0 .75-.323.75-.75 0-.191-.073-.384-.22-.531l-2.727-2.728 2.717-2.717c.146-.147.219-.338.219-.531 0-.425-.346-.75-.75-.75-.192 0-.385.073-.531.22z" fill-rule="nonzero"/>
</svg>
</button>
<label for="name-group-${number}"><strong>Loan Name</strong></label>
<input type="text" placeholder="Name" id="name-group-${number}" class="loan-input"></input>
<label for="balance-group-${number}"><strong>Balance</strong></label>
<input type="number" min="1" placeholder="Balance" id="balance-group-${number}" class="loan-input"></input>
<label for="rate-group-${number}"><strong>Interest Rate</strong></label>
<input type="number" min="0" step="0.1" placeholder="%" id="rate-group-${number}" class="loan-input"></input>
<label for="minPayment"><strong>Min Monthly Payment: </strong></label>
<input type="number" min="0" step="0.01" placeholder="$0.00" id="minPayment-group-${number}" class="loan-input">
<button class="confirm-btn" onclick="confirmLoan(this)">CONFIRM</button>
</div>`
addEntryButton.insertAdjacentHTML('beforebegin', HTMLString)
addEntryButton.style.cursor = "not-allowed"
addEntryButton.disabled = true;
}
//cancel entry, deletes added container
function cancelEntry(buttonEl){
let target = document.getElementById('add-entry')
target.disabled = false;
target.style.cursor = "pointer"
buttonEl.parentElement.remove()
}
//deletes current container
function deleteEntry(buttonEl){
let container = buttonEl.closest('.loan')
let dataArrIndex = loanData.findIndex((item)=>
item.id === container.id
);
container.remove();
loanData.splice(dataArrIndex, 1);
localStorage.setItem("LPMdata", JSON.stringify(loanData))
}
//populates user input into an input form, ready for resubmition
function editEntry(buttonEl){
let target = buttonEl.closest('.loan');
let dataArrIndex = loanData.findIndex((item)=>
item.id === target.id);
const {id, loanName, balance, rate, minPayment}=loanData[dataArrIndex]
const HTMLString =
`<div class="loan" id="${id}">
<label for="name-${id}"><strong>Loan Name</strong></label>
<input type="text" placeholder="Name" id="name-${id}" class="loan-input" value="${loanName}"></input>
<label for="balance-${id}"><strong>Balance</strong></label>
<input type="number" min="1" placeholder="Balance" id="balance-${id}" class="loan-input" value="${balance}"></input>
<label for="rate-${id}"><strong>Interest Rate</strong></label>
<input type="number" min="0" step="0.1" placeholder="%" id="rate-${id}" class="loan-input" value="${rate}"></input>
<label for="minPayment"><strong>Min Monthly Payment: </strong></label>
<input type="number" min="0" step="0.01" placeholder="$0.00" id="minPayment-${id}" class="loan-input" value="${minPayment}">
<button class="confirm-btn" onclick="confirmLoan(this)">CONFIRM</button>
</div>`
document.getElementById(target.id).outerHTML = HTMLString
}
//Toggle the dropdown
function dropDown(buttonEl){
let content = buttonEl.nextElementSibling
buttonEl.classList.toggle("active")
}
//Toggle the separate date calendar input field
function toggleModal(inputEl){
let target = inputEl.id.split("-")[1]
let element = `modal-${target}`
let targetEl = document.getElementById(element)
targetEl.classList.toggle('toggleModal')
}
//reload all local storage data into the display field
window.addEventListener('DOMContentLoaded', (e)=>{
updateLoanContainer()
})