Skip to content

Commit

Permalink
fix: disabled (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaoNeng-wWw authored Nov 26, 2023
1 parent 1de5185 commit b413ecf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 43 deletions.
13 changes: 8 additions & 5 deletions app/authenticate/hooks/useButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ export const useDisabled = (
password: string,
confirmPassword: string,
checkCode: string,
valide: boolean
valide: boolean,
passwordRobustness: boolean[]
):[boolean,React.Dispatch<SetStateAction<boolean>>] => {
const [disabled, setDisabled] = useState<boolean>(
!policyState ||
(userName.length > 30 || userName.length < 2) ||
password !== confirmPassword
)
useEffect(()=>{
setDisabled(!policyState ||
(password !== confirmPassword) || !valide ||
checkCode.length !== 0)
}, [policyState, userName.length, password, confirmPassword, valide, checkCode])
setDisabled(
!(
policyState && password === confirmPassword && valide && checkCode.length > 0 && passwordRobustness.every((v) => v)
)
)
}, [policyState, userName.length, password, confirmPassword, valide, checkCode, passwordRobustness])
return [disabled, setDisabled]
}
export const useButtonColor = (
Expand Down
25 changes: 10 additions & 15 deletions app/authenticate/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ const CheckCode = (props: {type: 'email' | 'phone' | 'unknown', userInput: strin
setLoading(true)
IOC.user.getCheckCodeByEmail(props.userInput)
.then((val)=>{
setCD(val.data.cd);
// setCD(val.data.cd);
})
.catch((err)=>console.log(err))
.finally(()=>setLoading(false));
}
if (type === 'phone'){
setLoading(true)
IOC.user.getCheckCodeByPhone(props.userInput)
.then((val)=>{
setCD(val.data.cd);
// setCD(val.data.cd);
})
.finally(()=>setLoading(false));
}
Expand All @@ -68,13 +69,7 @@ const CheckCode = (props: {type: 'email' | 'phone' | 'unknown', userInput: strin
},[time])
return (
<Button size="lg" onClick={getCode} isLoading={loading}>
{
loading ?
<span>
请等待 {time}
</span>
: <span>发送验证码</span>
}
<span>点击发送验证码</span>
</Button>
)
}
Expand Down Expand Up @@ -220,9 +215,9 @@ export default function Page() {
useEffect(()=>{
setValide(/^[\w\d.\-_a-zA-Z\u4e00-\u9fa5]+$/gm.test(userName) && userName.length >= 2);
}, [userName]);
const [disabled] = useDisabled(policyState,userName,password,confirmPassword,code,valide);
const {buttonColor} = useButtonColor(disabled);
const [passwordRobustness, setPasswordRobustness] = useState(new Array(6).fill(false));
const [disabled] = useDisabled(policyState,userName,password,confirmPassword,code,valide, passwordRobustness);
const {buttonColor} = useButtonColor(disabled);
const fns = useMemo(()=>{
return [
(val: string) => val.length >= 8,
Expand Down Expand Up @@ -271,8 +266,8 @@ export default function Page() {
}
setLoading(true);
IOC.user.createUser({
phone: type === 'phone' ? userInput : undefined,
email: type === 'email' ? userInput : undefined,
phone: type === 'phone' ? userInput : '',
email: type === 'email' ? userInput : '',
password,
username: userName,
code
Expand Down Expand Up @@ -347,10 +342,10 @@ export default function Page() {
<Button
isLoading={loading}
disabled={
pageType !== 'wait-check' ? disabled : !policyState && isEmail || isPhone
pageType !== 'wait-check' ? disabled : !policyState && (isEmail || isPhone)
}
color={
pageType !== 'wait-check' ? buttonColor : policyState && isEmail || isPhone ? 'primary' : 'default'
pageType !== 'wait-check' ? buttonColor : policyState && (isEmail || isPhone) ? 'primary' : 'default'
}
onClick={handleClick()[pageType]}
>
Expand Down
25 changes: 3 additions & 22 deletions providers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,22 @@ http.interceptors.request.use(
(config) => {
if (typeof window !== 'undefined'){
const token = cookie.load('token')
// const controller = new AbortController();
// // if (!token){
// // Message.error('登陆状态过期')
// // controller.abort()
// // config.signal = controller.signal;
// // }
if (token){
config.headers.token = token;
}
}
return config;
},
(err)=>{
console.log(err);
throw new Error(err);
}
)
http.interceptors.response.use((val)=>{

return val;
return Promise.reject(val);
}, (err: AxiosError)=>{
const status = err.response?.status ?? {};
switch (status){
case 401:
Message.error('登录状态过期')
break;
case 404:
Message.error('接口不存在')
break;
case 429:
Message.error('请求过快')
break;
default:
Message.error(`登陆错误: ${err.response?.data}`);
console.error(err.response?.data)
}
const msg = typeof err.response?.data === 'object' ? JSON.stringify(err.response?.data ?? {}) : '未知错误'
Message.error(msg);
return Promise.reject(err);
})
2 changes: 1 addition & 1 deletion providers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class User {
return this.axios.get('/user', { withCredentials: true });
}
async getCheckCodeByPhone(phone: string){
return this.axios.get<CheckCodeData>('/user/code/phone', {params:{phone}});
return this.axios.get<'success'>(`/sms/${phone}`);
}
async getCheckCodeByEmail(email: string){
return this.axios.get<CheckCodeData>('/user/code/email', {params: {email}});
Expand Down

0 comments on commit b413ecf

Please sign in to comment.