fix(admin): remove redundant confirmations and repair code access
This commit is contained in:
@@ -7,7 +7,6 @@ import { App, Button, Card, Form, Input, Modal, Select, Space, Table, Tag, Typog
|
||||
import { useState } from "react";
|
||||
|
||||
import { adminRequestJson, type AdminIdentity } from "@/lib/admin/providers";
|
||||
import { ConfirmActionModal } from "./confirm-action-modal";
|
||||
import { formatAdminDate } from "./resource-table";
|
||||
|
||||
const roleOptions = [
|
||||
@@ -30,12 +29,11 @@ interface Administrator {
|
||||
}
|
||||
|
||||
type AssignmentForm = { email: string; roleCode: RoleCode };
|
||||
type PendingRoleAction = {
|
||||
type RoleAction = {
|
||||
action: "assign" | "revoke";
|
||||
roleCode: RoleCode;
|
||||
userId?: string;
|
||||
email?: string;
|
||||
label: string;
|
||||
};
|
||||
|
||||
export default function AdministratorsResource() {
|
||||
@@ -50,7 +48,6 @@ export default function AdministratorsResource() {
|
||||
const [assignmentForm] = Form.useForm<AssignmentForm>();
|
||||
const [assignmentOpen, setAssignmentOpen] = useState(false);
|
||||
const [assignmentUser, setAssignmentUser] = useState<Administrator | null>(null);
|
||||
const [pendingAction, setPendingAction] = useState<PendingRoleAction | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const canManage = Boolean(identity?.permissions.includes("admin.users.manage_roles"));
|
||||
|
||||
@@ -60,34 +57,32 @@ export default function AdministratorsResource() {
|
||||
setAssignmentOpen(true);
|
||||
}
|
||||
|
||||
function prepareAssignment(values: AssignmentForm) {
|
||||
const role = roleOptions.find((item) => item.value === values.roleCode)!;
|
||||
setPendingAction({
|
||||
async function assignRole(values: AssignmentForm) {
|
||||
await submitRoleAction({
|
||||
action: "assign",
|
||||
roleCode: values.roleCode,
|
||||
...(assignmentUser ? { userId: assignmentUser.id } : { email: values.email.trim() }),
|
||||
label: `${assignmentUser?.email ?? values.email.trim()} · ${role.label}`,
|
||||
});
|
||||
}
|
||||
|
||||
async function submitRoleAction() {
|
||||
if (!pendingAction) return;
|
||||
async function submitRoleAction(action: RoleAction) {
|
||||
setSaving(true);
|
||||
try {
|
||||
await adminRequestJson("/api/admin/administrators", {
|
||||
method: pendingAction.action === "assign" ? "POST" : "DELETE",
|
||||
method: action.action === "assign" ? "POST" : "DELETE",
|
||||
body: JSON.stringify({
|
||||
userId: pendingAction.userId,
|
||||
email: pendingAction.email,
|
||||
roleCode: pendingAction.roleCode,
|
||||
userId: action.userId,
|
||||
email: action.email,
|
||||
roleCode: action.roleCode,
|
||||
}),
|
||||
});
|
||||
message.success(pendingAction.action === "assign" ? "管理员角色已分配" : "管理员角色已撤销");
|
||||
setPendingAction(null);
|
||||
message.success(action.action === "assign" ? "管理员角色已分配" : "管理员角色已撤销");
|
||||
setAssignmentOpen(false);
|
||||
setAssignmentUser(null);
|
||||
assignmentForm.resetFields();
|
||||
await tableQuery.refetch();
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : "管理员角色操作失败");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
@@ -121,7 +116,7 @@ export default function AdministratorsResource() {
|
||||
closable={canManage}
|
||||
onClose={(event) => {
|
||||
event.preventDefault();
|
||||
setPendingAction({ action: "revoke", roleCode: role, userId: item.id, label: `${item.email} · ${role}` });
|
||||
void submitRoleAction({ action: "revoke", roleCode: role, userId: item.id });
|
||||
}}
|
||||
>{role}</Tag>)}</Space>,
|
||||
},
|
||||
@@ -132,13 +127,14 @@ export default function AdministratorsResource() {
|
||||
<Modal
|
||||
title={assignmentUser ? `为 ${assignmentUser.email} 分配角色` : "按邮箱分配管理员角色"}
|
||||
open={assignmentOpen}
|
||||
okText="继续"
|
||||
okText="分配"
|
||||
cancelText="取消"
|
||||
confirmLoading={saving}
|
||||
onOk={() => assignmentForm.submit()}
|
||||
onCancel={() => setAssignmentOpen(false)}
|
||||
destroyOnHidden
|
||||
>
|
||||
<Form<AssignmentForm> form={assignmentForm} layout="vertical" onFinish={prepareAssignment}>
|
||||
<Form<AssignmentForm> form={assignmentForm} layout="vertical" onFinish={(values) => void assignRole(values)}>
|
||||
<Form.Item name="email" label="用户邮箱" rules={[{ required: true }, { type: "email" }]}>
|
||||
<Input disabled={Boolean(assignmentUser)} autoComplete="email" />
|
||||
</Form.Item>
|
||||
@@ -150,15 +146,6 @@ export default function AdministratorsResource() {
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<ConfirmActionModal
|
||||
open={Boolean(pendingAction)}
|
||||
title={pendingAction?.action === "revoke" ? `撤销角色:${pendingAction.label}` : `分配角色:${pendingAction?.label ?? ""}`}
|
||||
okText={pendingAction?.action === "revoke" ? "确认撤销" : "确认分配"}
|
||||
danger={pendingAction?.action === "revoke"}
|
||||
confirmLoading={saving}
|
||||
onCancel={() => setPendingAction(null)}
|
||||
onConfirm={submitRoleAction}
|
||||
/>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user