Skip to content

Commit 2b9e7fa

Browse files
committed
subscription qualifier field visibility should be toggled based on type
1 parent 89d5114 commit 2b9e7fa

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

app/assets/javascripts/subscriptions.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
document.addEventListener('DOMContentLoaded', () => {
2+
/**
3+
* Clears subscription qualifier field value
4+
* @returns {void}
5+
*/
26
const clearQualifier = () => {
37
const qualifierField = document.querySelector('.js-subscription-qualifier-field');
48

@@ -7,10 +11,40 @@ document.addEventListener('DOMContentLoaded', () => {
711
}
812
};
913

14+
/**
15+
* Sets subscription qualifier section visibility
16+
* @param {boolean} visible visibility state
17+
* @returns {void}
18+
*/
19+
const setQualifierVisibility = (visible) => {
20+
document.querySelector('.js-subscription-qualifier-field')
21+
.closest('.form-group')
22+
?.classList.toggle('hide', visible);
23+
};
24+
25+
/**
26+
* Is a given subscription type qualifiable?
27+
* @param {string} type subscription type
28+
* @returns {boolean}
29+
*/
30+
const isQualifiable = (type) => {
31+
return ['category', 'tag', 'user'].includes(type);
32+
};
33+
34+
/**
35+
* Is a given element a subscription type select?
36+
* @param {Element} element
37+
* @returns {element is HTMLSelectElement}
38+
*/
39+
const isTypeSelect = (element) => {
40+
return element.matches('.js-subscription-type-select');
41+
};
42+
1043
document.querySelectorAll('.js-subscription-type-select, .js-subscription-frequency-select').forEach((el) => {
1144
$(el).select2().on('change', ($event) => {
12-
if ($event.target.matches('.js-subscription-type-select')) {
45+
if (isTypeSelect($event.target)) {
1346
clearQualifier();
47+
setQualifierVisibility(!isQualifiable($event.target.value));
1448
}
1549
});
1650
});

0 commit comments

Comments
 (0)