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

296 lines
9.1 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-05-12 16:06:19 +08:00
import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
2024-03-15 16:05:33 +08:00
import Title from '@douyinfe/semi-ui/lib/es/typography/title';
2024-03-23 21:24:39 +08:00
import {
Button,
Divider,
Input,
2024-05-12 16:06:19 +08:00
Modal,
2024-03-23 21:24:39 +08:00
Select,
SideSheet,
Space,
Spin,
Typography,
} from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
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);
2024-05-12 16:06:19 +08:00
const [addQuotaModalOpen, setIsModalOpen] = useState(false);
2024-05-12 16:12:31 +08:00
const [addQuotaLocal, setAddQuotaLocal] = useState('');
2023-04-22 20:39:27 +08:00
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
};
2024-05-12 16:06:19 +08:00
const addLocalQuota = () => {
2024-05-12 16:12:31 +08:00
let newQuota = parseInt(quota) + parseInt(addQuotaLocal);
2024-05-12 16:06:19 +08:00
setInputs((inputs) => ({ ...inputs, quota: newQuota }));
};
const openAddQuotaModal = () => {
2024-05-12 16:12:31 +08:00
setAddQuotaLocal('0');
2024-05-12 16:06:19 +08:00
setIsModalOpen(true);
};
const { t } = useTranslation();
2023-04-22 20:39:27 +08:00
return (
<>
<SideSheet
placement={'right'}
title={<Title level={3}>{t('编辑用户')}</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}>
{t('提交')}
2024-03-23 21:24:39 +08:00
</Button>
<Button
theme='solid'
size={'large'}
type={'tertiary'}
onClick={handleCancel}
>
{t('取消')}
2024-03-23 21:24:39 +08:00
</Button>
</Space>
</div>
}
closeIcon={null}
onCancel={() => handleCancel()}
width={isMobile() ? '100%' : 600}
>
<Spin spinning={loading}>
<div style={{ marginTop: 20 }}>
<Typography.Text>{t('用户名')}</Typography.Text>
</div>
<Input
label={t('用户名')}
2024-03-23 21:24:39 +08:00
name='username'
placeholder={t('请输入新的用户名')}
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>{t('密码')}</Typography.Text>
</div>
<Input
label={t('密码')}
2024-03-23 21:24:39 +08:00
name='password'
type={'password'}
placeholder={t('请输入新的密码,最短 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>{t('显示名称')}</Typography.Text>
</div>
<Input
label={t('显示名称')}
2024-03-23 21:24:39 +08:00
name='display_name'
placeholder={t('请输入新的显示名称')}
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>{t('分组')}</Typography.Text>
2023-11-27 22:43:46 +08:00
</div>
<Select
placeholder={t('请选择分组')}
2024-03-23 21:24:39 +08:00
name='group'
fluid
search
selection
allowAdditions
additionLabel={t('请在系统设置页面编辑分组倍率以添加新的分组:')}
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>{`${t('剩余额度')}${renderQuotaWithPrompt(quota)}`}</Typography.Text>
</div>
2024-05-12 16:06:19 +08:00
<Space>
<Input
name='quota'
placeholder={t('请输入新的剩余额度')}
2024-05-12 16:06:19 +08:00
onChange={(value) => handleInputChange('quota', value)}
value={quota}
type={'number'}
autoComplete='new-password'
/>
<Button onClick={openAddQuotaModal}>{t('添加额度')}</Button>
2024-05-12 16:06:19 +08:00
</Space>
</>
2024-03-23 21:24:39 +08:00
)}
<Divider style={{ marginTop: 20 }}>{t('以下信息不可修改')}</Divider>
<div style={{ marginTop: 20 }}>
<Typography.Text>{t('已绑定的 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={t('此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改')}
readonly
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>{t('已绑定的微信账户')}</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={t('此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改')}
readonly
/>
2024-04-03 23:51:25 +08:00
<div style={{ marginTop: 20 }}>
<Typography.Text>{t('已绑定的邮箱账户')}</Typography.Text>
2024-04-03 23:51:25 +08:00
</div>
<Input
2024-04-03 23:51:25 +08:00
name='email'
value={email}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder={t('此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改')}
readonly
/>
<div style={{ marginTop: 20 }}>
<Typography.Text>{t('已绑定的Telegram账户')}</Typography.Text>
</div>
<Input
2024-04-03 23:51:25 +08:00
name='telegram_id'
value={telegram_id}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
placeholder={t('此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改')}
readonly
/>
</Spin>
</SideSheet>
2024-05-12 16:06:19 +08:00
<Modal
centered={true}
visible={addQuotaModalOpen}
onOk={() => {
addLocalQuota();
setIsModalOpen(false);
}}
onCancel={() => setIsModalOpen(false)}
closable={null}
>
<div style={{ marginTop: 20 }}>
<Typography.Text>{`${t('新额度')}${renderQuota(quota)} + ${renderQuota(addQuotaLocal)} = ${renderQuota(quota + parseInt(addQuotaLocal))}`}</Typography.Text>
2024-05-12 16:06:19 +08:00
</div>
<Input
name='addQuotaLocal'
placeholder={t('需要添加的额度(支持负数)')}
2024-05-12 16:12:31 +08:00
onChange={(value) => {
setAddQuotaLocal(value);
}}
2024-05-12 16:06:19 +08:00
value={addQuotaLocal}
type={'number'}
autoComplete='new-password'
/>
</Modal>
</>
2023-04-22 20:39:27 +08:00
);
};
export default EditUser;