-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscholarship-fee-status-toggle.js
More file actions
62 lines (52 loc) · 1.93 KB
/
scholarship-fee-status-toggle.js
File metadata and controls
62 lines (52 loc) · 1.93 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
class ScholarshipFeeStatusToggle {
static selector() {
return '[data-scholarship-toggle]';
}
constructor(button) {
this.toggleSwitch = button;
this.checkbox = this.toggleSwitch.firstElementChild;
this.labelHome = this.toggleSwitch.querySelector(
'.toggle-switch__label--first',
);
this.labelOverseas = this.toggleSwitch.querySelector(
'.toggle-switch__label--last',
);
this.resetButton = document.querySelector('[data-filters-reset]');
let locationRoot = window.location.href;
locationRoot = locationRoot.replace('#results', '');
this.path = window.location.pathname;
const paramString = locationRoot.split('?')[1];
this.queryString = new URLSearchParams(paramString);
this.feeStatus = this.queryString.get('fee-status');
this.setCheckboxState();
this.bindEvents();
}
setCheckboxState() {
const isOverseas = this.feeStatus === 'international';
this.checkbox.checked = isOverseas;
this.updateLabels(isOverseas);
if (this.feeStatus && this.resetButton) {
this.resetButton.classList.remove('reset--hidden');
}
}
updateLabels(isOverseas) {
// Make sure that the selected label is highlighted.
this.labelHome.classList.toggle(
'toggle-switch__label--selected',
!isOverseas,
);
this.labelOverseas.classList.toggle(
'toggle-switch__label--selected',
isOverseas,
);
}
applyToggle() {
const feeStatus = this.checkbox.checked ? 'international' : 'uk';
this.queryString.set('fee-status', feeStatus);
window.location = `${this.path}?${this.queryString.toString()}#results`;
}
bindEvents() {
this.toggleSwitch.addEventListener('click', () => this.applyToggle());
}
}
export default ScholarshipFeeStatusToggle;