-
Notifications
You must be signed in to change notification settings - Fork 39
[윤정환] sprint4 #139
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
The head ref may contain hidden characters: "Basic-\uC724\uC815\uD658-sprint4"
[윤정환] sprint4 #139
Changes from 5 commits
95d038d
4e794f5
14972ef
0c13b34
1d2f2f6
fd06a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!-- --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //모든 input의 유효성 검사를 통과했을 때에만 누를 수 있어야하므로 validate와 작동방식이 달라야함 | ||
| export function switchBtnStatus() { | ||
| const btn = document.querySelector(".btn--interactions"); | ||
| const errorMessages = document.querySelectorAll(".form__error-msg"); | ||
| const inputs = document.querySelectorAll(".form__input"); | ||
|
|
||
| for (const message of errorMessages) { | ||
| if (message.textContent) { | ||
| btn.setAttribute("disabled", ""); | ||
| btn.classList.add("btn--disabled"); | ||
| return; | ||
| } | ||
| } | ||
| for (const input of inputs) { | ||
| if (!input.value) { | ||
| btn.setAttribute("disabled", ""); | ||
| btn.classList.add("btn--disabled"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| btn.removeAttribute("disabled"); | ||
| btn.classList.remove("btn--disabled"); | ||
| } | ||
|
|
||
| export function switchVisibility(event) { | ||
|
||
| const btn = event.target.parentElement; | ||
| const passwordInput = btn.previousElementSibling; | ||
| const iconOff = btn.children[0]; | ||
| const iconOn = btn.children[1]; | ||
|
|
||
| if (passwordInput.getAttribute("type") === "password") { | ||
| passwordInput.setAttribute("type", "text"); | ||
| btn.setAttribute("aria-pressed", "true"); | ||
| } else { | ||
| passwordInput.setAttribute("type", "password"); | ||
| btn.setAttribute("aria-pressed", "false"); | ||
| } | ||
| iconOff.classList.toggle("display-none"); | ||
| iconOn.classList.toggle("display-none"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| isEmpty, | ||
| validateEmail, | ||
| validatePassword, | ||
| validatePasswordConfirm, | ||
| validatePasswordConfirmReverse, | ||
| validateNickname, | ||
| } from "./validate.js"; | ||
| import { switchBtnStatus, switchVisibility } from "./btn-functions.js"; | ||
|
|
||
| function isSignupPage() { | ||
|
||
| if (window.location.pathname.search("signup") === -1) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| const form = document.querySelector(".container--form"); | ||
| const validateMap = { | ||
| email: validateEmail, | ||
| password: validatePassword, | ||
| "password-confirm": validatePasswordConfirm, | ||
| nickname: validateNickname, | ||
| }; | ||
|
|
||
| form.addEventListener("focusout", (event) => { | ||
| const { id } = event.target; | ||
|
|
||
| if (validateMap[id]) { | ||
| validateMap[id](event); | ||
| //비밀번호확인란부터 입력후 비밀번호를 변경할 시 비밀번호 확인 유효성 검사 | ||
| if (id === "password" && isSignupPage()) { | ||
| const passwordConfirmInput = document.getElementById("password-confirm"); | ||
| const passwordConfirm = passwordConfirmInput.value; | ||
| if (!isEmpty(passwordConfirm)) { | ||
| validatePasswordConfirmReverse(); | ||
| } | ||
| } | ||
| switchBtnStatus(); | ||
| } | ||
| }); | ||
|
Collaborator
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. 우선 validateMap을 만들어 각 필드마다 유효성검사를 수행하는 함수를 사용해 코드를 더 간결하게 만든건 아주 잘하셨습니다! 👍 다만 비밀번호 확인의 경우, 비밀번호 확인 함수 내부에서 비밀번호 확인란에 값이 있는지 체크하고 유효성 검사를 실행하는 로직을 써주지않고, 이 함수에서 꼭 조건문을 써줘야하는 필연적인 이유가 보이진 않네요 :)
Collaborator
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. 만약 함수가 하는 일을 좀 더 세부적으로 쪼개서 사용해보고싶었던거라면, 이런식으로 나눠보시는게 더 나을것같아요.
각 요소를 핸들링하는 함수에서
이런식으로 해야하는 일들을 순서대로 보여주게하면 좀 더 흐름이 좋아지고 개별적으로 테스트하기도 용이해져서 유지보수에 좋겠죠? :) 또한 중간에 추가적으로 핸들링해줘야하는 작업이 또 생긴다면 얼마든지 확장도 가능해질겁니다! :) |
||
| form.addEventListener("click", (event) => { | ||
| if (event.target.classList.contains("form__icon")) { | ||
| switchVisibility(event); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // common functions | ||
| export function isEmpty(value) { | ||
| return value ? false : true; | ||
| } | ||
|
|
||
| function addInvalid(input) { | ||
| input.classList.add("invalid"); | ||
| } | ||
|
|
||
| function removeInvalid(input) { | ||
| input.classList.remove("invalid"); | ||
| } | ||
|
|
||
| // email functions | ||
| const emailPattern = /^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+/; | ||
|
|
||
| function checkEmailFormat(email) { | ||
| if (emailPattern.test(email)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function validateEmail(event) { | ||
|
Collaborator
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. email, password, password confirm, nickname 지금은 이렇게 네개지만 지금과 같은 구조라면 폼 요소가 더 늘어날때 작성되는 코드양도 같이 늘어나겠죠? 재사용 가능한 단위까지 로직을 쪼개서 단일 책임을 가지고있는 함수를 만들면 어떨까요?
export const validators = {
email: (value) => {
if (!value) return { isValid: false, message: "이메일을 입력해주세요" };
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
return { isValid: false, message: "올바른 이메일 형식이 아닙니다" };
}
return { isValid: true, message: "" };
},
password: (value) => {
if (!value) return { isValid: false, message: "비밀번호를 입력해주세요" };
if (value.length < 8) {
return { isValid: false, message: "비밀번호는 8자 이상이어야 합니다" };
}
return { isValid: true, message: "" };
},
};
const validateInput = (input, errorMsgElement, validatorType) => {
const { isValid, message } = validators[validatorType](input.value);
errorMsgElement.innerText = message;
// 스타일 업데이트
input.classList.remove("success", "error");
errorMsgElement.classList.remove("error");
if (isValid) {
input.classList.add("success");
} else {
input.classList.add("error");
errorMsgElement.classList.add("error");
}
return isValid;
}; |
||
| const emailInput = event.target; | ||
| const email = emailInput.value; | ||
| let divErrorMessage = document.querySelector(".error-msg--email"); | ||
|
|
||
| if (isEmpty(email)) { | ||
| divErrorMessage.textContent = "이메일을 입력해주세요"; | ||
| addInvalid(emailInput); | ||
| } else if (!checkEmailFormat(email)) { | ||
| divErrorMessage.textContent = "잘못된 이메일 형식입니다"; | ||
| addInvalid(emailInput); | ||
| } else { | ||
| divErrorMessage.textContent = ""; | ||
| removeInvalid(emailInput); | ||
| } | ||
| } | ||
|
|
||
| // password functions | ||
| function checkPasswordFormat(password) { | ||
| if (password.length < 8) { | ||
| return false; | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| function checkPassword(password, passwordConfirm) { | ||
| return password === passwordConfirm ? true : false; | ||
| } | ||
|
|
||
| export function validatePassword(event) { | ||
| const passwordInput = event.target; | ||
| const password = passwordInput.value; | ||
| let divErrorMessage = document.querySelector(".error-msg--password"); | ||
|
|
||
| if (isEmpty(password)) { | ||
| divErrorMessage.textContent = "비밀번호를 입력해주세요"; | ||
| addInvalid(passwordInput); | ||
| } else if (!checkPasswordFormat(password)) { | ||
| divErrorMessage.textContent = "비밀번호를 8자 이상 입력해주세요"; | ||
| addInvalid(passwordInput); | ||
| } else { | ||
| divErrorMessage.textContent = ""; | ||
| removeInvalid(passwordInput); | ||
| } | ||
| } | ||
|
|
||
| export function validatePasswordConfirm(event) { | ||
| const passwordInput = document.getElementById("password"); | ||
| const password = passwordInput.value; | ||
| const passwordConfirmInput = event.target; | ||
| const passwordConfirm = passwordConfirmInput.value; | ||
| let divErrorMessage = document.querySelector(".error-msg--password-confirm"); | ||
|
|
||
| if (!checkPassword(password, passwordConfirm)) { | ||
| divErrorMessage.textContent = "비밀번호가 일치하지 않습니다"; | ||
| addInvalid(passwordConfirmInput); | ||
| } else { | ||
| divErrorMessage.textContent = ""; | ||
| removeInvalid(passwordConfirmInput); | ||
| } | ||
| } | ||
|
|
||
| //password유효성 검사를 하면서 password-confirm검사를 하기 위해 event가 아닌 고정으로 값을 가져오도록 함 | ||
| export function validatePasswordConfirmReverse() { | ||
| const passwordInput = document.getElementById("password"); | ||
| const password = passwordInput.value; | ||
| const passwordConfirmInput = document.getElementById("password-confirm"); | ||
| const passwordConfirm = passwordConfirmInput.value; | ||
| let divErrorMessage = document.querySelector(".error-msg--password-confirm"); | ||
|
|
||
| if (!checkPassword(password, passwordConfirm)) { | ||
| divErrorMessage.textContent = "비밀번호가 일치하지 않습니다"; | ||
| addInvalid(passwordConfirmInput); | ||
| } else { | ||
| divErrorMessage.textContent = ""; | ||
| removeInvalid(passwordConfirmInput); | ||
| } | ||
| } | ||
|
|
||
| //nickname functions | ||
| export function validateNickname(event) { | ||
| const nicknameInput = event.target; | ||
| const nickname = nicknameInput.value; | ||
| let divErrorMessage = document.querySelector(".error-msg--nickname"); | ||
|
|
||
| if (isEmpty(nickname)) { | ||
| divErrorMessage.textContent = "닉네임을 입력해주세요"; | ||
| addInvalid(nicknameInput); | ||
| } else { | ||
| divErrorMessage.textContent = ""; | ||
| removeInvalid(nicknameInput); | ||
| } | ||
| } | ||
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.
switch보다는 update나 toggle이 더 적합한 네이밍이 아닐까싶네요!
switchBtnStatus=>toggleBtnStatus