AddUser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. headerStyle={{
  63. borderBottom: '1px solid var(--semi-color-border)',
  64. padding: '24px'
  65. }}
  66. bodyStyle={{ padding: '0' }}
  67. visible={props.visible}
  68. width={isMobile() ? '100%' : 600}
  69. footer={
  70. <div className="flex justify-end bg-white">
  71. <Space>
  72. <Button
  73. theme="solid"
  74. className="!rounded-full"
  75. onClick={() => formApiRef.current?.submitForm()}
  76. icon={<IconSave />}
  77. loading={loading}
  78. >
  79. {t('提交')}
  80. </Button>
  81. <Button
  82. theme="light"
  83. className="!rounded-full"
  84. type="primary"
  85. onClick={handleCancel}
  86. icon={<IconClose />}
  87. >
  88. {t('取消')}
  89. </Button>
  90. </Space>
  91. </div>
  92. }
  93. closeIcon={null}
  94. onCancel={() => handleCancel()}
  95. >
  96. <Spin spinning={loading}>
  97. <Form
  98. initValues={getInitValues()}
  99. getFormApi={(api) => formApiRef.current = api}
  100. onSubmit={submit}
  101. onSubmitFail={(errs) => {
  102. const first = Object.values(errs)[0];
  103. if (first) showError(Array.isArray(first) ? first[0] : first);
  104. formApiRef.current?.scrollToError();
  105. }}
  106. >
  107. <div className="p-6 space-y-6">
  108. <Card className="!rounded-2xl shadow-sm border-0">
  109. <div className="flex items-center mb-2">
  110. <Avatar size="small" color="blue" className="mr-2 shadow-md">
  111. <IconUserAdd size={16} />
  112. </Avatar>
  113. <div>
  114. <Text className="text-lg font-medium">{t('用户信息')}</Text>
  115. <div className="text-xs text-gray-600">{t('创建新用户账户')}</div>
  116. </div>
  117. </div>
  118. <Row gutter={12}>
  119. <Col span={24}>
  120. <Form.Input
  121. field='username'
  122. label={t('用户名')}
  123. placeholder={t('请输入用户名')}
  124. rules={[{ required: true, message: t('请输入用户名') }]} />
  125. </Col>
  126. <Col span={24}>
  127. <Form.Input
  128. field='display_name'
  129. label={t('显示名称')}
  130. placeholder={t('请输入显示名称')} />
  131. </Col>
  132. <Col span={24}>
  133. <Form.Input
  134. field='password'
  135. label={t('密码')}
  136. type='password'
  137. placeholder={t('请输入密码')}
  138. rules={[{ required: true, message: t('请输入密码') }]} />
  139. </Col>
  140. <Col span={24}>
  141. <Form.Input
  142. field='remark'
  143. label={t('备注')}
  144. placeholder={t('请输入备注(仅管理员可见)')} />
  145. </Col>
  146. </Row>
  147. </Card>
  148. </div>
  149. </Form>
  150. </Spin>
  151. </SideSheet>
  152. </>
  153. );
  154. };
  155. export default AddUser;