AddUser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useState } from 'react';
  2. import { API, isMobile, showError, showSuccess } from '../../helpers';
  3. import Title from '@douyinfe/semi-ui/lib/es/typography/title';
  4. import { Button, Input, SideSheet, Space, Spin } from '@douyinfe/semi-ui';
  5. const AddUser = (props) => {
  6. const originInputs = {
  7. username: '',
  8. display_name: '',
  9. password: '',
  10. };
  11. const [inputs, setInputs] = useState(originInputs);
  12. const [loading, setLoading] = useState(false);
  13. const { username, display_name, password } = inputs;
  14. const handleInputChange = (name, value) => {
  15. setInputs((inputs) => ({ ...inputs, [name]: value }));
  16. };
  17. const submit = async () => {
  18. setLoading(true);
  19. if (inputs.username === '' || inputs.password === '') return;
  20. const res = await API.post(`/api/user/`, inputs);
  21. const { success, message } = res.data;
  22. if (success) {
  23. showSuccess('用户账户创建成功!');
  24. setInputs(originInputs);
  25. props.refresh();
  26. props.handleClose();
  27. } else {
  28. showError(message);
  29. }
  30. setLoading(false);
  31. };
  32. const handleCancel = () => {
  33. props.handleClose();
  34. };
  35. return (
  36. <>
  37. <SideSheet
  38. placement={'left'}
  39. title={<Title level={3}>{'添加用户'}</Title>}
  40. headerStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
  41. bodyStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
  42. visible={props.visible}
  43. footer={
  44. <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
  45. <Space>
  46. <Button theme='solid' size={'large'} onClick={submit}>
  47. 提交
  48. </Button>
  49. <Button
  50. theme='solid'
  51. size={'large'}
  52. type={'tertiary'}
  53. onClick={handleCancel}
  54. >
  55. 取消
  56. </Button>
  57. </Space>
  58. </div>
  59. }
  60. closeIcon={null}
  61. onCancel={() => handleCancel()}
  62. width={isMobile() ? '100%' : 600}
  63. >
  64. <Spin spinning={loading}>
  65. <Input
  66. style={{ marginTop: 20 }}
  67. label='用户名'
  68. name='username'
  69. addonBefore={'用户名'}
  70. placeholder={'请输入用户名'}
  71. onChange={(value) => handleInputChange('username', value)}
  72. value={username}
  73. autoComplete='off'
  74. />
  75. <Input
  76. style={{ marginTop: 20 }}
  77. addonBefore={'显示名'}
  78. label='显示名称'
  79. name='display_name'
  80. autoComplete='off'
  81. placeholder={'请输入显示名称'}
  82. onChange={(value) => handleInputChange('display_name', value)}
  83. value={display_name}
  84. />
  85. <Input
  86. style={{ marginTop: 20 }}
  87. label='密 码'
  88. name='password'
  89. type={'password'}
  90. addonBefore={'密码'}
  91. placeholder={'请输入密码'}
  92. onChange={(value) => handleInputChange('password', value)}
  93. value={password}
  94. autoComplete='off'
  95. />
  96. </Spin>
  97. </SideSheet>
  98. </>
  99. );
  100. };
  101. export default AddUser;