From ec1908381fd7b85131a311105dd9eb3b288208c6 Mon Sep 17 00:00:00 2001 From: seung365 Date: Thu, 8 Aug 2024 14:15:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A9=94=EC=9D=BC=20=EB=B3=B4=EB=82=B4?= =?UTF-8?q?=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/hooks/Mail/useGoMail.tsx | 41 +++++++++++++++++++++++++++++++ src/components/Mail/MailModal.tsx | 30 ++++++++++++++++++++++ src/types/index.ts | 7 ++++++ 3 files changed, 78 insertions(+) create mode 100644 src/api/hooks/Mail/useGoMail.tsx diff --git a/src/api/hooks/Mail/useGoMail.tsx b/src/api/hooks/Mail/useGoMail.tsx new file mode 100644 index 0000000..ccad041 --- /dev/null +++ b/src/api/hooks/Mail/useGoMail.tsx @@ -0,0 +1,41 @@ +import { MailGoData } from '@/types'; +import axios from 'axios'; +import { BASE_URL } from '../..'; +import { useMutation } from '@tanstack/react-query'; + +export const postMailPath = () => `${BASE_URL}/send-email`; + +const createApiClient = () => { + const token = sessionStorage.getItem('accessToken'); + + return axios.create({ + baseURL: BASE_URL, + headers: { + Authorization: `Bearer ${token}`, + }, + }); +}; + +const goMail = async (mailGo: MailGoData) => { + try { + const apiClient = createApiClient(); + const response = await apiClient.post(postMailPath(), mailGo); + return response.data; + } catch (error) { + console.error('Error posting mail:', error); + throw error; + } +}; + +export const useGoMail = () => { + const { mutate } = useMutation({ + mutationFn: goMail, + onSuccess: (result) => { + console.log('Mail send successfully:', result); + }, + onError: (error) => { + console.error('Error sending mail:', error); + }, + }); + return { mutate }; +}; diff --git a/src/components/Mail/MailModal.tsx b/src/components/Mail/MailModal.tsx index ec8f7ac..f71e63b 100644 --- a/src/components/Mail/MailModal.tsx +++ b/src/components/Mail/MailModal.tsx @@ -31,6 +31,7 @@ import { } from './MailModalData'; import { useAuth } from '@/Provider/Auth'; import { usePostMail } from '@/api/hooks/Mail/usePostMail'; +import { useGoMail } from '@/api/hooks/Mail/useGoMail'; interface MailModalProps { isOpen: boolean; @@ -57,6 +58,7 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => { const { authInfo } = useAuth(); const { mutate: mailmutate } = usePostMail(); + const { mutate: mailGo } = useGoMail(); const currentcurrentInputNames = isActive === 'univ' ? currentInputNames : currentInputNamesBusiness; @@ -222,6 +224,31 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => { onClose(); }; + const handleGoMail = () => { + const recipientEmail = prompt('받는 사람의 이메일 주소를 입력해 주세요:'); + + if (authInfo) { + const myMailAddress = sessionStorage.getItem('email'); + + if (recipientEmail) { + const mailGoContent = { + to: recipientEmail, + from: myMailAddress as string, + subject: mailResult.subject, + body: mailResult.body, + }; + mailGo({ ...mailGoContent }); + alert('📨 메일을 보냈습니다!'); + } else { + alert('유효한 이메일 주소를 입력해 주세요.'); + } + } else { + alert('로그인 후 메일을 보낼할 수 있습니다.'); + } + + onClose(); + }; + useEffect(() => { setIsFocused(false); setValue(currentcurrentInputNames[currentIndex], '', { shouldValidate: true }); @@ -373,6 +400,9 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => { + )} diff --git a/src/types/index.ts b/src/types/index.ts index 97f4d89..abc555b 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -66,3 +66,10 @@ export interface MailListResponse { content: Array<{ subject: string; body: string; createDate: string }>; pageable: { pageNumber: number; pageSize: number }; } + +export interface MailGoData { + to: string; + from: string; + subject: string; + body: string; +}