Skip to content

Commit

Permalink
Feat : 회원가입 통신
Browse files Browse the repository at this point in the history
- axios로 서버랑 통신한다.
  • Loading branch information
future9061 committed Feb 3, 2024
1 parent a2053f8 commit 03834ad
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 133 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "frontend",
"version": "0.1.0",
"proxy": "https://api.doit-toeic.xyz",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/apis/BaseUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import axios from 'axios';

export default axios.create({ baseURL: 'https://api.doit-toeic.xyz' });
19 changes: 19 additions & 0 deletions frontend/src/apis/FetchRegister.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { RegisterData } from '../types/RegisterData';
import url from './BaseUrl';

export const FetchRegister = async (register: RegisterData) => {
const data = { email: register.email, password: register.password };

const headers = {
'Content-Type': 'application/json',
};

try {
const res = await url.post('/auth/register', data, { headers });
res && alert('회원가입이 완료되었습니다.');
return res.data;
} catch (error) {
alert('회원가입이 실패하였습니다.');
throw new Error();
}
};
15 changes: 0 additions & 15 deletions frontend/src/components/Header.tsx

This file was deleted.

47 changes: 0 additions & 47 deletions frontend/src/components/LoginForm.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions frontend/src/components/common/SubmitBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { SubmitBtnCSS } from '../../style/components/common/SubmitBtnCSS';
import { SubmitBtnProps } from '../../types/SubmitBtnProps';

function SubmitBtn({ children, bgColor, color }: SubmitBtnProps) {
return <SubmitBtnCSS children={children} bgColor={bgColor} color={color} />;
function SubmitBtn({ children, bgcolor, color }: SubmitBtnProps) {
return <SubmitBtnCSS children={children} bgcolor={bgcolor} color={color} />;
}

export default SubmitBtn;
12 changes: 0 additions & 12 deletions frontend/src/components/register/DoubleCheck.tsx

This file was deleted.

78 changes: 29 additions & 49 deletions frontend/src/components/register/RegisterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
import { useEffect } from 'react';
import { RegisterFormCSS } from '../../style/components/register/RegisterFormCSS';
import { SubmitHandler, useForm } from 'react-hook-form';
import { RegisterData } from '../../types/RegisterData';
import DoubleCheck from './DoubleCheck';
import Agreement from './Agreement';
import SubmitBtn from '../common/SubmitBtn';
import { FetchRegister } from '../../apis/FetchRegister';
import { useNavigate } from 'react-router-dom';

function RegisterForm() {
const {
register,
handleSubmit,
watch,
formState: { errors },
setError,
clearErrors,
} = useForm<RegisterData>();
const navigate = useNavigate();

const onsubmit: SubmitHandler<RegisterData> = (data) => console.log(data);
const onsubmit: SubmitHandler<RegisterData> = async (Register) => {
await FetchRegister(Register);
navigate('/login');
};

useEffect(() => {
if (
watch('password') !== watch('password_confirm') &&
watch('password_confirm')
) {
setError('password_confirm', {
type: 'password-mismatch',
message: '비밀번호가 일치하지 않습니다.',
});
} else {
clearErrors('password_confirm');
}
}, [watch('password'), watch('password_confirm')]);
const passwordRef = watch('password');

return (
<>
<RegisterFormCSS onSubmit={handleSubmit(onsubmit)}>
<fieldset>
<input
placeholder="아이디"
{...register('username', { required: true })}
placeholder="이메일"
{...register('email', {
required: true,
pattern: {
value:
/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,}$/ as RegExp,
message: '유효하지 않은 이메일입니다.',
},
})}
/>
<DoubleCheck children={'중복'} />
</fieldset>
{errors.username && <span>잘못된 아이디입니다</span>}
{errors.email && <span>유효하지 않은 이메일입니다.</span>}

<fieldset>
<input
placeholder="비밀번호(영문, 숫자, 특수문자 포함 8 ~ 20자)"
placeholder="비밀번호(영문, 영문 대문자, 숫자, 특수문자 포함 8 ~ 20자)"
type="password"
{...register('password', {
required: true,
required: '비밀번호를 작성해주세요.',
pattern: {
value: /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/,
value:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/,
message: '유효하지 않은 비밀번호 입니다.',
},
})}
Expand All @@ -64,34 +61,17 @@ function RegisterForm() {
placeholder="비밀번호 확인"
type="password"
{...register('password_confirm', {
required: true,
required: '비밀번호를 확인해주세요.',
validate: (value) =>
value === passwordRef || '비밀번호가 일치하지 않습니다.',
})}
/>
</fieldset>
{(errors.password_confirm?.message && (
{errors.password_confirm && (
<span>{errors.password_confirm.message}</span>
)) ||
(watch('password') !== watch('password_confirm') &&
watch('password_confirm') && (
<span>{errors.password_confirm?.message}</span>
))}

<fieldset>
<input
placeholder="이메일"
{...register('email', { required: true })}
/>
<DoubleCheck children={'인증'} />
</fieldset>
{errors.email && <span>유효하지 않은 이메일입니다.</span>}

<fieldset>
<input placeholder="인증번호" />
<DoubleCheck children={'확인'} />
</fieldset>
)}

<Agreement />

<fieldset className="radio-filed">
<label>
<input
Expand All @@ -103,13 +83,13 @@ function RegisterForm() {
</label>

<label>
<input type="radio" name="agree" checked={true} value="false" />
<input type="radio" name="agree" defaultChecked value="false" />
비동의
</label>
</fieldset>
{errors.agree && <span>개인정보 동의서를 읽고 동의해주세요</span>}

<SubmitBtn children={'회원가입'} bgColor="#7AC3CE" color="#fff" />
<SubmitBtn children={'회원가입'} bgcolor="#7AC3CE" color="#fff" />
</RegisterFormCSS>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/style/components/common/SubmitBtnCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { SubmitBtnProps } from '../../../types/SubmitBtnProps';
import { media } from '../../mediaQuery';

export const SubmitBtnCSS = styled.button<SubmitBtnProps>`
background: ${(props) => props.bgColor || '#fff'};
background: ${(props) => props.bgcolor || '#fff'};
color: ${(props) => props.color || '#7AC3CE'};
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.35);
margin-top: 10px;
&:hover {
background-color: ${(props) => props.color || '#7AC3CE'};
color: ${(props) => props.bgColor || '#fff'};
color: ${(props) => props.bgcolor || '#fff'};
}
${media.smallMobile`
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/types/FormData.ts

This file was deleted.

1 change: 0 additions & 1 deletion frontend/src/types/RegisterData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type RegisterData = {
username: string;
password: string;
password_confirm: string;
email: string;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/SubmitBtnProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { ReactNode } from 'react';

export type SubmitBtnProps = {
children: ReactNode;
bgColor?: string;
bgcolor?: string;
color?: string;
};

0 comments on commit 03834ad

Please sign in to comment.