Skip to content

Commit 7112aaf

Browse files
committed
feat: add check duplicated logic when change email
1 parent fdcd657 commit 7112aaf

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/components/setting/SettingEmailRow.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import SettingInput from './SettingInput';
55
import styled from 'styled-components';
66
import Button from '../common/Button';
77
import SettingEmailSuccess from './SettingEmailSuccess';
8+
import { toast } from 'react-toastify';
9+
import { CHECK_DUPLICATED_EMAIL } from '../../lib/graphql/user';
10+
import client from '../../lib/graphql/client';
811

912
export type SettingEmailRowProps = {
1013
email: string;
@@ -22,9 +25,28 @@ function SettingEmailRow({
2225

2326
const onSubmit = async (e: React.FormEvent) => {
2427
e.preventDefault();
25-
//! TODO: 이메일 형식으로 입력하지 않을 경우 처리
26-
//! TODO: 같은 이메일인 경우 처리
27-
//! TODO: 이메일 중복 체크
28+
29+
if (!validateEmail(value)) {
30+
toast.error('잘못된 이메일 형식입니다.');
31+
return;
32+
}
33+
34+
if (value === email) {
35+
toast.error('새 이메일 주소가 현재 이메일과 동일합니다.');
36+
return;
37+
}
38+
39+
const response = await client.query<{ isDuplicated: boolean }>({
40+
query: CHECK_DUPLICATED_EMAIL,
41+
fetchPolicy: 'network-only',
42+
variables: { email: value },
43+
});
44+
45+
if (response.data.isDuplicated) {
46+
toast.error('동일한 이메일이 존재합니다.');
47+
return;
48+
}
49+
2850
await onChangeEmail(value);
2951
setEdit(false);
3052
};
@@ -66,3 +88,9 @@ const Form = styled.form`
6688
`;
6789

6890
export default SettingEmailRow;
91+
92+
function validateEmail(email: string) {
93+
const re =
94+
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
95+
return re.test(String(email).toLowerCase());
96+
}

src/containers/etc/EmailChange.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { connect } from 'react-redux';
33
import { withRouter, RouteComponentProps } from 'react-router';
44
import qs from 'qs';
55

6-
import { CONFIRM_CHANGE_EMAIL } from '../../lib/graphql/user';
6+
import {
7+
CONFIRM_CHANGE_EMAIL,
8+
CurrentUser,
9+
GET_CURRENT_USER,
10+
} from '../../lib/graphql/user';
711
import { RootState } from '../../modules';
812
import { useApolloClient } from '@apollo/react-hooks';
913
import styled from 'styled-components';
1014
import { toast } from 'react-toastify';
1115
import SpinnerBlock from '../../components/common/SpinnerBlock';
1216
import usePopup from '../../lib/hooks/usePopup';
17+
import storage from '../../lib/storage';
1318

1419
const mapStateToProps = (state: RootState) => ({});
1520
const mapDispatchToProps = {};
@@ -38,12 +43,20 @@ const EmailChange: React.FC<EmailChangeProps> = ({ location, history }) => {
3843
},
3944
});
4045

46+
const response = await client.query<{ auth: CurrentUser }>({
47+
query: GET_CURRENT_USER,
48+
fetchPolicy: 'network-only',
49+
});
50+
51+
storage.setItem('CURRENT_USER', response.data.auth);
52+
4153
history.replace('/setting');
4254
popup.open({
4355
title: '성공',
4456
message: '이메일 변경이 완료 되었습니다.',
4557
});
4658
} catch (e) {
59+
// TODO: show 401
4760
toast.error('잘못된 접근입니다.');
4861
history.replace('/');
4962
}

src/lib/graphql/user.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ export const GET_USER_SERIES_LIST = gql`
130130
}
131131
`;
132132

133+
export const CHECK_DUPLICATED_EMAIL = gql`
134+
query CHECK_DUPLICATED_EMAIL($email: String!) {
135+
checkDuplicatedEmail(email: $email) {
136+
isDuplicated
137+
}
138+
}
139+
`;
140+
133141
// Generated by https://quicktype.io
134142

135143
export interface GetUserSeriesListResponse {

0 commit comments

Comments
 (0)