diff --git a/client/src/components/Logins/LoginConfirmatationModal.tsx b/client/src/components/Logins/LoginConfirmatationModal.tsx index 2ac20066..e94930e7 100644 --- a/client/src/components/Logins/LoginConfirmatationModal.tsx +++ b/client/src/components/Logins/LoginConfirmatationModal.tsx @@ -2,7 +2,7 @@ import React, { useEffect } from 'react'; import styled from 'styled-components'; import { useSelector, useDispatch } from 'react-redux'; import { setMemberInfo } from '../../reducer/member/memberInfoSlice.ts'; -import useGetMemberInfo from '../../hooks/useGetmemberInfo.ts'; +import { useGetMemberInfo } from '../../hooks/useGetMemberInfo.ts'; import { RootState } from '../../reducer/member/rootReducer'; const LoginConfirmationModal: React.FC = ({ onClose }) => { diff --git a/client/src/components/Profile/memberInfoModal.tsx b/client/src/components/Profile/memberInfoModal.tsx index 9d60792b..b0b966ff 100644 --- a/client/src/components/Profile/memberInfoModal.tsx +++ b/client/src/components/Profile/memberInfoModal.tsx @@ -1,7 +1,7 @@ import React from 'react'; import styled from 'styled-components'; import { useSelector } from 'react-redux'; -import useGetMemberInfo from '../../hooks/useGetmemberInfo'; +import { useGetMemberInfo } from '../../hooks/useGetMemberInfo.ts'; import { RootState } from '../../store/config'; const MemberInfoModal: React.FC = ({ onClose }) => { diff --git a/client/src/components/Profile/memberWithdrawalModal.tsx b/client/src/components/Profile/memberWithdrawalModal.tsx index 09cd9721..49324f21 100644 --- a/client/src/components/Profile/memberWithdrawalModal.tsx +++ b/client/src/components/Profile/memberWithdrawalModal.tsx @@ -1,43 +1,36 @@ -import React, { useState, useEffect } from 'react'; -import styled from 'styled-components'; -import axios from 'axios'; - -const MemberWithdrawalModal: React.FC = ({ onClose, memberId }) => { +import React, { useState } from 'react'; +import styled from 'styled-components'; +import { useSelector } from 'react-redux'; // redux-toolkit에서 가져옴 +import { useDeleteMember } from '../../hooks/useDeleteMembers'; // 적절한 경로로 수정 +import { RootState } from '../../store/config'; + +const MemberWithdrawalModal: React.FC = ({ onClose }) => { + // redux-toolkit에서 memberId 가져옴 + const memberId = useSelector((state: RootState) => state.login.memberId); + const [password, setPassword] = useState(''); - const [memberPassword, setMemberPassword] = useState(null); + const deleteMemberMutation = useDeleteMember(); const withdrawalTitle = "StockHolm에서 탈퇴하시겠습니까?"; const passwordInputLabel = "비밀번호를 입력해주세요."; const incorrectPasswordMsg = "Incorrect password!"; const withdrawalButtonText = "회원탈퇴"; - useEffect(() => { - axios.get(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/members/${memberId}`) - .then(response => { - setMemberPassword(response.data.password); - }) - .catch(error => { - console.error("Error fetching member password:", error); - }); - }, [memberId]); - const handleWithdrawal = () => { - if (password === memberPassword) { - axios.delete(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/members/${memberId}`) - .then(response => { - if (response.status === 204) { - alert('회원탈퇴 되었습니다!'); - onClose(); - } - }) - .catch(error => { - console.error('Error deleting member:', error); - }); + if (password === memberId) { + deleteMemberMutation.mutate(memberId, { + onSuccess: () => { + alert('회원탈퇴 되었습니다!'); + onClose(); + }, + onError: () => { + alert(incorrectPasswordMsg); + } + }); } else { alert(incorrectPasswordMsg); } }; - return ( @@ -53,7 +46,7 @@ const MemberWithdrawalModal: React.FC = ({ onClose, interface MemberWithdrawalModalProps { onClose: () => void; - memberId: string; + } // Styled Components Definitions: diff --git a/client/src/components/Profile/profileModal.tsx b/client/src/components/Profile/profileModal.tsx index 9f356cc0..c8754d2a 100644 --- a/client/src/components/Profile/profileModal.tsx +++ b/client/src/components/Profile/profileModal.tsx @@ -1,6 +1,7 @@ import React, { useState } from 'react'; import styled from 'styled-components'; import MemberInfoModal from './memberInfoModal'; // 경로는 실제 파일 위치에 따라 수정해야 합니다. +import MemberWithdrawalModal from './memberWithdrawalModal'; // 경로는 실제 파일 위치에 따라 수정해야 합니다. const ProfileModal: React.FC = ({ onClose }) => { const [selectedTab, setSelectedTab] = useState(1); @@ -21,12 +22,12 @@ const ProfileModal: React.FC = ({ onClose }) => { {selectedTab === 1 && } {selectedTab === 2 &&
현금 Content
} - {selectedTab === 3 &&
회원탈퇴 Content
} + {selectedTab === 3 && } {/* 회원탈퇴 모달 추가 */}
); -}; +} interface ProfileModalProps { onClose: () => void; diff --git a/client/src/hooks/useDeleteMembers.ts b/client/src/hooks/useDeleteMembers.ts new file mode 100644 index 00000000..483b772b --- /dev/null +++ b/client/src/hooks/useDeleteMembers.ts @@ -0,0 +1,12 @@ +import { useMutation } from 'react-query'; +import axios from 'axios'; + +export function useDeleteMember() { + return useMutation(async (memberId: string) => { + const response = await axios.delete(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/members/${memberId}`); + + if (response.status !== 204) { + throw new Error('Failed to delete member'); + } + }); +} diff --git a/client/src/hooks/useGetMemberInfo.ts b/client/src/hooks/useGetMemberInfo.ts index 2df3cc24..46a0927e 100644 --- a/client/src/hooks/useGetMemberInfo.ts +++ b/client/src/hooks/useGetMemberInfo.ts @@ -1,7 +1,7 @@ import { useQuery } from 'react-query'; import axios from 'axios'; -function useGetMemberInfo(memberId: number | null) { +export function useGetMemberInfo(memberId: number | null) { return useQuery(['member', memberId], async () => { const response = await axios.get(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com:8080/members/${memberId}`); @@ -13,4 +13,3 @@ function useGetMemberInfo(memberId: number | null) { }); } -export default useGetMemberInfo; \ No newline at end of file