Skip to content

Commit

Permalink
Merge pull request #65 from Beside-Potenday/seungbeom
Browse files Browse the repository at this point in the history
�메일 저장 버튼 추가
  • Loading branch information
seung365 authored Aug 7, 2024
2 parents b678276 + 2a34d79 commit f3c194a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Provider/MailContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext, useContext, useState } from 'react';
import type { ReactNode } from 'react';
import { MailInput } from '@/types';
import { MailPostData, MailInput } from '@/types';

export type mailSendUniv = {
sender: string;
Expand All @@ -27,6 +27,8 @@ interface MailContextProps {
onIsActive: (state: 'univ' | 'business') => void;
resetMailInputUniv: () => void;
resetMailInputBusiness: () => void;
mailResult: MailPostData;
handleMailResult: (mailResult: MailPostData) => void;
}

export const MailContext = createContext<MailContextProps | null>(null);
Expand All @@ -41,6 +43,14 @@ export const MailProvider = ({ children }: { children: ReactNode }) => {
subject: '',
receiver: '',
});
const [mailResult, setMailResult] = useState<MailPostData>({
subject: '',
body: '',
});

const handleMailResult = (mailResult: MailPostData) => {
setMailResult(mailResult);
};

const handleMail = (mailBox: MailInput) => {
setMailInput(mailBox);
Expand Down Expand Up @@ -86,6 +96,8 @@ export const MailProvider = ({ children }: { children: ReactNode }) => {
onIsActive,
resetMailInputUniv,
resetMailInputBusiness,
mailResult,
handleMailResult,
}}
>
{children}
Expand Down
36 changes: 36 additions & 0 deletions src/api/hooks/usePostMail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MailPostData } from '@/types';
import axios from 'axios';
import { BASE_URL } from '..';
import { useMutation } from '@tanstack/react-query';

export const postMailPath = () => `${BASE_URL}/save-mail`;

const apiClient = axios.create({
baseURL: BASE_URL,
headers: {
Authorization: `Bearer ${sessionStorage.getItem('accessToken')}`,
},
});

const postMail = async (mailInput: MailPostData) => {
try {
const response = await apiClient.post<number>(postMailPath(), mailInput);
return response.data;
} catch (error) {
console.error('Error posting mail:', error);
throw error;
}
};

export const usePostMail = () => {
const { mutate } = useMutation({
mutationFn: postMail,
onSuccess: (result) => {
console.log('Mail posted successfully:', result);
},
onError: (error) => {
console.error('Error posting mail:', error);
},
});
return { mutate };
};
36 changes: 35 additions & 1 deletion src/components/Mail/MailModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
warningTextsBusiness,
warningTextsUniv,
} from './MailModalData';
import { useAuth } from '@/Provider/Auth';
import { usePostMail } from '@/api/hooks/usePostMail';

interface MailModalProps {
isOpen: boolean;
Expand All @@ -51,7 +53,11 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
const [isFocused, setIsFocused] = useState(false);
const [isHide, setIsHide] = useState(false);
const [firstInput, setFirstInput] = useState('');
const { isActive } = useMail();
const { isActive, handleMailResult, mailResult } = useMail();
const [noError, setNoError] = useState(false);
const { authInfo } = useAuth();

const { mutate: mailmutate } = usePostMail();

const currentcurrentInputNames =
isActive === 'univ' ? currentInputNames : currentInputNamesBusiness;
Expand Down Expand Up @@ -102,12 +108,18 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
setContent(data.content || '메일이 성공적으로 생성되었습니다.');
setIsSubmitted(true);
setIsLoading(false);
setNoError(true);
handleMailResult({
subject: data.title,
body: data.content,
});
},
onError: (error) => {
setTitle('메일 생성 실패');
setContent('메일 생성 중 오류가 발생했습니다.');
setIsSubmitted(true);
setIsLoading(false);
setNoError(false);
},
},
);
Expand Down Expand Up @@ -139,12 +151,19 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
setContent(data.content || '메일이 성공적으로 생성되었습니다.');
setIsSubmitted(true);
setIsLoading(false);
setNoError(true);

handleMailResult({
subject: data.title,
body: data.content,
});
},
onError: (error) => {
setTitle('메일 생성 실패');
setContent('메일 생성 중 오류가 발생했습니다.');
setIsSubmitted(true);
setIsLoading(false);
setNoError(false);
},
},
);
Expand Down Expand Up @@ -192,6 +211,14 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
}
};

const handlePutMail = () => {
if (authInfo) {
mailmutate({ ...mailResult });
} else {
alert('로그인 후 메일을 저장 할 수 있습니다.');
}
};

useEffect(() => {
setIsFocused(false);
setValue(currentcurrentInputNames[currentIndex], '', { shouldValidate: true });
Expand Down Expand Up @@ -337,6 +364,13 @@ export const MailModal = ({ isOpen, onOpen, onClose }: MailModalProps) => {
)}
</CustomModalFooter>
)}
{isSubmitted && noError && (
<ModalFooter>
<Button colorScheme="blue" onClick={handlePutMail}>
저장하기
</Button>
</ModalFooter>
)}
</CustomModalContent>
</CustomModal>
);
Expand Down
6 changes: 3 additions & 3 deletions src/pages/MyPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const MyPage = () => {
>
<GridItem backgroundColor={'yellow'}>
<h2>User Information</h2>
<h1>{authInfo?.email}</h1>
<h1>{authInfo?.name}</h1>
<h1>{authInfo?.email}</h1>
<img src={authInfo?.picture} alt="사용자 프로필" />
</GridItem>
<GridItem backgroundColor={'blue'}>
<h2>User History</h2>
{/* user history */}
<h2>메일 내역</h2>

</GridItem>
</Grid>
</Wrapper>
Expand Down
14 changes: 14 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ export interface AuthContextType {
authInfo?: AuthInfo;
updateAuthInfo: (auth: AuthInfo) => void;
}

export interface MailPostData {
subject: string;
body: string;
}

export interface MailGetData {
sender: string;
body: string;
year: string;
month: string;
day: string;
job: string;
}

0 comments on commit f3c194a

Please sign in to comment.