|
16 | 16 | </button> |
17 | 17 | </div> |
18 | 18 |
|
19 | | - <p v-if="vote !== null" class="feedback__reply text-sm text-terciary flex"> |
| 19 | + <p v-if="vote !== null && !isRateLimited" class="feedback__reply text-sm text-terciary flex"> |
20 | 20 | Thank you! We received your feedback. |
21 | 21 | </p> |
22 | 22 |
|
| 23 | + <p v-if="isRateLimited" class="feedback__reply text-sm text-terciary flex"> |
| 24 | + {{ rateLimitMessage }} |
| 25 | + </p> |
| 26 | + |
23 | 27 | <form |
24 | | - v-if="vote === false" |
| 28 | + v-if="vote === false && !isRateLimited" |
25 | 29 | class="flex flex-col gap-2 w-full" |
26 | 30 | @submit.prevent="handleSubmit" |
27 | 31 | > |
|
49 | 53 | const message = ref(''); |
50 | 54 | const feedbackId = ref(null); |
51 | 55 | const isSubmitting = ref(false); |
| 56 | + const isRateLimited = ref(false); |
| 57 | + const rateLimitMessage = ref(''); |
52 | 58 |
|
53 | | - function handleVote(val) { |
| 59 | + async function handleVote(val) { |
54 | 60 | if (isSubmitting.value) { return }; |
55 | 61 |
|
56 | 62 | vote.value = val; |
57 | 63 | isSubmitting.value = true; |
58 | 64 |
|
59 | | - fetch('/.netlify/functions/feedback-create', { |
60 | | - method: 'POST', |
61 | | - headers: { 'Content-Type': 'application/json' }, |
62 | | - body: JSON.stringify({ |
63 | | - pageUrl: window.location.href, |
64 | | - feedbackId: feedbackId.value, |
65 | | - vote: val |
66 | | - }) |
67 | | - }) |
68 | | - .then((res) => res.json()) |
69 | | - .then((data) => { |
70 | | - feedbackId.value ||= data.feedbackId; |
71 | | - console.log('create callback') |
72 | | - console.log(`id: ${feedbackId.value}`) |
| 65 | +
|
| 66 | + try { |
| 67 | + const res = await fetch('/.netlify/functions/feedback-create', { |
| 68 | + method: 'POST', |
| 69 | + headers: { 'Content-Type': 'application/json' }, |
| 70 | + body: JSON.stringify({ |
| 71 | + pageUrl: window.location.href, |
| 72 | + feedbackId: feedbackId.value, |
| 73 | + vote: val, |
| 74 | + }), |
73 | 75 | }) |
74 | | - .catch((err) => console.error('Feedback error:', err)) |
75 | | - .finally(() => { isSubmitting.value = false; }); |
| 76 | +
|
| 77 | + if (res.status === 429) { |
| 78 | + const data = await res.json() |
| 79 | + isRateLimited.value = true |
| 80 | + rateLimitMessage.value = data.error || 'Too many requests. Please wait.' |
| 81 | + return |
| 82 | + } |
| 83 | +
|
| 84 | + const data = await res.json() |
| 85 | + feedbackId.value ||= data.feedbackId |
| 86 | + isRateLimited.value = false |
| 87 | + rateLimitMessage.value = '' |
| 88 | + } catch (err) { |
| 89 | + console.error('Feedback error:', err) |
| 90 | + } finally { |
| 91 | + isSubmitting.value = false; |
| 92 | + } |
76 | 93 | } |
77 | 94 |
|
78 | 95 | function handleSubmit() { |
|
0 commit comments