-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor (SignForm) : handleSubmit 함수에서 책임별 함수 분리
- Loading branch information
Showing
11 changed files
with
114 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import { InputRef } from "@/components/Main/sign/Sign.type"; | ||
import { ErrorText, Input, StyledLabel } from "@/components/Main/sign/SignLabel.styled"; | ||
import useSignInput from "@/hooks/useSignInput"; | ||
import { RefObject, forwardRef, useImperativeHandle } from "react"; | ||
|
||
export default function SignEmLabel() { | ||
export default forwardRef(function SignEmLabel(props, ref) { | ||
const { error, input, p, handleBlur, handleFocus } = useSignInput(); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => { | ||
return { | ||
...(ref as RefObject<InputRef>).current, | ||
emBlur() { | ||
return handleBlur(); | ||
}, | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<StyledLabel $error={error}> | ||
이메일 | ||
<Input type="email" name="email" placeholder="이메일을 입력해주세요" ref={input} onBlur={handleBlur} onFocus={handleFocus} $error={error} /> | ||
<ErrorText ref={p} /> | ||
</StyledLabel> | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,50 @@ | ||
import { InputType, Signin } from "@/components/Main/sign/Sign.type"; | ||
import { StyledForm, SubmitButton } from "@/components/Main/sign/SignForm.styled"; | ||
import SignEmLabel from "@/components/Main/sign/SignEmLabel"; | ||
import { StyledForm, SubmitButton } from "@/components/Main/sign/SignForm.styled"; | ||
import SignPwLabel from "@/components/Main/sign/SignPwLabel"; | ||
import useInputAllBlur from "@/hooks/useInputAllBlur"; | ||
import { NextRouter, useRouter } from "next/router"; | ||
import { FormEvent } from "react"; | ||
import axios from "@/lib/axios"; | ||
import { validate_signin, validate_signup } from "@/utils/validate"; | ||
import { useRouter } from "next/router"; | ||
import { FormEvent, useState } from "react"; | ||
|
||
export default function SignForm() { | ||
const [isError, SetIsError] = useState(false); | ||
const { inputRef, allBlur } = useInputAllBlur(); | ||
const router = useRouter(); | ||
const signin = router.asPath === "/signin"; | ||
|
||
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
const inputs = Array.from(document.querySelectorAll("input")); | ||
const ps = Array.from(document.querySelectorAll("label p")); | ||
for (const i in inputs) { | ||
const validateFunc = signin ? validate_signin : validate_signup; | ||
const type = inputs[i].type as InputType["type"]; | ||
const value = inputs[i].value; | ||
|
||
const res = await validateFunc({ type, value }); | ||
if (res) { | ||
SetIsError(true); | ||
ps[i].textContent = res; | ||
continue; | ||
} | ||
SetIsError(false); | ||
} | ||
const isError = await allBlur(); | ||
|
||
if (!isError) { | ||
try { | ||
const postData = { | ||
email: inputs[0].value, | ||
password: inputs[1].value, | ||
}; | ||
const url = signin ? "/api/sign-in" : "/api/sign-up"; | ||
const res = await axios.post(url, postData); | ||
const token = res.data.data.accessToken; | ||
sessionStorage.setItem("accessToken", token); | ||
router.push(`/folder?a=${token}`, "/folder"); | ||
} catch { | ||
ps.forEach((p) => (p.textContent = "계정 정보를 확인해주세요.")); | ||
} | ||
postSignData(e.target, signin, router); | ||
} | ||
}; | ||
|
||
return ( | ||
<StyledForm onSubmit={handleSubmit}> | ||
<SignEmLabel /> | ||
<SignPwLabel type="password" /> | ||
{signin ? null : <SignPwLabel type="passwordCheck" />} | ||
<SignEmLabel ref={inputRef} /> | ||
<SignPwLabel ref={inputRef} type="password" /> | ||
{signin ? null : <SignPwLabel ref={inputRef} type="passwordCheck" />} | ||
<SubmitButton>{signin ? "로그인" : "회원가입"}</SubmitButton> | ||
</StyledForm> | ||
); | ||
} | ||
|
||
const postSignData = async (obj: EventTarget, signin: boolean, router: NextRouter) => { | ||
try { | ||
const postData = new FormData(obj as HTMLFormElement); | ||
const email = postData.get("email"); | ||
const password = postData.get("password"); | ||
|
||
const url = signin ? "/api/sign-in" : "/api/sign-up"; | ||
const res = await axios.post(url, { email, password }); | ||
const { accessToken, refreshToken } = res.data.data; | ||
document.cookie = `accessToken=${accessToken}`; | ||
document.cookie = `refreshToken=${refreshToken}`; | ||
|
||
router.push(`/folder`); | ||
} catch { | ||
alert("아이디 또는 비밀번호가 일치하지 않습니다. 다시 입력해주세요."); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { Signin } from "@/components/Main/sign/Sign.type"; | ||
import SignForm from "@/components/Main/sign/SignForm"; | ||
import SignLogo from "@/components/Main/sign/SignLogo"; | ||
import { StyledSection } from "@/components/Main/sign/SignSection.styled"; | ||
import SignSns from "@/components/Main/sign/SignSns"; | ||
|
||
export default function SignSection({ signin = false }: Signin) { | ||
export default function SignSection() { | ||
return ( | ||
<StyledSection> | ||
<SignLogo signin={signin} /> | ||
<SignForm signin={signin} /> | ||
<SignSns signin={signin} /> | ||
<SignLogo /> | ||
<SignForm /> | ||
<SignSns /> | ||
</StyledSection> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { InputRef } from "@/components/Main/sign/Sign.type"; | ||
import { useRef } from "react"; | ||
|
||
export default function useInputAllBlur() { | ||
const inputRef = useRef<InputRef>(null); | ||
|
||
const allBlur = () => { | ||
if (!inputRef.current) return; | ||
let res; | ||
for (const f of Object.values(inputRef.current)) { | ||
if (f instanceof Function) { | ||
res = f(); | ||
} | ||
} | ||
return res; | ||
}; | ||
|
||
return { inputRef, allBlur }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import SignSection from "@/components/Main/sign/SignSection"; | ||
|
||
export default function Signin() { | ||
return <SignSection signin />; | ||
return <SignSection />; | ||
} |