From 2193ff4bcb77431f0bd71ae960de0bf03c179178 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 3 Sep 2023 02:52:41 +0800 Subject: [PATCH] feat: login view --- src/client/hooks/useEvent.ts | 35 ++++++++++++++++++++++++++++++++ src/client/pages/Login.tsx | 39 +++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/client/hooks/useEvent.ts diff --git a/src/client/hooks/useEvent.ts b/src/client/hooks/useEvent.ts new file mode 100644 index 0000000..2e1c525 --- /dev/null +++ b/src/client/hooks/useEvent.ts @@ -0,0 +1,35 @@ +import { useMemo, useRef } from 'react'; + +// From https://github.com/alibaba/hooks/blob/master/packages/hooks/src/useMemoizedFn/index.ts + +type Noop = (this: any, ...args: any[]) => any; + +type PickFunction = ( + this: ThisParameterType, + ...args: Parameters +) => ReturnType; + +export function useEvent(fn: T) { + if (process.env.NODE_ENV === 'development') { + if (typeof fn !== 'function') { + console.error( + `useMemoizedFn expected parameter is a function, got ${typeof fn}` + ); + } + } + + const fnRef = useRef(fn); + + // why not write `fnRef.current = fn`? + // https://github.com/alibaba/hooks/issues/728 + fnRef.current = useMemo(() => fn, [fn]); + + const memoizedFn = useRef>(); + if (!memoizedFn.current) { + memoizedFn.current = function (this, ...args) { + return fnRef.current.apply(this, args); + }; + } + + return memoizedFn.current as T; +} diff --git a/src/client/pages/Login.tsx b/src/client/pages/Login.tsx index 7c02596..4f1814d 100644 --- a/src/client/pages/Login.tsx +++ b/src/client/pages/Login.tsx @@ -1,6 +1,43 @@ +import { Button, Form, Input, Typography } from 'antd'; import React from 'react'; +import { useEvent } from '../hooks/useEvent'; +import axios from 'axios'; export const Login: React.FC = React.memo(() => { - return
Login
; + const handleLogin = useEvent(async (values: any) => { + await axios.post('/api/user/login', { + username: values.username, + password: values.password, + }); + }); + + return ( +
+
+ Tianji +
+ + + + + + + + + +
+
+
+ ); }); Login.displayName = 'Login';