-
Notifications
You must be signed in to change notification settings - Fork 79
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
[임희서] sprint8 #711
Merged
airman5573
merged 1 commit into
codeit-bootcamp-frontend:React-임희서
from
Imgaic:React-임희서-sprint8
Jun 27, 2024
The head ref may contain hidden characters: "React-\uC784\uD76C\uC11C-sprint8"
Merged
[임희서] sprint8 #711
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,89 +3,67 @@ import { useState } from "react"; | |
const EmailPattern = /^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-]+\.[A-za-z0-9\-]+/; | ||
|
||
export default function useValidationCheck(type) { | ||
const [errorMessages, setErrorMessages] = useState(() => { | ||
if (type === "login") { | ||
return { | ||
emailError: "initial", | ||
passwordError: "initial", | ||
}; | ||
} else { | ||
return { | ||
emailError: "initial", | ||
nicknameError: "initial", | ||
passwordError: "initial", | ||
confirmPasswordError: "initial", | ||
}; | ||
} | ||
}); | ||
const initialErrorMessages = | ||
type === "login" | ||
? { | ||
emailError: "initial", | ||
passwordError: "initial", | ||
} | ||
: { | ||
emailError: "initial", | ||
nicknameError: "initial", | ||
passwordError: "initial", | ||
confirmPasswordError: "initial", | ||
}; | ||
|
||
const [errorMessages, setErrorMessages] = useState(initialErrorMessages); | ||
|
||
const setError = (field, message) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 양을 줄이기 위해 따로 setError를 만드셨군요 👍 |
||
setErrorMessages((prev) => ({ ...prev, [field]: message })); | ||
}; | ||
|
||
const checkEmail = (value) => { | ||
if (!value || !EmailPattern.test(value)) { | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
emailError: !value | ||
? "이메일을 입력해주세요" | ||
setError( | ||
"emailError", | ||
!value | ||
? "이메일을 입력해주세요" | ||
: EmailPattern.test(value) | ||
? "" | ||
: "잘못된 이메일 형식입니다", | ||
})); | ||
} else | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
emailError: "", | ||
})); | ||
); | ||
}; | ||
|
||
const checkNickname = (value) => { | ||
if (!value) { | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
nicknameError: "닉네임을 입력해주세요", | ||
})); | ||
} else | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
nicknameError: "", | ||
})); | ||
setError("nickNameError", !value ? "닉네임을 입력해주세요" : ""); | ||
}; | ||
|
||
const checkPassword = (value) => { | ||
if (!value || value.length < 8) { | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
passwordError: !value | ||
? "비밀번호를 입력해주세요" | ||
: "비밀번호를 8자 이상 입력해주세요", | ||
})); | ||
} else | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
passwordError: "", | ||
})); | ||
setError( | ||
"passwordError", | ||
!value | ||
? "비밀번호를 입력해주세요" | ||
: value.length < 8 | ||
? "비밀번호를 8자 이상 입력해주세요" | ||
: "", | ||
); | ||
}; | ||
|
||
const checkConfirmPassword = (value, password) => { | ||
if (value !== password) { | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
confirmPasswordError: "비밀번호가 일치하지 않습니다", | ||
})); | ||
} else | ||
setErrorMessages((prev) => ({ | ||
...prev, | ||
confirmPasswordError: "", | ||
})); | ||
setError( | ||
"confirmPasswordError", | ||
value === password ? "" : "비밀번호가 일치하지 않습니다", | ||
); | ||
}; | ||
|
||
return type === "login" | ||
? { | ||
errorMessages, | ||
checkEmail, | ||
checkPassword, | ||
} | ||
: { | ||
errorMessages, | ||
checkEmail, | ||
checkNickname, | ||
checkPassword, | ||
checkConfirmPassword, | ||
}; | ||
const validators = { | ||
checkEmail, | ||
checkPassword, | ||
}; | ||
|
||
if (type !== "login") { | ||
validators.checkNickname = checkNickname; | ||
validators.checkConfirmPassword = checkConfirmPassword; | ||
} | ||
|
||
return { errorMessages, ...validators }; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
잘 작성 해주셨습니다만, 몇가지 개선할 부분이 보입니다.
아래 코드를 참고해주세요!