EditChannel.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Header, Segment } from 'semantic-ui-react';
  3. import { useParams } from 'react-router-dom';
  4. import { API, showError, showSuccess } from '../../helpers';
  5. const EditChannel = () => {
  6. const params = useParams();
  7. const userId = params.id;
  8. const [loading, setLoading] = useState(true);
  9. const [inputs, setInputs] = useState({
  10. username: '',
  11. display_name: '',
  12. password: '',
  13. github_id: '',
  14. wechat_id: '',
  15. email: '',
  16. });
  17. const { username, display_name, password, github_id, wechat_id, email } =
  18. inputs;
  19. const handleInputChange = (e, { name, value }) => {
  20. setInputs((inputs) => ({ ...inputs, [name]: value }));
  21. };
  22. const loadUser = async () => {
  23. let res = undefined;
  24. if (userId) {
  25. res = await API.get(`/api/user/${userId}`);
  26. } else {
  27. res = await API.get(`/api/user/self`);
  28. }
  29. const { success, message, data } = res.data;
  30. if (success) {
  31. data.password = '';
  32. setInputs(data);
  33. } else {
  34. showError(message);
  35. }
  36. setLoading(false);
  37. };
  38. useEffect(() => {
  39. loadUser().then();
  40. }, []);
  41. const submit = async () => {
  42. let res = undefined;
  43. if (userId) {
  44. res = await API.put(`/api/user/`, { ...inputs, id: parseInt(userId) });
  45. } else {
  46. res = await API.put(`/api/user/self`, inputs);
  47. }
  48. const { success, message } = res.data;
  49. if (success) {
  50. showSuccess('用户信息更新成功!');
  51. } else {
  52. showError(message);
  53. }
  54. };
  55. return (
  56. <>
  57. <Segment loading={loading}>
  58. <Header as='h3'>更新用户信息</Header>
  59. <Form autoComplete='off'>
  60. <Form.Field>
  61. <Form.Input
  62. label='用户名'
  63. name='username'
  64. placeholder={'请输入新的用户名'}
  65. onChange={handleInputChange}
  66. value={username}
  67. autoComplete='off'
  68. />
  69. </Form.Field>
  70. <Form.Field>
  71. <Form.Input
  72. label='密码'
  73. name='password'
  74. type={'password'}
  75. placeholder={'请输入新的密码'}
  76. onChange={handleInputChange}
  77. value={password}
  78. autoComplete='off'
  79. />
  80. </Form.Field>
  81. <Form.Field>
  82. <Form.Input
  83. label='显示名称'
  84. name='display_name'
  85. placeholder={'请输入新的显示名称'}
  86. onChange={handleInputChange}
  87. value={display_name}
  88. autoComplete='off'
  89. />
  90. </Form.Field>
  91. <Form.Field>
  92. <Form.Input
  93. label='已绑定的 GitHub 账户'
  94. name='github_id'
  95. value={github_id}
  96. autoComplete='off'
  97. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  98. readOnly
  99. />
  100. </Form.Field>
  101. <Form.Field>
  102. <Form.Input
  103. label='已绑定的微信账户'
  104. name='wechat_id'
  105. value={wechat_id}
  106. autoComplete='off'
  107. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  108. readOnly
  109. />
  110. </Form.Field>
  111. <Form.Field>
  112. <Form.Input
  113. label='已绑定的邮箱账户'
  114. name='email'
  115. value={email}
  116. autoComplete='off'
  117. placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
  118. readOnly
  119. />
  120. </Form.Field>
  121. <Button onClick={submit}>提交</Button>
  122. </Form>
  123. </Segment>
  124. </>
  125. );
  126. };
  127. export default EditChannel;