Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 회원 가입 중복 호출 방지 #219

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/home/components/SignupContents/EmailForm/EmailForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BoxButton, PlainButton, SuffixTextField } from '@yourssu/design-system-
import { EMAIL_DOMAIN } from '@/constants/email.constant';
import { EmailFormProps } from '@/home/components/SignupContents/EmailForm/EmailForm.type.ts';
import { useEmailForm } from '@/home/components/SignupContents/EmailForm/useEmailForm.ts';
import { usePreventDuplicateClick } from '@/hooks/usePreventDuplicateClick.ts';

import {
StyledSignupButtonText,
Expand All @@ -18,7 +19,8 @@ import {
} from './EmailForm.style';

export const EmailForm = ({ onConfirm }: EmailFormProps) => {
const { email, emailSending, emailError, onEmailSubmit, onChange } = useEmailForm({ onConfirm });
const { email, emailError, onEmailSubmit, onChange } = useEmailForm({ onConfirm });
const { disabled, handleClick } = usePreventDuplicateClick();

return (
<StyledSignupContentContainer>
Expand Down Expand Up @@ -47,8 +49,8 @@ export const EmailForm = ({ onConfirm }: EmailFormProps) => {
size="large"
variant="filled"
rounding={8}
disabled={email === '' || emailSending}
onClick={onEmailSubmit}
disabled={email === '' || disabled}
onClick={() => handleClick(onEmailSubmit)}
>
<StyledSignupButtonText>인증 메일 받기</StyledSignupButtonText>
</BoxButton>
Expand Down
7 changes: 1 addition & 6 deletions src/home/components/SignupContents/EmailForm/useEmailForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ import { useFullEmail } from '@/hooks/useFullEmail';
export const useEmailForm = ({ onConfirm }: EmailFormProps) => {
const [email, setEmail] = useState('');
const [emailError, setEmailError] = useState<string | undefined>(undefined);
const [emailSending, setEmailSending] = useState(false);
const fullEmail = useFullEmail(email);

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value);
};

const onEmailSubmit = async () => {
setEmailSending(true);

const res = await postAuthVerificationEmail({ email: fullEmail, verificationType: 'SIGN_UP' });
if (res.data) {
onConfirm(fullEmail);
} else {
setEmailError(res.error?.response?.data.message || '이메일을 다시 확인해주세요.');
}

setEmailSending(false);
};

return { email, emailError, emailSending, onChange, onEmailSubmit };
return { email, emailError, onChange, onEmailSubmit };
};
7 changes: 5 additions & 2 deletions src/home/components/SignupContents/SignupForm/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BoxButton, PasswordTextField, SimpleTextField } from '@yourssu/design-s

import { SignupFormProps } from '@/home/components/SignupContents/SignupForm/SignUpForm.type.ts';
import { useSignUpForm } from '@/home/components/SignupContents/SignupForm/useSignUpForm.ts';
import { usePreventDuplicateClick } from '@/hooks/usePreventDuplicateClick.ts';
import { useSignupFormValidation } from '@/hooks/useSignupFormValidator.ts';

import {
Expand All @@ -19,6 +20,8 @@ export const SignupForm = ({ email, onConfirm }: SignupFormProps) => {
const { nicknameValidOnce, passwordValidOnce, isFormValid, isNicknameValid, isPasswordValid } =
useSignupFormValidation(nickname, password);

const { disabled, handleClick } = usePreventDuplicateClick();

return (
<StyledSignupContentContainer>
<StyledSignupContentTitle>회원가입</StyledSignupContentTitle>
Expand Down Expand Up @@ -51,8 +54,8 @@ export const SignupForm = ({ email, onConfirm }: SignupFormProps) => {
rounding={8}
size="large"
variant="filled"
onClick={onFormConfirm}
disabled={!isFormValid}
onClick={() => handleClick(onFormConfirm)}
disabled={!isFormValid || disabled}
>
<StyledSignupButtonText>회원가입</StyledSignupButtonText>
</BoxButton>
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/usePreventDuplicateClick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback, useRef, useState } from 'react';

export const usePreventDuplicateClick = () => {
const [disabled, setDisabled] = useState(false);
const loadingRef = useRef(false);

const handleClick = useCallback(async (callback: () => Promise<void>) => {
if (loadingRef.current) return;

loadingRef.current = true;
setDisabled(true);

await callback();

loadingRef.current = false;
setDisabled(false);
}, []);

return { disabled, handleClick };
};
Loading