-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.js
More file actions
61 lines (50 loc) · 1.8 KB
/
application.js
File metadata and controls
61 lines (50 loc) · 1.8 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
import onChange from 'https://cdn.jsdelivr.net/npm/on-change@5.0.1/+esm';
import i18next from 'i18next';
import resources from './locales.js';
export default async function () {
const state = {
counter: 0,
lng: 'ru',
};
const watchedState = onChange(state, async () => {
await render(watchedState);
});
await render(watchedState);
}
async function render(watchedState) {
const i18n = i18next.createInstance();
const controls = {
primaryLang: () => document.querySelector('.btn-group > *:nth-child(1)'),
otherLang: () => document.querySelector('.btn-group > *:nth-child(2)'),
clicks: () => document.querySelector('.btn-info'),
reset: () => document.querySelector('.btn-warning'),
};
await i18n.init({
lng: watchedState.lng,
resources
});
document.querySelector('.container').innerHTML = `
<div class="btn-group" role="group">
<button type="button" class="btn mb-3 ${watchedState.lng === 'en' ? 'btn-primary' : 'btn-outline-primary'}">${i18n.t('english')}</button>
<button type="button" class="btn mb-3 ${watchedState.lng === 'ru' ? 'btn-primary' : 'btn-outline-primary'}">${i18n.t('russian')}</button>
</div>
<button type="button" class="btn btn-info mb-3 align-self-center">${i18n.t('clicks', {count: watchedState.counter})}</button>
<button type="button" class="btn btn-warning">${i18n.t('reset')}</button>
`;
controls.primaryLang()
.addEventListener('click', (e) => {
watchedState.lng = 'en';
});
controls.otherLang()
.addEventListener('click', (e) => {
watchedState.lng = 'ru';
});
controls.clicks()
.addEventListener('click', (e) => {
watchedState.counter = watchedState.counter + 1;
});
controls.reset()
.addEventListener('click', (e) => {
watchedState.counter = 0;
});
}