Browse Source

feat 增加测试用组件

main
谢亚昕 5 months ago
parent
commit
8e4d241014
  1. 2
      src/api/index.ts
  2. 8
      src/layout/base.tsx
  3. 26
      src/ui/Login/Login.tsx
  4. 12
      src/ui/Register/Register.tsx
  5. 62
      src/views/Home/index.tsx

2
src/api/index.ts

@ -4,7 +4,7 @@ const client = new Client()
client.setProject(import.meta.env.VUE_APPWRITE_PROJECT_ID)
export async function register(opts: { email: string; password: string }, errText: string) {
export async function apiRegister(opts: { email: string; password: string }, errText: string) {
const account = new Account(client)
try {
await account.create(ID.unique(), opts.email, opts.password)

8
src/layout/base.tsx

@ -6,6 +6,7 @@ import { GlobalStyles } from "./GlobalStyles"
import { Notice } from "@/ui/Notice"
import { Markdown } from "@/ui/Markdown"
import { Register } from "@/ui/Register/Register"
import { Login } from "@/ui/Login/Login"
interface PageProps {
children: ReactNode
@ -28,6 +29,7 @@ function BaseLayout(props: PageProps) {
const [isOpen, setIsOpen] = useState(false)
const [isOpenAbout, setIsOpenAbout] = useState(false)
const [isOpenRegister, setIsOpenRegister] = useState(false)
const [isOpenLogin, setIsOpenLogin] = useState(false)
const router = useHistory()
@ -79,6 +81,7 @@ function BaseLayout(props: PageProps) {
<Notice />
</Navbar.Group>
<Navbar.Group align={Alignment.RIGHT}>
<Button icon="person" className="bp5-minimal" text="登录" onClick={() => setIsOpenLogin(true)} />
<Button icon="person" className="bp5-minimal" text="注册" onClick={() => setIsOpenRegister(true)} />
<Navbar.Divider />
{/* <NavLink to={"/about"}> */}
@ -111,6 +114,11 @@ function BaseLayout(props: PageProps) {
}}
</Register>
</Dialog>
<Dialog isOpen={isOpenLogin} title="注册" icon="info-sign" onClose={() => setIsOpenLogin(false)}>
<div style={{padding: "20px"}}>
<Login></Login>
</div>
</Dialog>
</PageWrapper>
)
}

26
src/ui/Login/Login.tsx

@ -0,0 +1,26 @@
import { SubmitHandler, useForm } from "react-hook-form";
interface IProps {
}
type Inputs = {
email: string,
};
export function Login(props: IProps) {
const { register, handleSubmit, watch, formState: { errors } } = useForm<Inputs>();
const onSubmit: SubmitHandler<Inputs> = data => console.log(data);
console.log(watch("email"));
return <>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("email", { required: true })} />
{errors.email && <span>This field is required</span>}
<input type="submit" />
</form>
</>
}

12
src/ui/Register/Register.tsx

@ -1,8 +1,9 @@
// import { Tag } from "@blueprintjs/core";
import { register } from "@/api"
import { apiRegister } from "@/api"
import { Button, FormGroup, InputGroup, Intent, Tooltip } from "@blueprintjs/core"
import { FC, ReactNode, useCallback, useEffect, useRef, useState } from "react"
import { FC, FormEvent, ReactNode, useCallback, useEffect, useRef, useState } from "react"
import { useForm } from "react-hook-form"
import { toast } from "react-toastify"
interface IProps {
@ -20,7 +21,8 @@ export function Register({ onSuccess, children }: IProps) {
const [password, setPassword] = useState("")
const [repeatPassword, setRepeatPassword] = useState("")
const clickRegister = useCallback(async () => {
const clickRegister = useCallback(async (e: FormEvent) => {
e.preventDefault()
if (isLoading) {
toast.info("正在请求中")
return
@ -31,7 +33,7 @@ export function Register({ onSuccess, children }: IProps) {
if (repeatPassword !== password) return toast.error("两次输入的密码不一致")
setIsLoading(true)
try {
await toast.promise(register({ email, password }, "注册失败"), {
await toast.promise(apiRegister({ email, password }, "注册失败"), {
pending: '注册中',
success: '注册成功',
error: '注册失败'
@ -61,7 +63,7 @@ export function Register({ onSuccess, children }: IProps) {
return (
<>
<form onSubmit={() => false} onSubmitCapture={clickRegister}>
<form onSubmitCapture={clickRegister}>
<WrapperComp>
<FormGroup helperText="请输入一个可用的邮箱" label="邮箱" labelFor="text-input" labelInfo="(必须)">
<InputGroup

62
src/views/Home/index.tsx

@ -2,59 +2,31 @@ import withPage from "@/base/withPage"
import { Hero } from "@/ui/Hero"
import { Button } from "@blueprintjs/core"
import { ReactNode, useCallback } from "react"
import { Fly } from "@/plugins/request"
// import { Fly } from "@/plugins/request"
import { toast } from "react-toastify"
import { useForm, SubmitHandler } from "react-hook-form";
interface IProps {
children: ReactNode
}
type Inputs = {
example: string,
exampleRequired: string,
};
// export default withPage(function Project({ }: IProps) {
// const onClick = useCallback(() => {
// toast.success("Wow so easy !")
// // Fly.invoke().request<{a: number}>({
// // method: "get",
// // url: "http://api.juheapi.com/japi/toh",
// // global: false
// // }).then((res) => console.log(res.data)).catch(console.log)
// }, [])
// return (
// <>
// <Hero></Hero>
// <div className="container">
// <Button onClick={onClick}>请求</Button>
// </div>
// </>
// )
// })
export default withPage(function Project({ }: IProps) {
const { register, handleSubmit, watch, formState: { errors } } = useForm<Inputs>();
const onSubmit: SubmitHandler<Inputs> = data => console.log(data);
console.log(watch("example")) // watch input value by passing the name of it
const onClick = useCallback(() => {
toast.success("Wow so easy !")
// Fly.invoke().request<{a: number}>({
// method: "get",
// url: "http://api.juheapi.com/japi/toh",
// global: false
// }).then((res) => console.log(res.data)).catch(console.log)
}, [])
return (
/* "handleSubmit" will validate your inputs before invoking "onSubmit" */
<form onSubmit={handleSubmit(onSubmit)}>
{/* register your input into the hook by invoking the "register" function */}
<input defaultValue="test" {...register("example")} />
{/* include validation with required or other standard HTML validation rules */}
<input {...register("exampleRequired", { required: true })} />
{/* errors will return when field validation fails */}
{errors.exampleRequired && <span>This field is required</span>}
<input type="submit" />
</form>
);
<>
<Hero></Hero>
<div className="container">
<Button onClick={onClick}></Button>
</div>
</>
)
})

Loading…
Cancel
Save