ChannelPicker.tsx 1.9 KB

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