ComposeFederatedPost.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { FC, useState } from 'react';
  2. import { Button, Input, Modal } from 'antd';
  3. import { STATUS_ERROR, STATUS_SUCCESS } from '../../utils/input-statuses';
  4. import { fetchData, FEDERATION_MESSAGE_SEND } from '../../utils/apis';
  5. const { TextArea } = Input;
  6. export type ComposeFederatedPostProps = {
  7. open: boolean;
  8. handleClose: () => void;
  9. };
  10. export const ComposeFederatedPost: FC<ComposeFederatedPostProps> = ({ open, handleClose }) => {
  11. const [content, setContent] = useState('');
  12. const [postPending, setPostPending] = useState(false);
  13. const [postSuccessState, setPostSuccessState] = useState(null);
  14. function handleEditorChange(e) {
  15. setContent(e.target.value);
  16. }
  17. function close() {
  18. setPostPending(false);
  19. setPostSuccessState(null);
  20. handleClose();
  21. }
  22. async function sendButtonClicked() {
  23. setPostPending(true);
  24. const data = {
  25. value: content,
  26. };
  27. try {
  28. await fetchData(FEDERATION_MESSAGE_SEND, {
  29. data,
  30. method: 'POST',
  31. auth: true,
  32. });
  33. setPostSuccessState(STATUS_SUCCESS);
  34. setTimeout(close, 1000);
  35. } catch (e) {
  36. // eslint-disable-next-line no-console
  37. console.error(e);
  38. setPostSuccessState(STATUS_ERROR);
  39. }
  40. setPostPending(false);
  41. }
  42. return (
  43. <Modal
  44. destroyOnClose
  45. width={600}
  46. title="Post to Followers"
  47. open={open}
  48. onCancel={handleClose}
  49. footer={[
  50. <Button onClick={() => handleClose()}>Cancel</Button>,
  51. <Button
  52. type="primary"
  53. onClick={sendButtonClicked}
  54. disabled={postPending || postSuccessState}
  55. loading={postPending}
  56. >
  57. {postSuccessState?.toUpperCase() || 'Post'}
  58. </Button>,
  59. ]}
  60. >
  61. <h3>
  62. Tell the world about your future streaming plans or let your followers know to tune in.
  63. </h3>
  64. <TextArea
  65. placeholder="I'm still live, come join me!"
  66. size="large"
  67. showCount
  68. maxLength={500}
  69. style={{ height: '150px', width: '100%' }}
  70. onChange={handleEditorChange}
  71. />
  72. </Modal>
  73. );
  74. };