TermExport.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Button, message } from "antd";
  4. import { ExportOutlined } from "@ant-design/icons";
  5. import modal from "antd/lib/modal";
  6. import { API_HOST, get } from "../../request";
  7. interface IExportResponse {
  8. ok: boolean;
  9. message: string;
  10. data: {
  11. uuid: string;
  12. filename: string;
  13. type: string;
  14. };
  15. }
  16. interface IWidget {
  17. channelId?: string;
  18. studioName?: string;
  19. }
  20. const TermExportWidget = ({ channelId, studioName }: IWidget) => {
  21. const intl = useIntl();
  22. const [loading, setLoading] = useState(false);
  23. return (
  24. <Button
  25. loading={loading}
  26. icon={<ExportOutlined />}
  27. onClick={() => {
  28. let url = `/v2/terms-export?view=`;
  29. if (typeof channelId !== "undefined") {
  30. url += `channel&id=${channelId}`;
  31. } else if (typeof studioName !== "undefined") {
  32. url += `studio&name=${studioName}`;
  33. }
  34. setLoading(true);
  35. get<IExportResponse>(url)
  36. .then((json) => {
  37. if (json.ok) {
  38. const link = `${API_HOST}/api/v2/terms-export/${json.data.uuid}`;
  39. modal.info({
  40. title: intl.formatMessage({ id: "buttons.download" }),
  41. content: (
  42. <>
  43. <a
  44. href={link}
  45. target="_blank"
  46. key="export"
  47. rel="noreferrer"
  48. >
  49. {intl.formatMessage({ id: "buttons.download.link" })}
  50. </a>
  51. </>
  52. ),
  53. });
  54. } else {
  55. message.error(json.message);
  56. }
  57. })
  58. .finally(() => {
  59. setLoading(false);
  60. });
  61. }}
  62. >
  63. {intl.formatMessage({ id: "buttons.export" })}
  64. </Button>
  65. );
  66. };
  67. export default TermExportWidget;