fix(admin): restore teacher profile form values

This commit is contained in:
2026-07-13 12:01:38 +08:00
parent 337d25e370
commit 8cadf8970e
3 changed files with 49 additions and 2 deletions

View File

@@ -15,6 +15,10 @@ import dayjs from 'dayjs';
import api from '../../api';
import PermissionButton from '../../components/PermissionButton';
import { message } from '../../ui/app-message';
import {
userProfileResponseToFormValues,
type UserProfileResponse,
} from './user-profile-form';
const UsersPage: React.FC = () => {
const [data, setData] = useState<any[]>([]);
@@ -36,8 +40,8 @@ const UsersPage: React.FC = () => {
const handleOpenProfile = async (record: any) => {
setProfileUser(record);
try {
const res: any = await api.get(`/rbac/users/${record.id}/profile`);
profileForm.setFieldsValue(res);
const res = await api.get<UserProfileResponse>(`/rbac/users/${record.id}/profile`);
profileForm.setFieldsValue(userProfileResponseToFormValues(res));
} catch {
profileForm.setFieldsValue({});
}

View File

@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { userProfileResponseToFormValues } from './user-profile-form';
describe('user profile form mapping', () => {
it('unwraps the nested profile returned by the user profile endpoint', () => {
expect(
userProfileResponseToFormValues({
id: 9,
username: 'teacher01',
name: '测试教师',
profile: {
joinedAt: '2026-07-01',
qualifications: '教师资格证',
subjects: ['语文', '历史'],
},
}),
).toEqual({
joinedAt: '2026-07-01',
qualifications: '教师资格证',
subjects: ['语文', '历史'],
});
});
it('returns empty form values when the user has no profile', () => {
expect(userProfileResponseToFormValues({ profile: null })).toEqual({});
});
});

View File

@@ -0,0 +1,16 @@
export interface UserProfileFormValues {
joinedAt?: string;
qualifications?: string;
subjects?: string[];
}
export interface UserProfileResponse {
id?: number;
username?: string;
name?: string;
profile?: UserProfileFormValues | null;
}
export const userProfileResponseToFormValues = (
response: UserProfileResponse,
): UserProfileFormValues => response.profile || {};