Skip to content

Commit 506b4da

Browse files
feat: require comment in page feedback form (#783)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 77cdddf commit 506b4da

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/components/Feedback.astro

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Icon } from 'astro-icon/components';
33
---
44

5-
<div class="flex flex-col border border-zinc-300 dark:border-zinc-700 p-3 rounded-xl w-72">
5+
<div class="flex flex-col border border-zinc-300 dark:border-zinc-700 p-3 rounded-xl w-80">
66
<div id="vote-buttons" class="flex gap-4 mx-auto">
77
<span>{Astro.locals.t('feedback.question')}</span>
88
<div class="flex gap-3">
@@ -19,6 +19,7 @@ import { Icon } from 'astro-icon/components';
1919
rows="3"
2020
maxlength="250"
2121
placeholder={Astro.locals.t('feedback.placeholder')}></textarea>
22+
<span id="feedback-error" class="hidden text-sm text-red-500">{Astro.locals.t('feedback.required')}</span>
2223
</div>
2324
<button
2425
id="submit-button"
@@ -49,6 +50,10 @@ import { Icon } from 'astro-icon/components';
4950
return document.getElementById('feedback-comment') as HTMLTextAreaElement;
5051
}
5152

53+
function getErrorMessage(): HTMLElement {
54+
return document.getElementById('feedback-error') as HTMLElement;
55+
}
56+
5257
getLikeButton()?.addEventListener('click', function() {
5358
toggleForm('like');
5459
});
@@ -68,6 +73,10 @@ import { Icon } from 'astro-icon/components';
6873
}
6974
});
7075

76+
getCommentTextarea()?.addEventListener('input', function() {
77+
getErrorMessage().classList.add('hidden');
78+
});
79+
7180
function toggleForm(type: FeedbackType): void {
7281
const feedbackForm = getFeedbackForm();
7382
const activeClass = 'text-accent-700';
@@ -83,6 +92,7 @@ import { Icon } from 'astro-icon/components';
8392
getLikeButton().classList.remove(activeClass);
8493
getDislikeButton().classList.remove(activeClass);
8594
getCommentTextarea().value = '';
95+
getErrorMessage().classList.add('hidden');
8696
return;
8797
}
8898

@@ -99,6 +109,14 @@ import { Icon } from 'astro-icon/components';
99109
}
100110

101111
async function submitFeedback() {
112+
const comment = getCommentTextarea().value.trim();
113+
114+
if (!comment) {
115+
getErrorMessage().classList.remove('hidden');
116+
getCommentTextarea().focus();
117+
return;
118+
}
119+
102120
const host = import.meta.env.PUBLIC_DIRECTUS_HOST;
103121
const feedbackType = getFeedbackForm().dataset.feedbackType as FeedbackType;
104122

@@ -115,7 +133,7 @@ import { Icon } from 'astro-icon/components';
115133
createItem('feedback' as never, {
116134
type: feedbackType,
117135
page: window.location.pathname,
118-
comment: getCommentTextarea().value,
136+
comment: comment,
119137
})
120138
);
121139
} catch (error) {

src/content/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const collections = {
2424
'editLink.error': z.string().optional(),
2525
'feedback.question': z.string().optional(),
2626
'feedback.placeholder': z.string().optional(),
27+
'feedback.required': z.string().optional(),
2728
'feedback.submit': z.string().optional(),
2829
'feedback.success': z.string().optional(),
2930
'footer.status': z.string().optional(),

src/content/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"editLink.error": "Error!",
66
"feedback.question": "Was this page helpful?",
77
"feedback.placeholder": "Tell us more...",
8+
"feedback.required": "Please enter a comment before submitting.",
89
"feedback.submit": "Submit",
910
"feedback.success": "Thank you for your feedback!",
1011
"footer.status": "Status",

0 commit comments

Comments
 (0)