Skip to content

Commit

Permalink
[Feat]회원삭제 기능 구현
Browse files Browse the repository at this point in the history
회원 탈퇴 페이지 완성
/members/{memberId}로 delete요청 후 비밀번호 통과 시 데이터베이스에서 회원 삭제
#Issues #15
  • Loading branch information
김병현 authored and 김병현 committed Sep 10, 2023
1 parent 770d5be commit ad74d4b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion client/src/components/Logins/LoginConfirmatationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginConfirmationProps> = ({ onClose }) => {
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Profile/memberInfoModal.tsx
Original file line number Diff line number Diff line change
@@ -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<MemberInfoModalProps> = ({ onClose }) => {
Expand Down
51 changes: 22 additions & 29 deletions client/src/components/Profile/memberWithdrawalModal.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import axios from 'axios';

const MemberWithdrawalModal: React.FC<MemberWithdrawalModalProps> = ({ 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<MemberWithdrawalModalProps> = ({ onClose }) => {
// redux-toolkit에서 memberId 가져옴
const memberId = useSelector((state: RootState) => state.login.memberId);

const [password, setPassword] = useState<string>('');
const [memberPassword, setMemberPassword] = useState<string | null>(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 (
<ModalBackground>
<ModalContainer>
Expand All @@ -53,7 +46,7 @@ const MemberWithdrawalModal: React.FC<MemberWithdrawalModalProps> = ({ onClose,

interface MemberWithdrawalModalProps {
onClose: () => void;
memberId: string;

}

// Styled Components Definitions:
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/Profile/profileModal.tsx
Original file line number Diff line number Diff line change
@@ -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<ProfileModalProps> = ({ onClose }) => {
const [selectedTab, setSelectedTab] = useState<number>(1);
Expand All @@ -21,12 +22,12 @@ const ProfileModal: React.FC<ProfileModalProps> = ({ onClose }) => {
<TabContent>
{selectedTab === 1 && <MemberInfoModal onClose={onClose}/>}
{selectedTab === 2 && <div>현금 Content</div>}
{selectedTab === 3 && <div>회원탈퇴 Content</div>}
{selectedTab === 3 && <MemberWithdrawalModal onClose={onClose}/>} {/* 회원탈퇴 모달 추가 */}
</TabContent>
</ModalContainer>
</ModalBackground>
);
};
}

interface ProfileModalProps {
onClose: () => void;
Expand Down
12 changes: 12 additions & 0 deletions client/src/hooks/useDeleteMembers.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
}
3 changes: 1 addition & 2 deletions client/src/hooks/useGetMemberInfo.ts
Original file line number Diff line number Diff line change
@@ -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}`);

Expand All @@ -13,4 +13,3 @@ function useGetMemberInfo(memberId: number | null) {
});
}

export default useGetMemberInfo;

0 comments on commit ad74d4b

Please sign in to comment.