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

108 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-03-15 16:05:33 +08:00
import React, { useState } from 'react';
import { API, isMobile, showError, showSuccess } from '../../helpers';
import Title from '@douyinfe/semi-ui/lib/es/typography/title';
import { Button, Input, SideSheet, Space, Spin } from '@douyinfe/semi-ui';
2023-04-22 20:39:27 +08:00
2023-11-27 22:43:46 +08:00
const AddUser = (props) => {
2024-03-15 16:05:33 +08:00
const originInputs = {
username: '',
display_name: '',
2024-03-23 21:24:39 +08:00
password: '',
2024-03-15 16:05:33 +08:00
};
const [inputs, setInputs] = useState(originInputs);
const [loading, setLoading] = useState(false);
const { username, display_name, password } = inputs;
2023-04-22 20:39:27 +08:00
2024-03-15 16:05:33 +08:00
const handleInputChange = (name, value) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
2023-04-22 20:39:27 +08:00
2024-03-15 16:05:33 +08:00
const submit = async () => {
setLoading(true);
if (inputs.username === '' || inputs.password === '') return;
const res = await API.post(`/api/user/`, inputs);
const { success, message } = res.data;
if (success) {
showSuccess('用户账户创建成功!');
setInputs(originInputs);
props.refresh();
props.handleClose();
} else {
showError(message);
2023-04-22 20:39:27 +08:00
}
2024-03-15 16:05:33 +08:00
setLoading(false);
};
2023-04-22 20:39:27 +08:00
2024-03-15 16:05:33 +08:00
const handleCancel = () => {
props.handleClose();
};
return (
<>
<SideSheet
placement={'left'}
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>
2024-03-15 16:05:33 +08:00
</Space>
</div>
}
closeIcon={null}
onCancel={() => handleCancel()}
width={isMobile() ? '100%' : 600}
>
<Spin spinning={loading}>
<Input
style={{ marginTop: 20 }}
2024-03-23 21:24:39 +08:00
label='用户名'
name='username'
2024-03-15 16:05:33 +08:00
addonBefore={'用户名'}
placeholder={'请输入用户名'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('username', value)}
2024-03-15 16:05:33 +08:00
value={username}
2024-03-23 21:24:39 +08:00
autoComplete='off'
2024-03-15 16:05:33 +08:00
/>
<Input
style={{ marginTop: 20 }}
addonBefore={'显示名'}
2024-03-23 21:24:39 +08:00
label='显示名称'
name='display_name'
autoComplete='off'
2024-03-15 16:05:33 +08:00
placeholder={'请输入显示名称'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('display_name', value)}
2024-03-15 16:05:33 +08:00
value={display_name}
/>
<Input
style={{ marginTop: 20 }}
2024-03-23 21:24:39 +08:00
label='密 码'
name='password'
2024-03-15 16:05:33 +08:00
type={'password'}
addonBefore={'密码'}
placeholder={'请输入密码'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('password', value)}
2024-03-15 16:05:33 +08:00
value={password}
2024-03-23 21:24:39 +08:00
autoComplete='off'
2024-03-15 16:05:33 +08:00
/>
</Spin>
</SideSheet>
</>
);
2023-04-22 20:39:27 +08:00
};
export default AddUser;