AddChannel.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { useState } from 'react';
  2. import { Button, Form, Header, Segment } from 'semantic-ui-react';
  3. import { API, showError, showSuccess } from '../../helpers';
  4. import { CHANNEL_OPTIONS } from '../../constants';
  5. const AddChannel = () => {
  6. const originInputs = {
  7. name: '',
  8. type: 1,
  9. key: '',
  10. base_url: '',
  11. };
  12. const [inputs, setInputs] = useState(originInputs);
  13. const { name, type, key } = inputs;
  14. const handleInputChange = (e, { name, value }) => {
  15. setInputs((inputs) => ({ ...inputs, [name]: value }));
  16. };
  17. const submit = async () => {
  18. if (inputs.name === '' || inputs.key === '') return;
  19. if (inputs.base_url.endsWith('/')) {
  20. inputs.base_url = inputs.base_url.slice(0, inputs.base_url.length - 1);
  21. }
  22. const res = await API.post(`/api/channel/`, inputs);
  23. const { success, message } = res.data;
  24. if (success) {
  25. showSuccess('渠道创建成功!');
  26. setInputs(originInputs);
  27. } else {
  28. showError(message);
  29. }
  30. };
  31. return (
  32. <>
  33. <Segment>
  34. <Header as='h3'>创建新的渠道</Header>
  35. <Form autoComplete='new-password'>
  36. <Form.Field>
  37. <Form.Select
  38. label='类型'
  39. name='type'
  40. options={CHANNEL_OPTIONS}
  41. value={inputs.type}
  42. onChange={handleInputChange}
  43. />
  44. </Form.Field>
  45. {
  46. type === 8 && (
  47. <Form.Field>
  48. <Form.Input
  49. label='Base URL'
  50. name='base_url'
  51. placeholder={'请输入自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
  52. onChange={handleInputChange}
  53. value={inputs.base_url}
  54. autoComplete='new-password'
  55. />
  56. </Form.Field>
  57. )
  58. }
  59. <Form.Field>
  60. <Form.Input
  61. label='名称'
  62. name='name'
  63. placeholder={'请输入名称'}
  64. onChange={handleInputChange}
  65. value={name}
  66. autoComplete='new-password'
  67. required
  68. />
  69. </Form.Field>
  70. <Form.Field>
  71. <Form.Input
  72. label='密钥'
  73. name='key'
  74. placeholder={'请输入密钥'}
  75. onChange={handleInputChange}
  76. value={key}
  77. // type='password'
  78. autoComplete='new-password'
  79. required
  80. />
  81. </Form.Field>
  82. <Button type={'submit'} onClick={submit}>
  83. 提交
  84. </Button>
  85. </Form>
  86. </Segment>
  87. </>
  88. );
  89. };
  90. export default AddChannel;