AddUser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, { useState, useRef } from 'react';
  2. import { API, isMobile, showError, showSuccess } from '../../helpers';
  3. import {
  4. Button,
  5. SideSheet,
  6. Space,
  7. Spin,
  8. Typography,
  9. Card,
  10. Tag,
  11. Avatar,
  12. Form,
  13. Row,
  14. Col,
  15. } from '@douyinfe/semi-ui';
  16. import {
  17. IconSave,
  18. IconClose,
  19. IconUserAdd,
  20. } from '@douyinfe/semi-icons';
  21. import { useTranslation } from 'react-i18next';
  22. const { Text, Title } = Typography;
  23. const AddUser = (props) => {
  24. const { t } = useTranslation();
  25. const formApiRef = useRef(null);
  26. const [loading, setLoading] = useState(false);
  27. const getInitValues = () => ({
  28. username: '',
  29. display_name: '',
  30. password: '',
  31. remark: '',
  32. });
  33. const submit = async (values) => {
  34. setLoading(true);
  35. const res = await API.post(`/api/user/`, values);
  36. const { success, message } = res.data;
  37. if (success) {
  38. showSuccess(t('用户账户创建成功!'));
  39. formApiRef.current?.setValues(getInitValues());
  40. props.refresh();
  41. props.handleClose();
  42. } else {
  43. showError(message);
  44. }
  45. setLoading(false);
  46. };
  47. const handleCancel = () => {
  48. props.handleClose();
  49. };
  50. return (
  51. <>
  52. <SideSheet
  53. placement={'left'}
  54. title={
  55. <Space>
  56. <Tag color="green" shape="circle">{t('新建')}</Tag>
  57. <Title heading={4} className="m-0">
  58. {t('添加用户')}
  59. </Title>
  60. </Space>
  61. }
  62. bodyStyle={{ padding: '0' }}
  63. visible={props.visible}
  64. width={isMobile() ? '100%' : 600}
  65. footer={
  66. <div className="flex justify-end bg-white">
  67. <Space>
  68. <Button
  69. theme="solid"
  70. onClick={() => formApiRef.current?.submitForm()}
  71. icon={<IconSave />}
  72. loading={loading}
  73. >
  74. {t('提交')}
  75. </Button>
  76. <Button
  77. theme="light"
  78. type="primary"
  79. onClick={handleCancel}
  80. icon={<IconClose />}
  81. >
  82. {t('取消')}
  83. </Button>
  84. </Space>
  85. </div>
  86. }
  87. closeIcon={null}
  88. onCancel={() => handleCancel()}
  89. >
  90. <Spin spinning={loading}>
  91. <Form
  92. initValues={getInitValues()}
  93. getFormApi={(api) => formApiRef.current = api}
  94. onSubmit={submit}
  95. onSubmitFail={(errs) => {
  96. const first = Object.values(errs)[0];
  97. if (first) showError(Array.isArray(first) ? first[0] : first);
  98. formApiRef.current?.scrollToError();
  99. }}
  100. >
  101. <div className="p-2">
  102. <Card className="!rounded-2xl shadow-sm border-0">
  103. <div className="flex items-center mb-2">
  104. <Avatar size="small" color="blue" className="mr-2 shadow-md">
  105. <IconUserAdd size={16} />
  106. </Avatar>
  107. <div>
  108. <Text className="text-lg font-medium">{t('用户信息')}</Text>
  109. <div className="text-xs text-gray-600">{t('创建新用户账户')}</div>
  110. </div>
  111. </div>
  112. <Row gutter={12}>
  113. <Col span={24}>
  114. <Form.Input
  115. field='username'
  116. label={t('用户名')}
  117. placeholder={t('请输入用户名')}
  118. rules={[{ required: true, message: t('请输入用户名') }]}
  119. showClear
  120. />
  121. </Col>
  122. <Col span={24}>
  123. <Form.Input
  124. field='display_name'
  125. label={t('显示名称')}
  126. placeholder={t('请输入显示名称')}
  127. showClear
  128. />
  129. </Col>
  130. <Col span={24}>
  131. <Form.Input
  132. field='password'
  133. label={t('密码')}
  134. type='password'
  135. placeholder={t('请输入密码')}
  136. rules={[{ required: true, message: t('请输入密码') }]}
  137. showClear
  138. />
  139. </Col>
  140. <Col span={24}>
  141. <Form.Input
  142. field='remark'
  143. label={t('备注')}
  144. placeholder={t('请输入备注(仅管理员可见)')}
  145. showClear
  146. />
  147. </Col>
  148. </Row>
  149. </Card>
  150. </div>
  151. </Form>
  152. </Spin>
  153. </SideSheet>
  154. </>
  155. );
  156. };
  157. export default AddUser;