Skip to content

Commit 1d27653

Browse files
feat: migrate feedback submission to Store API (#788)
1 parent 73ba385 commit 1d27653

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
PUBLIC_DIRECTUS_HOST=
1+
PUBLIC_STORE_API_HOST=

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@astrojs/sitemap": "^3.7.3",
2626
"@astrojs/starlight": "^0.37.6",
2727
"@astrojs/starlight-tailwind": "^4.0.2",
28-
"@directus/sdk": "^21.1.0",
2928
"@expressive-code/plugin-collapsible-sections": "^0.41.7",
3029
"@fontsource-variable/noto-sans": "^5.2.10",
3130
"@fontsource-variable/plus-jakarta-sans": "^5.2.8",

pnpm-lock.yaml

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Feedback.astro

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { Icon } from 'astro-icon/components';
3232

3333
<script>
3434
type FeedbackType = 'like' | 'dislike';
35-
import { createDirectus, rest, createItem } from '@directus/sdk';
3635

3736
function getFeedbackForm(): HTMLElement {
3837
return document.getElementById('feedback-form') as HTMLElement;
@@ -110,34 +109,39 @@ import { Icon } from 'astro-icon/components';
110109

111110
async function submitFeedback() {
112111
const comment = getCommentTextarea().value.trim();
112+
const feedbackType = getFeedbackForm().dataset.feedbackType as FeedbackType;
113113

114-
if (!comment) {
114+
// A comment is only mandatory for dislikes.
115+
if (feedbackType === 'dislike' && !comment) {
115116
getErrorMessage().classList.remove('hidden');
116117
getCommentTextarea().focus();
117118
return;
118119
}
119120

120-
const host = import.meta.env.PUBLIC_DIRECTUS_HOST;
121-
const feedbackType = getFeedbackForm().dataset.feedbackType as FeedbackType;
121+
const host = import.meta.env.PUBLIC_STORE_API_HOST;
122122

123123
if (!host) {
124-
console.error('Directus host is not set');
124+
console.error('Store API host is not set');
125125
closeForm(feedbackType);
126126
return;
127127
}
128128

129-
const client = createDirectus(host).with(rest());
130-
131129
try {
132-
await client.request(
133-
createItem('feedback' as never, {
130+
const response = await fetch(`${host}/api/feedback/submit`, {
131+
method: 'POST',
132+
headers: { 'Content-Type': 'application/json' },
133+
body: JSON.stringify({
134134
type: feedbackType,
135135
page: window.location.pathname,
136-
comment: comment,
137-
})
138-
);
136+
...(comment ? { comment } : {}),
137+
}),
138+
});
139+
140+
if (!response.ok) {
141+
console.error(`Feedback submission failed: ${response.status}`);
142+
}
139143
} catch (error) {
140-
console.log(error);
144+
console.error(error);
141145
}
142146

143147
closeForm(feedbackType);

0 commit comments

Comments
 (0)