feat: 添加對話

This commit is contained in:
CrescentLeaf
2025-09-25 16:51:43 +08:00
parent d26c67f06d
commit c0c6c6ed1c
4 changed files with 51 additions and 32 deletions

View File

@@ -1,11 +1,12 @@
import * as React from 'react'
import { Button, Dialog, TextField } from "mdui"
import useEventListener from "../useEventListener.ts"
import { checkApiSuccessOrSncakbar } from "../snackbar.ts"
import { checkApiSuccessOrSncakbar, snackbar } from "../snackbar.ts"
import Client from "../../api/Client.ts"
import * as CryptoJS from 'crypto-js'
import data from "../../Data.ts"
import EventBus from "../../EventBus.ts"
interface Refs {
addContactDialogRef: React.MutableRefObject<Dialog | null>
@@ -14,35 +15,27 @@ interface Refs {
export default function AddContactDialog({
addContactDialogRef,
}: Refs) {
const loginButtonRef = React.useRef<Button>(null)
const registerButtonRef = React.useRef<Button>(null)
useEventListener(registerButtonRef, 'click', () => registerDialogRef.current!.open = true)
useEventListener(loginButtonRef, 'click', async () => {
const account = loginInputAccountRef.current!.value
const password = loginInputPasswordRef.current!.value
const re = await Client.invoke("User.login", {
account: account,
password: CryptoJS.SHA256(password).toString(CryptoJS.enc.Hex),
})
if (checkApiSuccessOrSncakbar(re, "登錄失敗")) return
data.access_token = re.data!.access_token as string
data.apply()
location.reload()
})
const inputUserAccountRef = React.useRef<TextField>(null)
return (
<mdui-dialog headline="添加對話" ref={addContactDialogRef}>
<mdui-text-field label="用戶 ID / 用戶名" ref={loginInputAccountRef as any}></mdui-text-field>
<div style={{
height: "10px",
}}></div>
<mdui-text-field label="密碼" type="password" toggle-password ref={loginInputPasswordRef as any}></mdui-text-field>
, ...
<mdui-text-field style={{ marginTop: "10px", }} label="對方的 用戶 ID / 用戶名" ref={inputUserAccountRef as any}></mdui-text-field>
<mdui-button slot="action" variant="text" onClick={() => addContactDialogRef.current!.open = false}></mdui-button>
<mdui-button slot="action" variant="text"></mdui-button>
<mdui-button slot="action" variant="text" onClick={async () => {
const re = await Client.invoke("User.addContact", {
account: inputUserAccountRef.current!.value,
token: data.access_token,
})
if (checkApiSuccessOrSncakbar(re, "添加失敗")) return
snackbar({
message: "添加成功!",
placement: "top",
})
EventBus.emit('ContactsList.updateContacts')
addContactDialogRef.current!.open = false
}}></mdui-button>
</mdui-dialog>
)
}