Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowballXueQiu committed Dec 21, 2023
1 parent d004346 commit 12574cd
Show file tree
Hide file tree
Showing 27 changed files with 338 additions and 313 deletions.
2 changes: 2 additions & 0 deletions app/about/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from "react";

export default function AboutLayout({children,}: {
children: React.ReactNode;
}) {
Expand Down
45 changes: 23 additions & 22 deletions app/authenticate/components/check-code.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
'use client'

import { useState } from "react";
import {useState} from "react";
import IOC from "@/providers";
import { Button } from "@nextui-org/react";
import {Button} from "@nextui-org/react";
import CountDown from "./count-down";

const CheckCode = (props: {type: 'email' | 'phone' | 'unknown', userInput: string}) => {
const CheckCode = (props: { type: 'email' | 'phone' | 'unknown', userInput: string }) => {
const {type} = props;
const [cd, setCD] = useState(0)
const onCountdownFinish = () => {
setCD(0)
}
const getCode = () => {
if (type === 'email'){
if (type === 'email') {
IOC.user.getCheckCodeByEmail(props.userInput)
.then((val)=>{
setCD(val.data.cd + 1);
})
.catch((err)=>{
console.log(err);
setCD(60 * 1000);
})
.then((val) => {
setCD(val.data.cd + 1);
})
.catch((err) => {
console.log(err);
setCD(60 * 1000);
})
}
if (type === 'phone'){
if (type === 'phone') {
IOC.user.getCheckCodeByPhone(props.userInput)
.then((val)=>{
setCD(val.data.cd + 1);
console.log(val.data.cd)
})
.catch(()=>{
setCD(60 * 1000);
})
.then((val) => {
setCD(val.data.cd + 1);
console.log(val.data.cd)
})
.catch(() => {
setCD(60 * 1000);
})
}
}
return (
<Button onClick={getCode} isDisabled={cd > 0} size="lg" className="flex gap-2 flex-grow-0 flex-shrink-0 basis-auto px-2 w-32 break-words">
<Button onClick={getCode} isDisabled={cd > 0} size="lg"
className="flex gap-2 flex-grow-0 flex-shrink-0 basis-auto px-2 w-32 break-words">
{
cd > 0 ? <CountDown countDown={cd} onFinish={onCountdownFinish} /> : '发送验证码'
cd > 0 ? <CountDown countDown={cd} onFinish={onCountdownFinish}/> : '发送验证码'
}
</Button>
)
}

export default CheckCode;
export default CheckCode;
20 changes: 10 additions & 10 deletions app/authenticate/components/count-down.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
'use client'

import { useEffect, useRef, useState } from "react";
import { useCountDown } from "../hooks"
import {useEffect, useState} from "react";
import {useCountDown} from "../hooks"

const CountDown = (
props: {countDown: number, onFinish: () => void}
props: { countDown: number, onFinish: () => void }
) => {
const [cd, setCD] = useState(
const [cd] = useState(
Number((props.countDown / 1000).toFixed())
);
const [time, start] = useCountDown(cd);
useEffect(()=>{
useEffect(() => {
start();
},[])
useEffect(()=>{
if (time === 0){
}, [])
useEffect(() => {
if (time === 0) {
props.onFinish();
}
},[time, props])
}, [time, props])
return (
<span>{time}秒后重新获取</span>
)
}

export default CountDown;
export default CountDown;
21 changes: 11 additions & 10 deletions app/authenticate/components/password-robustness-list.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IoCheckmark, IoCloseSharp } from "react-icons/io5";
import {IoCheckmark, IoCloseSharp} from "react-icons/io5";

const PasswordRobustnessList = (props: {active: boolean[]}) => {
const PasswordRobustnessList = (props: { active: boolean[] }) => {
const labels = [
{
key: 'min',
Expand All @@ -21,21 +21,22 @@ const PasswordRobustnessList = (props: {active: boolean[]}) => {
return (
<ul className="w-full">
{
labels.map(({label, key, defaultShow}, idx)=>{
labels.map(({label, key, defaultShow}, idx) => {
let show = defaultShow;
if (idx === labels.length-1){
if (idx === labels.length - 1) {
show = !props.active[idx];
}
return show && (
<li
key={key}
className={`
key={key}
className={`
flex items-center transition
${props.active[idx] ? 'text-green-600 dark:text-green-500' : 'text-red-600 dark:text-red-500'}`
}
>
}
>
{
props.active[idx] ? <IoCheckmark className="inline text-xl" /> : <IoCloseSharp className="inline text-xl" />
props.active[idx] ? <IoCheckmark className="inline text-xl"/> :
<IoCloseSharp className="inline text-xl"/>
}
<span>{label}</span>
</li>
Expand All @@ -46,4 +47,4 @@ const PasswordRobustnessList = (props: {active: boolean[]}) => {
)
}

export default PasswordRobustnessList
export default PasswordRobustnessList
40 changes: 20 additions & 20 deletions app/authenticate/hooks/useButton.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from "react";
import { PageType } from "../page";
import {useEffect, useState} from "react";
import {PageType} from "../page";

export const useButtonMessage = (pageType: PageType, initMessage: string) => {
const [buttonMessage, setButtonMessage] = useState(initMessage);
useEffect(()=>{
useEffect(() => {
let msg = '下一步';
switch (pageType) {
case 'login':
Expand Down Expand Up @@ -50,13 +50,13 @@ export const useDisabled = (
account
} = props;
const [disabled, setDisabled] = useState(true);
useEffect(()=>{
if (pageType === 'wait-check'){
useEffect(() => {
if (pageType === 'wait-check') {
setDisabled(
!policyState || account.length === 0
)
}
if (pageType === 'login'){
if (pageType === 'login') {
setDisabled(!policyState || password.length === 0 || account.length === 0);
}
if (pageType === 'register') {
Expand All @@ -72,23 +72,23 @@ export const useDisabled = (


export const useButton = (props: {
policyState: boolean,
userName: string,
password: string,
confirmPassword: string,
checkCode: string,
valide: boolean,
passwordRobustness: boolean[],
pageType: PageType,
isEmail: boolean,
isPhone: boolean,
account: string
policyState: boolean,
userName: string,
password: string,
confirmPassword: string,
checkCode: string,
valide: boolean,
passwordRobustness: boolean[],
pageType: PageType,
isEmail: boolean,
isPhone: boolean,
account: string
}) => {
const {disabled} = useDisabled(props);
const [color, setColor] = useState<"default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined>(disabled ? 'default' : 'primary');
const {buttonMessage} = useButtonMessage(props.pageType, '下一步')
useEffect(()=>{
useEffect(() => {
setColor(disabled ? 'default' : 'primary')
}, [disabled]);
return {color,disabled, buttonMessage};
}
return {color, disabled, buttonMessage};
}
51 changes: 25 additions & 26 deletions app/authenticate/hooks/useCountDown.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import React, { useState, useEffect } from "react";
import {useEffect, useState} from "react";

export function useCountDown(ms: number):[number, ()=>void,()=>void,()=>void] {
const [time, setTime] = useState(ms);
const [isRunning, setIsRunning] = useState(false);
useEffect(() => {
if (isRunning && time > 0) {
const timerId = setTimeout(() => {
setTime(time - 1);
}, 1000);
return () => {
clearTimeout(timerId);
};
export function useCountDown(ms: number): [number, () => void, () => void, () => void] {
const [time, setTime] = useState(ms);
const [isRunning, setIsRunning] = useState(false);
useEffect(() => {
if (isRunning && time > 0) {
const timerId = setTimeout(() => {
setTime(time - 1);
}, 1000);
return () => {
clearTimeout(timerId);
};
} else {
setIsRunning(false);
}
}, [isRunning, time]);
const start = () => {
setIsRunning(true)
}
else {
setIsRunning(false);
const pause = () => {
setIsRunning(false);
}
}, [isRunning, time]);
const start = () => {
setIsRunning(true)
}
const pause = ()=>{
setIsRunning(false);
}
const reset = () => {
setTime(ms);
setIsRunning(false);
}
return [time, start, pause, reset];
const reset = () => {
setTime(ms);
setIsRunning(false);
}
return [time, start, pause, reset];
}
20 changes: 10 additions & 10 deletions app/authenticate/hooks/useType.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { SetStateAction, useEffect, useState } from "react"
import React, {SetStateAction, useEffect, useState} from "react"

export const useType = (
isPhone: boolean,
isEmail: boolean
):['email'|'phone'|'unknown', React.Dispatch<SetStateAction<'email'|'phone'|'unknown'>>] => {
const [type,setType] = useState<'email'|'phone'|'unknown'>('email')
useEffect(()=>{
if (!isPhone && !isEmail){
): ['email' | 'phone' | 'unknown', React.Dispatch<SetStateAction<'email' | 'phone' | 'unknown'>>] => {
const [type, setType] = useState<'email' | 'phone' | 'unknown'>('email')
useEffect(() => {
if (!isPhone && !isEmail) {
setType('unknown');
return;
}
if (isPhone){
if (isPhone) {
setType('phone');
return;
}
if (isEmail){
if (isEmail) {
setType('email');
return ;
return;
}
},[isPhone, isEmail]);
}, [isPhone, isEmail]);
return [type, setType]
}
}
23 changes: 11 additions & 12 deletions app/authenticate/hooks/useValid.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { useMemo } from "react"
import {useMemo} from "react"

export const useIsEmail = (userInput: string) => {
const validateEmail = (val: string)=>val.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,10}$/i);
return useMemo(()=>{
if (!userInput){
const validateEmail = (val: string) => val.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,10}$/i);
return useMemo(() => {
if (!userInput) {
return false;
}
return validateEmail(userInput) ? true : false;
return !!validateEmail(userInput);
}, [userInput]);
}

export const useIsPhone = (userInput: string) => {
const validatePhone = (val:string)=>val.match(/^1[3-9]\d{9}$/i);
const isPhone = useMemo(()=>{
if (!userInput){
const validatePhone = (val: string) => val.match(/^1[3-9]\d{9}$/i);
return useMemo(() => {
if (!userInput) {
return false;
}
return validatePhone(userInput) ? true : false;
}, [userInput])
return isPhone;
}
return !!validatePhone(userInput);
}, [userInput]);
}
Loading

0 comments on commit 12574cd

Please sign in to comment.