ChannelTableModal.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React, { useEffect, useState } from "react";
  2. import { Modal } from "antd";
  3. import { ArticleType } from "../article/Article";
  4. import ChannelTable from "./ChannelTable";
  5. import { useAppSelector } from "../../hooks";
  6. import { currentUser as _currentUser } from "../../reducers/current-user";
  7. import { IChannel } from "./Channel";
  8. import { TChannelType } from "../api/Channel";
  9. import { useIntl } from "react-intl";
  10. interface IWidget {
  11. trigger?: React.ReactNode;
  12. channelType?: TChannelType;
  13. type?: ArticleType | "editable";
  14. articleId?: string;
  15. multiSelect?: boolean;
  16. disableChannels?: string[];
  17. open?: boolean;
  18. onClose?: Function;
  19. onSelect?: Function;
  20. }
  21. const ChannelTableModalWidget = ({
  22. trigger,
  23. type,
  24. articleId,
  25. multiSelect = true,
  26. disableChannels,
  27. channelType,
  28. open = false,
  29. onClose,
  30. onSelect,
  31. }: IWidget) => {
  32. const [isModalOpen, setIsModalOpen] = useState(open);
  33. const intl = useIntl();
  34. const user = useAppSelector(_currentUser);
  35. useEffect(() => {
  36. setIsModalOpen(open);
  37. }, [open]);
  38. const showModal = () => {
  39. setIsModalOpen(true);
  40. };
  41. const handleOk = () => {
  42. setIsModalOpen(false);
  43. if (typeof onClose !== "undefined") {
  44. onClose();
  45. }
  46. };
  47. const handleCancel = () => {
  48. setIsModalOpen(false);
  49. if (typeof onClose !== "undefined") {
  50. onClose();
  51. }
  52. };
  53. return (
  54. <>
  55. <span onClick={showModal}>{trigger}</span>
  56. <Modal
  57. width={"90%"}
  58. title={intl.formatMessage({
  59. id: "buttons.select.channel",
  60. })}
  61. footer={false}
  62. open={isModalOpen}
  63. onOk={handleOk}
  64. onCancel={handleCancel}
  65. >
  66. <div style={{ overflowX: "scroll" }}>
  67. <ChannelTable
  68. studioName={user?.realName}
  69. type={type}
  70. channelType={channelType}
  71. disableChannels={disableChannels}
  72. onSelect={(channel: IChannel) => {
  73. handleCancel();
  74. if (typeof onClose !== "undefined") {
  75. onClose();
  76. }
  77. if (typeof onSelect !== "undefined") {
  78. onSelect(channel);
  79. }
  80. }}
  81. />
  82. </div>
  83. </Modal>
  84. </>
  85. );
  86. };
  87. export default ChannelTableModalWidget;