From 3a499d316d0776d04f490987dedce555c1f13ae4 Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Sun, 20 Apr 2025 00:45:16 +0400 Subject: [PATCH] update email api endpoints --- src/api/email.js | 9 +++++++++ src/api/index.js | 7 +++++++ src/pages/BugReport.jsx | 30 ++++++++++++++---------------- src/pages/Survey.jsx | 34 +++++++++++++++------------------- 4 files changed, 45 insertions(+), 35 deletions(-) create mode 100644 src/api/email.js create mode 100644 src/api/index.js diff --git a/src/api/email.js b/src/api/email.js new file mode 100644 index 0000000..e0375c2 --- /dev/null +++ b/src/api/email.js @@ -0,0 +1,9 @@ +import axios from "axios"; + +export async function send(subject, message, attachments) { + return await axios.post(`${import.meta.env.VITE_BACKEND_URL}/email/send`, { + subject, + message, + attachments, + }); +} diff --git a/src/api/index.js b/src/api/index.js new file mode 100644 index 0000000..bfd4362 --- /dev/null +++ b/src/api/index.js @@ -0,0 +1,7 @@ +import { send } from "./email"; + +export const api = { + email: { + send, + }, +}; diff --git a/src/pages/BugReport.jsx b/src/pages/BugReport.jsx index b0a11c1..42260e9 100644 --- a/src/pages/BugReport.jsx +++ b/src/pages/BugReport.jsx @@ -14,9 +14,9 @@ import { editorConfig } from "../data/editorConfig"; import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; import { $generateHtmlFromNodes } from "@lexical/html"; import { CLEAR_EDITOR_COMMAND } from "lexical"; -import axios from "axios"; import { Link } from "react-router-dom"; import { socials } from "../data/socials"; +import { api } from "../api"; function Form({ theme }) { const [editor] = useLexicalComposerContext(); @@ -65,21 +65,19 @@ function Form({ theme }) { setLoading(true); editor.update(() => { const sendMail = async () => { - await axios - .post(`${import.meta.env.VITE_BACKEND_URL}/send_email`, { - subject: `[BUG REPORT]: ${data.title}`, - message: $generateHtmlFromNodes(editor), - attachments: data.attachments, - }) - .then(() => { - Toast.success("Bug reported!"); - editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null); - resetForm(); - }) - .catch(() => { - Toast.error("Oops! Something went wrong."); - setLoading(false); - }); + try { + await api.email.send( + `[BUG REPORT]: ${data.title}`, + $generateHtmlFromNodes(editor), + data.attachments, + ); + Toast.success("Bug reported!"); + editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null); + resetForm(); + } catch { + Toast.error("Oops! Something went wrong."); + setLoading(false); + } }; sendMail(); }); diff --git a/src/pages/Survey.jsx b/src/pages/Survey.jsx index 1291ba8..5a28332 100644 --- a/src/pages/Survey.jsx +++ b/src/pages/Survey.jsx @@ -20,8 +20,8 @@ import { $generateHtmlFromNodes } from "@lexical/html"; import { CLEAR_EDITOR_COMMAND } from "lexical"; import { Link } from "react-router-dom"; import RichEditor from "../components/LexicalEditor/RichEditor"; -import axios from "axios"; import { questions } from "../data/surveyQuestions"; +import { api } from "../api"; function SurveyForm({ theme }) { const [editor] = useLexicalComposerContext(); @@ -55,24 +55,20 @@ function SurveyForm({ theme }) { setLoading(true); editor.update(() => { const sendMail = async () => { - await axios - .post(`${import.meta.env.VITE_BACKEND_URL}/send_email`, { - subject: `[SURVEY]: ${new Date().toDateString()}`, - message: `${Object.keys(form).map( - (k) => `
${questions[k]}
${form[k]}
` - )}
How can we make drawDB a better experience for you?
${$generateHtmlFromNodes( - editor - )}`, - }) - .then(() => { - Toast.success("Thanks for the feedback!"); - editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined); - resetForm(); - }) - .catch(() => { - Toast.error("Oops! Something went wrong."); - setLoading(false); - }); + try { + await api.email.send( + `[SURVEY]: ${new Date().toDateString()}`, + `${Object.keys(form) + .map((k) => `
${questions[k]}
${form[k]}
`) + .join("\n\n")}
${$generateHtmlFromNodes(editor)}`, + ); + Toast.success("Thanks for the feedback!"); + editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null); + resetForm(); + } catch { + Toast.error("Oops! Something went wrong."); + setLoading(false); + } }; sendMail(); });