EditUser.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Header, Segment } from 'semantic-ui-react';
  3. import { useParams, useNavigate } from 'react-router-dom';
  4. import { API, showError, showSuccess } from '../../helpers';
  5. import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
  6. const EditUser = () => {
  7. const params = useParams();
  8. const userId = params.id;
  9. const [loading, setLoading] = useState(true);
  10. const [inputs, setInputs] = useState({
  11. username: '',
  12. display_name: '',
  13. password: '',
  14. github_id: '',
  15. wechat_id: '',
  16. email: '',
  17. quota: 0,
  18. group: 'default'
  19. });
  20. const [groupOptions, setGroupOptions] = useState([]);
  21. const { username, display_name, password, github_id, wechat_id, email, quota, group } =
  22. inputs;
  23. const handleInputChange = (e, { name, value }) => {
  24. setInputs((inputs) => ({ ...inputs, [name]: value }));
  25. };
  26. const fetchGroups = async () => {
  27. try {
  28. let res = await API.get(`/api/group/`);
  29. setGroupOptions(res.data.data.map((group) => ({
  30. key: group,
  31. text: group,
  32. value: group,
  33. })));
  34. } catch (error) {
  35. showError(error.message);
  36. }
  37. };
  38. const navigate = useNavigate();
  39. const handleCancel = () => {
  40. navigate("/setting");
  41. }
  42. const loadUser = async () => {
  43. let res = undefined;
  44. if (userId) {
  45. res = await API.get(`/api/user/${userId}`);
  46. } else {
  47. res = await API.get(`/api/user/self`);
  48. }
  49. const { success, message, data } = res.data;
  50. if (success) {
  51. data.password = '';
  52. setInputs(data);
  53. } else {
  54. showError(message);
  55. }
  56. setLoading(false);
  57. };
  58. useEffect(() => {
  59. loadUser().then();
  60. if (userId) {
  61. fetchGroups().then();
  62. }
  63. }, []);
  64. const submit = async () => {
  65. let res = undefined;
  66. if (userId) {
  67. let data = { ...inputs, id: parseInt(userId) };
  68. if (typeof data.quota === 'string') {
  69. data.quota = parseInt(data.quota);
  70. }
  71. res = await API.put(`/api/user/`, data);
  72. } else {
  73. res = await API.put(`/api/user/self`, inputs);
  74. }
  75. const { success, message } = res.data;
  76. if (success) {
  77. showSuccess('用户信息更新成功!');
  78. } else {
  79. showError(message);
  80. }
  81. };
  82. return (
  83. <>
  84. <Segment loading={loading}>
  85. <Header as='h3'>更新用户信息</Header>
  86. <Form autoComplete='new-password'>
  87. <Form.Field>
  88. <Form.Input
  89. label='用户名'
  90. name='username'
  91. placeholder={'请输入新的用户名'}
  92. onChange={handleInputChange}
  93. value={username}
  94. autoComplete='new-password'
  95. />
  96. </Form.Field>
  97. <Form.Field>
  98. <Form.Input
  99. label='密码'
  100. name='password'
  101. type={'password'}
  102. placeholder={'请输入新的密码,最短 8 位'}
  103. onChange={handleInputChange}
  104. value={password}
  105. autoComplete='new-password'
  106. />
  107. </Form.Field>
  108. <Form.Field>
  109. <Form.Input
  110. label='显示名称'
  111. name='display_name'
  112. placeholder={'请输入新的显示名称'}
  113. onChange={handleInputChange}
  114. value={display_name}
  115. autoComplete='new-password'
  116. />
  117. </Form.Field>
  118. {
  119. userId && <>
  120. <Form.Field>
  121. <Form.Dropdown
  122. label='分组'
  123. placeholder={'请选择分组'}
  124. name='group'
  125. fluid
  126. search
  127. selection
  128. allowAdditions
  129. additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
  130. onChange={handleInputChange}
  131. value={inputs.group}
  132. autoComplete='new-password'
  133. options={groupOptions}
  134. />
  135. </Form.Field>
  136. <Form.Field>
  137. <Form.Input
  138. label={`剩余额度${renderQuotaWithPrompt(quota)}`}
  139. name='quota'
  140. placeholder={'请输入新的剩余额度'}
  141. onChange={handleInputChange}
  142. value={quota}
  143. type={'number'}
  144. autoComplete='new-password'
  145. />
  146. </Form.Field>
  147. </>
  148. }
  149. <Form.Field>
  150. <Form.Input
  151. label='已绑定的 GitHub 账户'
  152. name='github_id'
  153. value={github_id}
  154. autoComplete='new-password'
  155. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  156. readOnly
  157. />
  158. </Form.Field>
  159. <Form.Field>
  160. <Form.Input
  161. label='已绑定的微信账户'
  162. name='wechat_id'
  163. value={wechat_id}
  164. autoComplete='new-password'
  165. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  166. readOnly
  167. />
  168. </Form.Field>
  169. <Form.Field>
  170. <Form.Input
  171. label='已绑定的邮箱账户'
  172. name='email'
  173. value={email}
  174. autoComplete='new-password'
  175. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  176. readOnly
  177. />
  178. </Form.Field>
  179. <Button onClick={handleCancel}>取消</Button>
  180. <Button positive onClick={submit}>提交</Button>
  181. </Form>
  182. </Segment>
  183. </>
  184. );
  185. };
  186. export default EditUser;