ChannelTableModal.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { useEffect, useState } from "react";
  2. import { Modal } from "antd";
  3. import type { ArticleType } from "../article/Article";
  4. import ChannelTable, { type IChapter } from "./ChannelTable";
  5. import { useAppSelector } from "../../hooks";
  6. import { currentUser as _currentUser } from "../../reducers/current-user";
  7. import type { IChannel } from "./Channel";
  8. import type { 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. chapter?: IChapter;
  19. onClose?: () => void;
  20. onSelect?: (channel: IChannel) => void;
  21. }
  22. const ChannelTableModalWidget = ({
  23. trigger,
  24. type,
  25. disableChannels,
  26. channelType,
  27. open = false,
  28. chapter,
  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. destroyOnClose
  62. footer={false}
  63. open={isModalOpen}
  64. onOk={handleOk}
  65. onCancel={handleCancel}
  66. >
  67. <div style={{ overflowX: "scroll" }}>
  68. <ChannelTable
  69. studioName={user?.realName}
  70. type={type}
  71. chapter={chapter}
  72. channelType={channelType}
  73. disableChannels={disableChannels}
  74. onSelect={(channel: IChannel) => {
  75. handleCancel();
  76. if (typeof onClose !== "undefined") {
  77. onClose();
  78. }
  79. if (typeof onSelect !== "undefined") {
  80. onSelect(channel);
  81. }
  82. }}
  83. />
  84. </div>
  85. </Modal>
  86. </>
  87. );
  88. };
  89. export default ChannelTableModalWidget;