Unverified Commit 60eb702f authored by Jude Gao's avatar Jude Gao Committed by GitHub

fix: enhance a11y, prevent double firing in error rating (#74563)

Improved a11y (aria-live, aria-label) and simplified loading/error state
to prevent double firing when feedback buttons get clicked multiple
times while loading.
parent 7581beff
...@@ -5,24 +5,28 @@ import { ThumbsDown } from '../../../../icons/thumbs/thumbs-down' ...@@ -5,24 +5,28 @@ import { ThumbsDown } from '../../../../icons/thumbs/thumbs-down'
interface ErrorFeedbackProps { interface ErrorFeedbackProps {
errorCode: string errorCode: string
} }
export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) { export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) {
const [voted, setVoted] = useState<boolean | null>(null) const [voted, setVoted] = useState<boolean | null>(null)
const hasVoted = voted !== null const hasVoted = voted !== null
const handleFeedback = useCallback( const handleFeedback = useCallback(
async (wasHelpful: boolean) => { async (wasHelpful: boolean) => {
// Optimistically set feedback state without loading/error states to keep implementation simple
setVoted(wasHelpful)
try { try {
const response = await fetch( const response = await fetch(
`${process.env.__NEXT_ROUTER_BASEPATH || ''}/__nextjs_error_feedback?errorCode=${errorCode}&wasHelpful=${wasHelpful}` `${process.env.__NEXT_ROUTER_BASEPATH || ''}/__nextjs_error_feedback?${new URLSearchParams(
{
errorCode,
wasHelpful: wasHelpful.toString(),
}
)}`
) )
if (!response.ok) { if (!response.ok) {
// Handle non-2xx HTTP responses here if needed // Handle non-2xx HTTP responses here if needed
console.error('Failed to record feedback on the server.') console.error('Failed to record feedback on the server.')
} }
setVoted(wasHelpful)
} catch (error) { } catch (error) {
console.error('Failed to record feedback:', error) console.error('Failed to record feedback:', error)
} }
...@@ -32,27 +36,29 @@ export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) { ...@@ -32,27 +36,29 @@ export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) {
return ( return (
<> <>
<div className="error-feedback"> <div className="error-feedback" role="region" aria-label="Error feedback">
{hasVoted ? ( {hasVoted ? (
<p className="error-feedback-thanks">Thanks for your feedback!</p> <p className="error-feedback-thanks" role="status" aria-live="polite">
Thanks for your feedback!
</p>
) : ( ) : (
<> <>
<p>Was this helpful?</p> <p>Was this helpful?</p>
<button <button
aria-label="Mark as helpful" aria-label="Mark as helpful"
onClick={() => handleFeedback(true)} onClick={() => handleFeedback(true)}
disabled={hasVoted}
className={`feedback-button ${voted === true ? 'voted' : ''}`} className={`feedback-button ${voted === true ? 'voted' : ''}`}
type="button"
> >
<ThumbsUp /> <ThumbsUp aria-hidden="true" />
</button> </button>
<button <button
aria-label="Mark as not helpful" aria-label="Mark as not helpful"
onClick={() => handleFeedback(false)} onClick={() => handleFeedback(false)}
disabled={hasVoted}
className={`feedback-button ${voted === false ? 'voted' : ''}`} className={`feedback-button ${voted === false ? 'voted' : ''}`}
type="button"
> >
<ThumbsDown /> <ThumbsDown aria-hidden="true" />
</button> </button>
</> </>
)} )}
......
...@@ -68,9 +68,8 @@ describe('ErrorOverlayLayout Component', () => { ...@@ -68,9 +68,8 @@ describe('ErrorOverlayLayout Component', () => {
screen.queryByText('Thanks for your feedback!') screen.queryByText('Thanks for your feedback!')
).not.toBeInTheDocument() ).not.toBeInTheDocument()
// Click helpful button
await act(async () => { await act(async () => {
fireEvent.click(screen.getByLabelText('Mark as helpful')) fireEvent.click(screen.getByRole('button', { name: 'Mark as helpful' }))
}) })
expect(fetch).toHaveBeenCalledWith( expect(fetch).toHaveBeenCalledWith(
...@@ -88,7 +87,9 @@ describe('ErrorOverlayLayout Component', () => { ...@@ -88,7 +87,9 @@ describe('ErrorOverlayLayout Component', () => {
).not.toBeInTheDocument() ).not.toBeInTheDocument()
await act(async () => { await act(async () => {
fireEvent.click(screen.getByLabelText('Mark as not helpful')) fireEvent.click(
screen.getByRole('button', { name: 'Mark as not helpful' })
)
}) })
expect(fetch).toHaveBeenCalledWith( expect(fetch).toHaveBeenCalledWith(
......
...@@ -348,8 +348,7 @@ describe('app dir - prefetching', () => { ...@@ -348,8 +348,7 @@ describe('app dir - prefetching', () => {
beforePageLoad(page: Page) { beforePageLoad(page: Page) {
page.on('request', async (req: Request) => { page.on('request', async (req: Request) => {
const url = new URL(req.url()) const url = new URL(req.url())
const headers = await req.allHeaders() if (url.searchParams.has('_rsc')) {
if (headers['rsc']) {
rscRequests.push(url.pathname + url.search) rscRequests.push(url.pathname + url.search)
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment