Skip to content

Johnny week3#16

Open
JohnnySiuky wants to merge 6 commits intodistinctioncoding:mainfrom
JohnnySiuky:johnny-week3
Open

Johnny week3#16
JohnnySiuky wants to merge 6 commits intodistinctioncoding:mainfrom
JohnnySiuky:johnny-week3

Conversation

@JohnnySiuky
Copy link

No description provided.

Copy link

@Gary-Distinctioncoding Gary-Distinctioncoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work, no any logic and code issues, all the comments are suggestions for higher code quality

Comment on lines +18 to +39
const getErrorMessage = (name, value) => {
const val = value.trim(); // 去除空白

switch (name) {
case 'username':
if (!val) return 'Please enter your name'; // true
return ''; // false

case 'phone':
if (!val) return 'Please enter your phone number';
if (isNaN(val)) return 'Phone number must be numeric';
if (val.length < 8) return 'Phone number must be at least 8 digits';
return '';

case 'account':
if (!val) return 'Please enter your Dulux account';
return '';

default:
return '';
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no logic error, can be simplified as

const validators = {
  username: () => {},
  phone: ()=>{},
  account: ()=>{}
}

Comment on lines +50 to +58
const errorMsg = getErrorMessage(id, value);

// 如果有錯,errorMsg 就是字串 -> 顯示紅字
// 如果沒錯,errorMsg 就是空字串 -> 紅字消失
setError((prev) => ({
...prev,
[id]: errorMsg
}))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以简化为,没有必要再存储一个变量

    setError((prev) => ({
      ...prev,
      [id]: getErrorMessage(id, value);
    }))
  }

Comment on lines +64 to +76
const usernameError = getErrorMessage('username', formData.username);
const phoneError = getErrorMessage('phone', formData.phone);
const accountError = getErrorMessage('account', formData.account);

// '' 這個會當false的
if (usernameError || phoneError || accountError) {
setError({
username: usernameError,
phone: phoneError,
account: accountError
});
return; // 停止發送
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以优化为:

const newErrors = {
    username: getErrorMessage('username', formData.username),
    phone: getErrorMessage('phone', formData.phone),
    account: getErrorMessage('account', formData.account)
  }
  
  if (Object.values(newErrors).some(err => err)) {
    setError(newErrors)
    return
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants