Skip to content

Commit

Permalink
Merge pull request #104 from Beside-Potenday/seungbeom
Browse files Browse the repository at this point in the history
feat: 메일 보내기 기능 추가
  • Loading branch information
seung365 authored Aug 8, 2024
2 parents 179f973 + ec19083 commit e2534c7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/api/hooks/Mail/useGoMail.tsx
Original file line number Diff line number Diff line change
@@ -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<string>(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 };
};
30 changes: 30 additions & 0 deletions src/components/Mail/MailModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -373,6 +400,9 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
<Button colorScheme="blue" onClick={handlePutMail}>
저장하기
</Button>
<Button colorScheme="blue" onClick={handleGoMail}>
메일 보내기
</Button>
</ModalFooter>
)}
</CustomModalContent>
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e2534c7

Please sign in to comment.