Open
Conversation
Gary-Distinctioncoding
left a comment
There was a problem hiding this comment.
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 ''; | ||
| } | ||
| } |
There was a problem hiding this comment.
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 | ||
| })) | ||
| } |
There was a problem hiding this comment.
可以简化为,没有必要再存储一个变量
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; // 停止發送 | ||
| } |
There was a problem hiding this comment.
可以优化为:
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
}
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.