Skip to content

Commit

Permalink
Add optional email + send from API to avoid CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
nichochar committed Jul 27, 2024
1 parent b2968bf commit 3d86e68
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
17 changes: 17 additions & 0 deletions packages/api/server/http.mts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ router.delete('/secrets/:name', cors(), async (req, res) => {
return res.json({ result: updated });
});

router.options('/feedback', cors());
router.post('/feedback', cors(), async (req, res) => {
const { feedback, email } = req.body;
// Every time you modify the appscript here, you'll need to update the URL below
// @TODO: once we have an env variable setup, we can use that here.
const url =
'https://script.google.com/macros/s/AKfycbxPrg8z47SkJnHyoZBYqNtkcH8hBe12f-f2UJJ3PcIHmKdbMMuJuPoOemEB1ib8a_IKCg/exec';

const result = await fetch(url, {
method: 'POST',
body: JSON.stringify({ feedback, email }),
headers: { 'Content-Type': 'text/plain;charset=utf-8' },
});

return res.json({ success: result.ok });
});

type NpmSearchResult = {
package: {
name: string;
Expand Down
16 changes: 13 additions & 3 deletions packages/web/src/components/feedback-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from 'react';
import { sendFeedback } from '@/lib/utils';
import { sendFeedback } from '@/lib/server';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
Dialog,
DialogContent,
Expand All @@ -19,6 +20,8 @@ export default function FeedbackDialog({
onOpenChange: (open: boolean) => void;
}) {
const [feedback, setFeedback] = useState('');
const [email, setEmail] = useState('');

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-xl">
Expand All @@ -27,7 +30,7 @@ export default function FeedbackDialog({
<DialogDescription asChild>
<div className="pt-4 flex flex-col gap-2">
<p>
We're always looking to improve Srcbook, and your feedback is invaluable.
We're always looking to improve Srcbook and your feedback is invaluable.
<br />
You can open a public{' '}
<a
Expand All @@ -43,11 +46,18 @@ export default function FeedbackDialog({
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
/>
<Input
type="text"
value={email}
placeholder="Email (optional)"
onChange={(e) => setEmail(e.target.value)}
/>
<Button
disabled={!feedback}
onClick={() => {
sendFeedback(feedback);
sendFeedback({ feedback, email });
setFeedback('');
setEmail('');
toast.info('Thanks for the feedback!');
onOpenChange(false);
}}
Expand Down
17 changes: 17 additions & 0 deletions packages/web/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,20 @@ export async function loadSrcbookExamples(): Promise<SrcbookExamplesResponse> {

return response.json();
}

type FeedbackRequestType = {
feedback: string;
email: string;
};

export async function sendFeedback({ feedback, email }: FeedbackRequestType) {
const response = await fetch(API_BASE_URL + '/feedback', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ feedback, email }),
});

if (!response.ok) {
console.error(response);
}
}
15 changes: 0 additions & 15 deletions packages/web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,3 @@ export function getTitleForSession(session: SessionType) {
const titleCell = session.cells.find((cell: CellType) => cell.type === 'title') as TitleCellType;
return titleCell?.text;
}

// POST feedback to Google Sheets
// Because of CORS, we don't get a response here so this is a fire and forget.
export async function sendFeedback(feedback: string) {
const url =
'https://script.google.com/macros/s/AKfycbxbNh5sEvmvuaYZyuNYY6vULEX1vyhkHrqoyfuUMBz3PG5RcekCVcuC4-ceboefxgF0FA/exec';

await fetch(url, {
method: 'POST',
redirect: 'follow',
mode: 'no-cors',
body: JSON.stringify({ feedback }),
headers: { 'Content-Type': 'text/plain;charset=utf-8' },
});
}

0 comments on commit 3d86e68

Please sign in to comment.