Files
new-api/web/src/pages/User/EditUser.js

250 lines
7.4 KiB
JavaScript
Raw Normal View History

2023-04-22 20:39:27 +08:00
import React, { useEffect, useState } from 'react';
2024-03-15 16:05:33 +08:00
import { useNavigate } from 'react-router-dom';
import { API, isMobile, showError, showSuccess } from '../../helpers';
2024-03-15 16:05:33 +08:00
import { renderQuotaWithPrompt } from '../../helpers/render';
import Title from '@douyinfe/semi-ui/lib/es/typography/title';
2024-03-23 21:24:39 +08:00
import {
Button,
Divider,
Input,
Select,
SideSheet,
Space,
Spin,
Typography,
} from '@douyinfe/semi-ui';
2023-04-22 20:39:27 +08:00
2023-11-27 22:43:46 +08:00
const EditUser = (props) => {
const userId = props.editingUser.id;
2023-04-22 20:39:27 +08:00
const [loading, setLoading] = useState(true);
const [inputs, setInputs] = useState({
username: '',
display_name: '',
password: '',
github_id: '',
wechat_id: '',
email: '',
2023-05-21 10:01:02 +08:00
quota: 0,
2024-03-23 21:24:39 +08:00
group: 'default',
2023-04-22 20:39:27 +08:00
});
const [groupOptions, setGroupOptions] = useState([]);
2024-03-23 21:24:39 +08:00
const {
username,
display_name,
password,
github_id,
wechat_id,
telegram_id,
email,
quota,
group,
} = inputs;
2023-11-27 22:43:46 +08:00
const handleInputChange = (name, value) => {
2023-04-22 20:39:27 +08:00
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const fetchGroups = async () => {
try {
let res = await API.get(`/api/group/`);
2024-03-23 21:24:39 +08:00
setGroupOptions(
res.data.data.map((group) => ({
label: group,
value: group,
})),
);
} catch (error) {
showError(error.message);
}
};
const navigate = useNavigate();
const handleCancel = () => {
2023-11-27 22:43:46 +08:00
props.handleClose();
2024-03-15 16:05:33 +08:00
};
2023-04-22 20:39:27 +08:00
const loadUser = async () => {
2023-11-27 22:43:46 +08:00
setLoading(true);
2023-04-22 20:39:27 +08:00
let res = undefined;
if (userId) {
res = await API.get(`/api/user/${userId}`);
} else {
res = await API.get(`/api/user/self`);
}
const { success, message, data } = res.data;
if (success) {
data.password = '';
setInputs(data);
} else {
showError(message);
}
setLoading(false);
};
2023-11-27 22:43:46 +08:00
2023-04-22 20:39:27 +08:00
useEffect(() => {
loadUser().then();
if (userId) {
fetchGroups().then();
}
2023-11-27 22:43:46 +08:00
}, [props.editingUser.id]);
2023-04-22 20:39:27 +08:00
const submit = async () => {
2023-11-27 22:43:46 +08:00
setLoading(true);
2023-04-22 20:39:27 +08:00
let res = undefined;
if (userId) {
2023-05-21 10:01:02 +08:00
let data = { ...inputs, id: parseInt(userId) };
if (typeof data.quota === 'string') {
data.quota = parseInt(data.quota);
}
res = await API.put(`/api/user/`, data);
2023-04-22 20:39:27 +08:00
} else {
res = await API.put(`/api/user/self`, inputs);
}
const { success, message } = res.data;
if (success) {
showSuccess('用户信息更新成功!');
2023-11-27 22:43:46 +08:00
props.refresh();
props.handleClose();
2023-04-22 20:39:27 +08:00
} else {
showError(message);
}
2023-11-27 22:43:46 +08:00
setLoading(false);
2023-04-22 20:39:27 +08:00
};
return (
<>
<SideSheet
placement={'right'}
title={<Title level={3}>{'编辑用户'}</Title>}
headerStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
bodyStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
visible={props.visible}
footer={
<div style={{ display: 'flex', justifyContent: 'flex-end' }}>
<Space>
2024-03-23 21:24:39 +08:00
<Button theme='solid' size={'large'} onClick={submit}>
提交
</Button>
<Button
theme='solid'
size={'large'}
type={'tertiary'}
onClick={handleCancel}
>
取消
</Button>
</Space>
</div>
}
closeIcon={null}
onCancel={() => handleCancel()}
width={isMobile() ? '100%' : 600}
>
<Spin spinning={loading}>
<div style={{ marginTop: 20 }}>
<Typography.Text>用户名</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
label='用户名'
name='username'
placeholder={'请输入新的用户名'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('username', value)}
value={username}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>密码</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
label='密码'
name='password'
type={'password'}
placeholder={'请输入新的密码,最短 8 位'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('password', value)}
value={password}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>显示名称</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
label='显示名称'
name='display_name'
placeholder={'请输入新的显示名称'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('display_name', value)}
value={display_name}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
/>
2024-03-23 21:24:39 +08:00
{userId && (
<>
<div style={{ marginTop: 20 }}>
<Typography.Text>分组</Typography.Text>
2023-11-27 22:43:46 +08:00
</div>
<Select
placeholder={'请选择分组'}
2024-03-23 21:24:39 +08:00
name='group'
fluid
search
selection
allowAdditions
additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('group', value)}
value={inputs.group}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
optionList={groupOptions}
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>{`剩余额度${renderQuotaWithPrompt(quota)}`}</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
name='quota'
placeholder={'请输入新的剩余额度'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('quota', value)}
value={quota}
type={'number'}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
/>
</>
2024-03-23 21:24:39 +08:00
)}
<Divider style={{ marginTop: 20 }}>以下信息不可修改</Divider>
<div style={{ marginTop: 20 }}>
<Typography.Text>已绑定的 GitHub 账户</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
name='github_id'
value={github_id}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readonly
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>已绑定的微信账户</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
name='wechat_id'
value={wechat_id}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readonly
/>
<Input
2024-03-23 21:24:39 +08:00
name='telegram_id'
value={telegram_id}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readonly
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>已绑定的邮箱账户</Typography.Text>
</div>
<Input
2024-03-23 21:24:39 +08:00
name='email'
value={email}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readonly
/>
</Spin>
</SideSheet>
</>
2023-04-22 20:39:27 +08:00
);
};
export default EditUser;