UserFormPage.tsx
6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { Alert, Button, Card, Form, Result, Space, Spin, message } from 'antd';
import { usersApi } from '../../api/users';
import type { CreateUserReq, UpdateUserReq, UserDetail } from '../../api/users';
import { BizError, isBizError } from '../../api/errors';
import { ERROR_MESSAGES } from './usersConstants';
import UserFormFields from './UserFormFields';
import UserPermissionPanel from './UserPermissionPanel';
interface Props {
mode: 'create' | 'edit';
}
interface FormValues {
username?: string;
userCode?: string;
userType?: 'NORMAL' | 'SUPER_ADMIN';
language?: 'zh-CN' | 'en-US' | 'zh-TW';
canEditDocument?: boolean;
employeeId?: number;
}
export default function UserFormPage({ mode }: Props) {
const navigate = useNavigate();
const params = useParams<{ userId?: string }>();
const userId = params.userId ? Number(params.userId) : undefined;
const [form] = Form.useForm<FormValues>();
const [loadingInitial, setLoadingInitial] = useState(mode === 'edit');
const [submitting, setSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [notFound, setNotFound] = useState(false);
const [permissionCategoryIds, setPermissionCategoryIds] = useState<number[]>([]);
const [originalDetail, setOriginalDetail] = useState<UserDetail | null>(null);
useEffect(() => {
if (mode !== 'edit' || userId == null) return;
let cancelled = false;
(async () => {
try {
const detail = await usersApi.get(userId);
if (cancelled) return;
setOriginalDetail(detail);
form.setFieldsValue({
username: detail.username,
userCode: detail.userCode,
userType: detail.userType,
language: detail.language as FormValues['language'],
canEditDocument: false, // detail VO 当前不返回,默认 false
employeeId: detail.employeeId ?? undefined,
});
setPermissionCategoryIds(detail.permissionCategoryIds ?? []);
} catch (e) {
if (cancelled) return;
if (isBizError(e) && e.code === 40401) {
setNotFound(true);
} else if (isBizError(e)) {
setErrorMessage(e.message || (ERROR_MESSAGES.UNKNOWN as string));
} else {
setErrorMessage(ERROR_MESSAGES.UNKNOWN as string);
}
} finally {
if (!cancelled) setLoadingInitial(false);
}
})();
return () => {
cancelled = true;
};
}, [mode, userId, form]);
const handleSubmit = async (values: FormValues) => {
setSubmitting(true);
setErrorMessage(null);
form.setFields([
{ name: 'username', errors: [] },
{ name: 'userCode', errors: [] },
]);
try {
if (mode === 'create') {
await usersApi.create({
username: values.username!,
userCode: values.userCode!,
userType: values.userType!,
language: values.language!,
canEditDocument: !!values.canEditDocument,
employeeId: values.employeeId,
permissionCategoryIds,
});
message.success('新增用户成功');
} else if (userId != null) {
const patch: UpdateUserReq = {
userCode: values.userCode,
userType: values.userType,
language: values.language,
canEditDocument: values.canEditDocument,
employeeId: values.employeeId,
permissionCategoryIds,
};
await usersApi.update(userId, patch);
message.success('保存成功');
}
navigate('/users');
} catch (e) {
handleBizError(e);
} finally {
setSubmitting(false);
}
};
const handleBizError = (e: unknown) => {
if (!isBizError(e)) {
setErrorMessage(ERROR_MESSAGES.UNKNOWN as string);
return;
}
const be = e as BizError;
if (be.code === 40901) {
form.setFields([{ name: 'username', errors: [ERROR_MESSAGES[40901] as string] }]);
} else if (be.code === 40902) {
form.setFields([{ name: 'userCode', errors: [ERROR_MESSAGES[40902] as string] }]);
} else if (be.code === 40004) {
setErrorMessage(ERROR_MESSAGES[40004] as string);
} else if (be.code === 40401) {
setNotFound(true);
} else if (be.code === -1) {
setErrorMessage(ERROR_MESSAGES.NETWORK as string);
} else {
setErrorMessage(be.message || (ERROR_MESSAGES.UNKNOWN as string));
}
};
if (notFound) {
return (
<div data-testid="user-not-found">
<Result
status="404"
title="用户不存在"
extra={
<Button type="primary" onClick={() => navigate('/users')}>
返回列表
</Button>
}
/>
</div>
);
}
return (
<div data-testid={mode === 'create' ? 'user-form-create' : 'user-form-edit'} style={{ padding: 16 }}>
<Space style={{ marginBottom: 12 }}>
<Button
type="primary"
loading={submitting}
onClick={() => form.submit()}
data-testid="form-save"
>
保存
</Button>
<Button onClick={() => navigate('/users')} data-testid="form-cancel">
取消
</Button>
</Space>
{errorMessage && (
<Alert
type="error"
message={errorMessage}
showIcon
style={{ marginBottom: 12 }}
data-testid="form-error-alert"
/>
)}
<Spin spinning={loadingInitial}>
<Card>
<Form<FormValues>
form={form}
layout="vertical"
onFinish={handleSubmit}
initialValues={{
userType: 'NORMAL',
language: 'zh-CN',
canEditDocument: false,
}}
disabled={submitting || loadingInitial}
data-testid="user-form"
>
<UserFormFields mode={mode} disabled={submitting || loadingInitial} />
</Form>
<UserPermissionPanel
value={permissionCategoryIds}
onChange={setPermissionCategoryIds}
disabled={submitting || loadingInitial}
/>
</Card>
</Spin>
</div>
);
}