Skip to content

Commit 3c29049

Browse files
authored
Update email api endpoints (#414)
1 parent 9e245cf commit 3c29049

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

src/api/email.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import axios from "axios";
2+
3+
export async function send(subject, message, attachments) {
4+
return await axios.post(`${import.meta.env.VITE_BACKEND_URL}/email/send`, {
5+
subject,
6+
message,
7+
attachments,
8+
});
9+
}

src/api/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { send } from "./email";
2+
3+
export const api = {
4+
email: {
5+
send,
6+
},
7+
};

src/pages/BugReport.jsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { editorConfig } from "../data/editorConfig";
1414
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
1515
import { $generateHtmlFromNodes } from "@lexical/html";
1616
import { CLEAR_EDITOR_COMMAND } from "lexical";
17-
import axios from "axios";
1817
import { Link } from "react-router-dom";
1918
import { socials } from "../data/socials";
19+
import { api } from "../api";
2020

2121
function Form({ theme }) {
2222
const [editor] = useLexicalComposerContext();
@@ -65,21 +65,19 @@ function Form({ theme }) {
6565
setLoading(true);
6666
editor.update(() => {
6767
const sendMail = async () => {
68-
await axios
69-
.post(`${import.meta.env.VITE_BACKEND_URL}/send_email`, {
70-
subject: `[BUG REPORT]: ${data.title}`,
71-
message: $generateHtmlFromNodes(editor),
72-
attachments: data.attachments,
73-
})
74-
.then(() => {
75-
Toast.success("Bug reported!");
76-
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null);
77-
resetForm();
78-
})
79-
.catch(() => {
80-
Toast.error("Oops! Something went wrong.");
81-
setLoading(false);
82-
});
68+
try {
69+
await api.email.send(
70+
`[BUG REPORT]: ${data.title}`,
71+
$generateHtmlFromNodes(editor),
72+
data.attachments,
73+
);
74+
Toast.success("Bug reported!");
75+
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null);
76+
resetForm();
77+
} catch {
78+
Toast.error("Oops! Something went wrong.");
79+
setLoading(false);
80+
}
8381
};
8482
sendMail();
8583
});

src/pages/Survey.jsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { $generateHtmlFromNodes } from "@lexical/html";
2020
import { CLEAR_EDITOR_COMMAND } from "lexical";
2121
import { Link } from "react-router-dom";
2222
import RichEditor from "../components/LexicalEditor/RichEditor";
23-
import axios from "axios";
2423
import { questions } from "../data/surveyQuestions";
24+
import { api } from "../api";
2525

2626
function SurveyForm({ theme }) {
2727
const [editor] = useLexicalComposerContext();
@@ -55,24 +55,20 @@ function SurveyForm({ theme }) {
5555
setLoading(true);
5656
editor.update(() => {
5757
const sendMail = async () => {
58-
await axios
59-
.post(`${import.meta.env.VITE_BACKEND_URL}/send_email`, {
60-
subject: `[SURVEY]: ${new Date().toDateString()}`,
61-
message: `${Object.keys(form).map(
62-
(k) => `<div>${questions[k]}</div><div>${form[k]}</div>`
63-
)}<div>How can we make drawDB a better experience for you?</div>${$generateHtmlFromNodes(
64-
editor
65-
)}`,
66-
})
67-
.then(() => {
68-
Toast.success("Thanks for the feedback!");
69-
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
70-
resetForm();
71-
})
72-
.catch(() => {
73-
Toast.error("Oops! Something went wrong.");
74-
setLoading(false);
75-
});
58+
try {
59+
await api.email.send(
60+
`[SURVEY]: ${new Date().toDateString()}`,
61+
`${Object.keys(form)
62+
.map((k) => `<div>${questions[k]}</div><div>${form[k]}</div>`)
63+
.join("\n\n")}<br/>${$generateHtmlFromNodes(editor)}`,
64+
);
65+
Toast.success("Thanks for the feedback!");
66+
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null);
67+
resetForm();
68+
} catch {
69+
Toast.error("Oops! Something went wrong.");
70+
setLoading(false);
71+
}
7672
};
7773
sendMail();
7874
});

0 commit comments

Comments
 (0)