-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
161 lines (135 loc) · 4.83 KB
/
main.js
File metadata and controls
161 lines (135 loc) · 4.83 KB
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
const currencyForm = document.getElementById("currency-form");
const fromSelect = document.getElementById("from-select");
const toSelect = document.getElementById("to-select");
const currencyInput = document.getElementById("currency-input");
const resultModal = document.querySelector("#result-modal .modal-content");
const btnShowResults = document.getElementById("btn-show-results");
const MsgContainer = document.getElementById("msg-container");
const fromInput = document.getElementById("from-input");
const toInput = document.getElementById("to-input");
const factorInput = document.getElementById("factor-input");
const saveChanges = document.getElementById("save-changes");
const closeAdd = document.getElementById('close-add');
const addBtn = document.getElementById('add-btn');
const countriesList = countries;
// Initilization
window.addEventListener("DOMContentLoaded", init);
// Listen to submit form
currencyForm.addEventListener("submit", currencyConvertor);
// Save Changes
saveChanges.addEventListener("click",addFactors);
addBtn.addEventListener('click', loadCountries);
function init(){
if (!getFactors()) {
showMsg( "warning", "<strong>Warning : </strong> Please Add Currency conversion factors to the list", 3000);
} else {
loadSelect(fromSelect, "from");
loadSelect(toSelect, "to");
loadCountries(fromInput);
loadCountries(toInput);
}
}
function currencyConvertor(e){
e.preventDefault();
const from = fromSelect.value;
const to = toSelect.value;
if (isNaN(currencyInput.value)) {
showMsg("danger", "<strong>Error: </Strong> Enter only numbers", 3000);
return;
}
if (currencyInput.value === "") {
showMsg( "danger", "<strong>Error: </Strong> Enter number of currency to convert", 3000);
return;
}
if (from !== to) {
const factor = getFactors();
const selectedConv = factor.find( (item) => item.from === from && item.to === to);
let result = selectedConv.factor * currencyInput.value;
let temp = `
<div class="modal-header">
<h5 class="modal-title text-center" id="add-model-header"> ${from} - ${to}</h5>
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body text-center" >${result} ${to}</div>`;
resultModal.innerHTML = temp;
btnShowResults.click();
} else {
showMsg( "danger", "<strong>Error: </Strong> Please select two different currncies", 3000);
}
}
function addFactors() {
if(factorInput.value!=='' ){
let factor = {
from: fromInput.value,
to: toInput.value,
factor: factorInput.value,
};
let factors = getFactors();
if (!factors) {
factors = [];
}
factors.push(factor);
setFactors(factors);
loadSelect(fromSelect, "from");
loadSelect(toSelect, "to");
closeAdd.click();
showMsg(
"success",
"<strong>SUCCESS: </Strong> Currency conversion Added to the system",
3000
);
}
}
function setFactors(factors) {
const currencyJSON = JSON.stringify(factors);
localStorage.setItem("currency", currencyJSON);
}
function getFactors() {
const currencyJSON = localStorage.getItem("currency");
if (currencyJSON) {
return (currency = JSON.parse(currencyJSON));
}
}
function loadCountries(){
let temp= ``;
countriesList.forEach(country=>{
temp += `<option value="${country.currency_code} - ${country.country}">${country.currency_code} - ${country.country}</option>`;
});
fromInput.innerHTML = temp;
toInput.innerHTML = temp;
}
function loadSelect(el, type) {
let temp = "";
const factors = getFactors();
if (type === "from") {
let from = [];
factors.forEach(item=>{ from.push(item.from)});
let uniqueFactors = [...new Set(from)];
uniqueFactors.forEach((item) => {
temp += `<option value="${item}">${item}</option>`;
});
} else if (type === "to") {
let to = [];
factors.forEach(item=>{ to.push(item.to)});
let uniqueFactors = [...new Set(to)];
uniqueFactors.forEach((item) => {
temp += `<option value="${item}">${item}</option>`;
});
}
el.innerHTML = temp;
}
function showMsg(type, msg, time) {
let temp = `
<div class="alert alert-${type} alert-dismissible fade show hide">
${msg}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
MsgContainer.innerHTML = temp;
setTimeout(() => {
MsgContainer.innerHTML = "";
}, time);
}