EditToken.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 EditToken = () => {
  6. const params = useParams();
  7. const tokenId = params.id;
  8. const [loading, setLoading] = useState(true);
  9. const [inputs, setInputs] = useState({
  10. name: ''
  11. });
  12. const { name } = inputs;
  13. const handleInputChange = (e, { name, value }) => {
  14. setInputs((inputs) => ({ ...inputs, [name]: value }));
  15. };
  16. const loadToken = async () => {
  17. let res = await API.get(`/api/token/${tokenId}`);
  18. const { success, message, data } = res.data;
  19. if (success) {
  20. data.password = '';
  21. setInputs(data);
  22. } else {
  23. showError(message);
  24. }
  25. setLoading(false);
  26. };
  27. useEffect(() => {
  28. loadToken().then();
  29. }, []);
  30. const submit = async () => {
  31. let res = await API.put(`/api/token/`, { ...inputs, id: parseInt(tokenId) });
  32. const { success, message } = res.data;
  33. if (success) {
  34. showSuccess('令牌更新成功!');
  35. } else {
  36. showError(message);
  37. }
  38. };
  39. return (
  40. <>
  41. <Segment loading={loading}>
  42. <Header as='h3'>更新令牌信息</Header>
  43. <Form autoComplete='off'>
  44. <Form.Field>
  45. <Form.Input
  46. label='名称'
  47. name='name'
  48. placeholder={'请输入新的名称'}
  49. onChange={handleInputChange}
  50. value={name}
  51. autoComplete='off'
  52. />
  53. </Form.Field>
  54. <Button onClick={submit}>提交</Button>
  55. </Form>
  56. </Segment>
  57. </>
  58. );
  59. };
  60. export default EditToken;