ChannelList.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { useIntl } from "react-intl";
  2. import { useState, useEffect } from "react";
  3. import { Card, List, message, Space, Tag } from "antd";
  4. import type { IChannelApiData } from "../../api/Channel";
  5. import type { IApiResponseChannelList } from "../../api/Corpus";
  6. import { get } from "../../request";
  7. import ChannelListItem from "./ChannelListItem";
  8. import type { IStudio } from "../auth/Studio";
  9. export interface ChannelFilterProps {
  10. chapterProgress: number;
  11. lang: string;
  12. channelType: string;
  13. }
  14. interface IChannelList {
  15. channel: IChannelApiData;
  16. studio: IStudio;
  17. count: number;
  18. }
  19. interface IWidgetChannelList {
  20. filter?: ChannelFilterProps;
  21. }
  22. const defaultChannelFilterProps: ChannelFilterProps = {
  23. chapterProgress: 0.9,
  24. lang: "zh",
  25. channelType: "translation",
  26. };
  27. const ChannelListWidget = ({
  28. filter = defaultChannelFilterProps,
  29. }: IWidgetChannelList) => {
  30. const [tableData, setTableData] = useState<IChannelList[]>([]);
  31. const intl = useIntl();
  32. useEffect(() => {
  33. const url = `/v2/progress?view=channel&channel_type=${filter.channelType}&lang=${filter.lang}&progress=${filter.chapterProgress}`;
  34. get<IApiResponseChannelList>(url).then(function (json) {
  35. if (json.ok) {
  36. console.log("channel", json.data);
  37. const newData: IChannelList[] = json.data.rows.map((item) => {
  38. return {
  39. channel: {
  40. name: item.channel.name,
  41. id: item.channel.uid,
  42. type: item.channel.type,
  43. },
  44. studio: item.studio,
  45. count: item.count,
  46. };
  47. });
  48. setTableData(newData);
  49. } else {
  50. message.error(json.message);
  51. }
  52. });
  53. }, [filter]);
  54. return (
  55. <Card
  56. title={intl.formatMessage({
  57. id: `columns.studio.channel.title`,
  58. })}
  59. size="small"
  60. >
  61. <List
  62. itemLayout="vertical"
  63. size="small"
  64. dataSource={tableData}
  65. renderItem={(item) => (
  66. <List.Item>
  67. <Space>
  68. <ChannelListItem channel={item.channel} studio={item.studio} />
  69. <Tag>{item.count}</Tag>
  70. </Space>
  71. </List.Item>
  72. )}
  73. />
  74. </Card>
  75. );
  76. };
  77. export default ChannelListWidget;