Skip to content

Commit a0819e3

Browse files
imklausanscontext
andcommitted
fix(feedback): Disable all buttons based on click on no matter which feedback element (#524)
Co-authored-by: LRubin <[email protected]>
1 parent e298de7 commit a0819e3

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

js/feedback/index.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
const COMPONENT_NAME = 'data-feedback'
22
const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`
3-
const HELPFUL_BUTTON_SELECTOR = `[data-ref="feedback[helpful]"]`
4-
const UNHELPFUL_BUTTON_SELECTOR = `[data-ref="feedback[unhelpful]"]`
3+
const BUTTON_SELECTOR = `[data-ref-button]`
54
const CONTENT_SELECTOR = `[data-ref="feedback[content]"]`
65
const ACTIVE_CLASS = 'data-active-class'
76
const typewriter = require('analytics')
87

98
export default function () {
109
const components = document.querySelectorAll(COMPONENT_SELECTOR)
10+
const buttons = document.querySelectorAll(BUTTON_SELECTOR)
11+
const helpfulButtons = [...buttons].filter(button => button.dataset.refButton === 'helpful')
12+
const unhelpfulButtons = [...buttons].filter(button => button.dataset.refButton === 'unhelpful')
13+
const contents = document.querySelectorAll(CONTENT_SELECTOR)
1114

1215
const trackFeedback = (helpful, section) => {
1316
typewriter.feedbackProvided({
@@ -18,38 +21,42 @@ export default function () {
1821
})
1922
}
2023

21-
for (let i = 0; i < components.length; i++) {
22-
const helpfulButton = components[i].querySelector(HELPFUL_BUTTON_SELECTOR)
23-
const unhelpfulButton = components[i].querySelector(UNHELPFUL_BUTTON_SELECTOR)
24-
const content = components[i].querySelector(CONTENT_SELECTOR)
25-
26-
const clickHandler = () => {
27-
if (content) {
28-
content.hidden = false
24+
const clickHandler = () => {
25+
if (contents.length) {
26+
for (let i = 0; i < contents.length; i++) {
27+
contents[i].hidden = false
2928
}
30-
31-
helpfulButton.disabled = true
32-
unhelpfulButton.disabled = true
3329
}
3430

35-
helpfulButton.addEventListener('click', (event) => {
36-
event.preventDefault()
37-
const section = helpfulButton.hasAttribute('data-section') ? 'right-nav' : 'footer'
38-
const activeClass = helpfulButton.getAttribute(ACTIVE_CLASS)
39-
helpfulButton.classList.add(activeClass)
31+
for (let i = 0; i < buttons.length; i++) {
32+
buttons[i].disabled = true
33+
}
34+
}
4035

41-
clickHandler()
42-
trackFeedback(true, section)
43-
})
36+
const addActiveClasses = (elements) => {
37+
for (let i = 0; i < elements.length; i++) {
38+
const activeClass = elements[i].getAttribute(ACTIVE_CLASS)
39+
elements[i].classList.add(activeClass)
40+
}
41+
}
4442

45-
unhelpfulButton.addEventListener('click', (event) => {
43+
for (let i = 0; i < buttons.length; i++) {
44+
buttons[i].addEventListener('click', (event) => {
4645
event.preventDefault()
47-
const section = helpfulButton.hasAttribute('data-section') ? 'side-nav' : 'footer'
48-
const activeClass = unhelpfulButton.getAttribute(ACTIVE_CLASS)
49-
unhelpfulButton.classList.add(activeClass)
46+
47+
const section = buttons[i].hasAttribute('data-section') ? 'right-nav' : 'footer'
48+
const helpful = buttons[i].dataset.refButton === 'helpful'
49+
let activeButtons = []
5050

5151
clickHandler()
52-
trackFeedback(false, section)
52+
53+
if (helpful) {
54+
addActiveClasses(helpfulButtons)
55+
trackFeedback(true, section)
56+
} else {
57+
addActiveClasses(unhelpfulButtons)
58+
trackFeedback(false, section)
59+
}
5360
})
5461
}
5562
}

src/_includes/components/feedback.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h4>Was this page helpful?</h4>
2323

2424
<div class="flex gutter gutter--large">
2525
<div class="flex__column flex__column--shrink">
26-
<button class="button button--small button-fill button-fill--white button-fill--borderable" data-ref="feedback[helpful]" data-active-class="button-fill--primary">
26+
<button class="button button--small button-fill button-fill--white button-fill--borderable" data-ref-button="helpful" data-active-class="button-fill--primary">
2727
<span class="button__icon">
2828
{% include icons/symbols/thumb-up.svg %}
2929
</span>
@@ -33,7 +33,7 @@ <h4>Was this page helpful?</h4>
3333
</div>
3434

3535
<div class="flex__column flex__column--shrink">
36-
<button class="button button--small button-fill button-fill--white button-fill--borderable" data-ref="feedback[unhelpful]" data-active-class="button-fill--gray">
36+
<button class="button button--small button-fill button-fill--white button-fill--borderable" data-ref-button="unhelpful" data-active-class="button-fill--gray">
3737
<span class="button__icon">
3838
{% include icons/symbols/thumb-down.svg %}
3939
</span>

src/_includes/sidebar/feedback.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<div class="flex">
55
<div class="flex__column flex__column--6">
6-
<button class="button button--small button-fill button-fill--white" data-section="sidebar" data-ref="feedback[helpful]" data-active-class="button-fill--primary">
6+
<button class="button button--small button-fill button-fill--white" data-section="right-bar" data-ref-button="helpful" data-active-class="button-fill--primary">
77
<span class="button__icon">
88
{% include icons/symbols/thumb-up.svg %}
99
</span>
@@ -13,7 +13,7 @@
1313
</div>
1414

1515
<div class="flex__column flex__column--6">
16-
<button class="button button--small button-fill button-fill--white" data-section="sidebar" data-ref="feedback[unhelpful]" data-active-class="button-fill--gray">
16+
<button class="button button--small button-fill button-fill--white" data-section="right-nav" data-ref-button="unhelpful" data-active-class="button-fill--gray">
1717
<span class="button__icon">
1818
{% include icons/symbols/thumb-down.svg %}
1919
</span>

0 commit comments

Comments
 (0)