-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
62 lines (52 loc) · 1.81 KB
/
popup.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
import {
currencyList,
getExchangeRateFor,
getInMemoryStore,
setInMemoryStore,
updateStores,
updateUi,
updateUiErrorMessage,
} from './src/index';
import DOM from './src/elements-from-dom';
const settingsSelectClassName = DOM.settingsSelect.className.slice(0);
const addFavCurrenciesClassName = DOM.addFavCurrencies.className.slice(0);
DOM.settingsBtn.addEventListener('click', () => {
const showSettingsSelect = DOM.settingsSelect.className.indexOf('show') === -1;
DOM.settingsBtn.textContent = showSettingsSelect ? 'Close' : '︎Options';
DOM.settingsSelect.className = showSettingsSelect ? `${settingsSelectClassName} show` : settingsSelectClassName;
DOM.addFavCurrencies.className = showSettingsSelect ? `${addFavCurrenciesClassName} show` : addFavCurrenciesClassName;
});
DOM.addFavCurrencies.addEventListener('click', () => {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
const init = (sourceOrTargetCurrency) => {
const route = { ...getInMemoryStore(), ...sourceOrTargetCurrency };
getExchangeRateFor(route)
.then(updateUi)
.then(updateStores)
.catch(err => updateUiErrorMessage(err, route));
};
const populateSelectOption = (select) => {
currencyList.forEach(c => {
const option = document.createElement('option');
option.value = c;
option.textContent = c;
select.appendChild(option);
});
};
const handleOnSelectCurrency = (select) => {
select.addEventListener('change', () => {
const sourceOrTargetCurrency = { [select.id]: select.value };
setInMemoryStore({ ...sourceOrTargetCurrency, rate: null });
init(sourceOrTargetCurrency);
});
};
DOM.selects.forEach(select => {
populateSelectOption(select);
handleOnSelectCurrency(select);
});
init({});