| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- import { type ActionType, ProTable } from "@ant-design/pro-components";
- import { useIntl } from "react-intl";
- import { Link } from "react-router";
- import { message, Modal, Typography } from "antd";
- import { PlusOutlined } from "@ant-design/icons";
- import { Button, Dropdown, Popover } from "antd";
- import {
- ExclamationCircleOutlined,
- TeamOutlined,
- DeleteOutlined,
- EyeOutlined,
- } from "@ant-design/icons";
- import AnthologyCreate from "./AnthologyCreate";
- import type {
- IAnthologyListResponse,
- IDeleteResponse,
- } from "../../api/Article";
- import { delete_, get } from "../../request";
- import { PublicityValueEnum } from "../studio/table";
- import { useEffect, useRef, useState } from "react";
- import Share, { EResType } from "../share/Share";
- import StudioName, { type IStudio } from "../auth/Studio";
- import { type IResNumberResponse, renderBadge } from "../channel/ChannelTable";
- import { fullUrl, getSorterUrl } from "../../utils";
- const { Text } = Typography;
- interface IItem {
- sn: number;
- id: string;
- title: string;
- subtitle: string;
- publicity: number;
- articles: number;
- studio?: IStudio;
- updated_at: string;
- }
- interface IWidget {
- title?: string;
- studioName?: string;
- showCol?: string[];
- showCreate?: boolean;
- showOption?: boolean;
- onTitleClick?: (id: string) => void;
- }
- const AnthologyListWidget = ({
- title,
- studioName,
- showCreate = true,
- showOption = true,
- onTitleClick,
- }: IWidget) => {
- const intl = useIntl();
- const [openCreate, setOpenCreate] = useState(false);
- const [activeKey, setActiveKey] = useState<React.Key | undefined>("my");
- const [myNumber, setMyNumber] = useState<number>(0);
- const [collaborationNumber, setCollaborationNumber] = useState<number>(0);
- useEffect(() => {
- /**
- * 获取各种课程的数量
- */
- const url = `/v2/anthology-my-number?studio=${studioName}`;
- console.log("url", url);
- get<IResNumberResponse>(url).then((json) => {
- if (json.ok) {
- setMyNumber(json.data.my);
- setCollaborationNumber(json.data.collaboration);
- }
- });
- }, [studioName]);
- const showDeleteConfirm = (id: string, title: string) => {
- Modal.confirm({
- icon: <ExclamationCircleOutlined />,
- title:
- intl.formatMessage({
- id: "message.delete.confirm",
- }) +
- intl.formatMessage({
- id: "message.irrevocable",
- }),
- content: title,
- okText: intl.formatMessage({
- id: "buttons.delete",
- }),
- okType: "danger",
- cancelText: intl.formatMessage({
- id: "buttons.no",
- }),
- onOk() {
- console.log("delete", id);
- return delete_<IDeleteResponse>(`/v2/anthology/${id}`)
- .then((json) => {
- if (json.ok) {
- message.success("删除成功");
- ref.current?.reload();
- } else {
- message.error(json.message);
- }
- })
- .catch((e) => console.log("Oops errors!", e));
- },
- });
- };
- const [isModalOpen, setIsModalOpen] = useState(false);
- const [shareResId, setShareResId] = useState<string>("");
- const [shareResType, setShareResType] = useState<EResType>(
- EResType.collection
- );
- const showShareModal = (resId: string, resType: EResType) => {
- setShareResId(resId);
- setShareResType(resType);
- setIsModalOpen(true);
- };
- const handleOk = () => {
- setIsModalOpen(false);
- };
- const handleCancel = () => {
- setIsModalOpen(false);
- };
- const ref = useRef<ActionType | null>(null);
- return (
- <>
- <ProTable<IItem>
- headerTitle={title}
- actionRef={ref}
- columns={[
- {
- title: intl.formatMessage({
- id: "dict.fields.sn.label",
- }),
- dataIndex: "sn",
- key: "sn",
- width: 50,
- search: false,
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.title.label",
- }),
- dataIndex: "title",
- key: "title",
- tooltip: "过长会自动收缩",
- ellipsis: true,
- render: (_text, row, index) => {
- return (
- <div key={index}>
- <div>
- <Typography.Link
- onClick={() => {
- if (typeof onTitleClick !== "undefined") {
- onTitleClick(row.id);
- }
- }}
- >
- {row.title}
- </Typography.Link>
- </div>
- <Text type="secondary">{row.subtitle}</Text>
- </div>
- );
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.owner.label",
- }),
- dataIndex: "studio",
- key: "studio",
- render: (_text, row) => {
- return <StudioName data={row.studio} />;
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.publicity.label",
- }),
- dataIndex: "publicity",
- key: "publicity",
- width: 100,
- search: false,
- filters: true,
- onFilter: true,
- valueEnum: PublicityValueEnum(),
- },
- {
- title: intl.formatMessage({
- id: "article.fields.article.count.label",
- }),
- dataIndex: "articles",
- key: "articles",
- width: 100,
- search: false,
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.updated-at.label",
- }),
- key: "updated_at",
- width: 100,
- search: false,
- dataIndex: "updated_at",
- valueType: "date",
- sorter: true,
- },
- {
- title: intl.formatMessage({ id: "buttons.option" }),
- key: "option",
- width: 120,
- hideInTable: !showOption,
- valueType: "option",
- render: (_text, row, index) => [
- <Dropdown.Button
- key={index}
- type="link"
- trigger={["click", "contextMenu"]}
- menu={{
- items: [
- {
- key: "open",
- label: (
- <Link to={`/anthology/${row.id}`}>
- {intl.formatMessage({
- id: "buttons.open.in.library",
- })}
- </Link>
- ),
- icon: <EyeOutlined />,
- },
- {
- key: "share",
- label: intl.formatMessage({
- id: "buttons.share",
- }),
- icon: <TeamOutlined />,
- },
- {
- key: "remove",
- label: intl.formatMessage({
- id: "buttons.delete",
- }),
- icon: <DeleteOutlined />,
- danger: true,
- },
- ],
- onClick: (e) => {
- switch (e.key) {
- case "open":
- window.open(fullUrl(`/anthology/${row.id}`), "_blank");
- break;
- case "share":
- console.log("share");
- showShareModal(row.id, EResType.collection);
- break;
- case "remove":
- showDeleteConfirm(row.id, row.title);
- break;
- }
- },
- }}
- >
- <Link to={`/anthology/${row.id}`} target="_blank">
- {intl.formatMessage({
- id: "buttons.view",
- })}
- </Link>
- </Dropdown.Button>,
- ],
- },
- ]}
- request={async (params = {}, sorter, filter) => {
- console.log(params, sorter, filter);
- let url = `/v2/anthology?view=studio&view2=${activeKey}&name=${studioName}`;
- const offset =
- ((params.current ? params.current : 1) - 1) *
- (params.pageSize ? params.pageSize : 20);
- url += `&limit=${params.pageSize}&offset=${offset}`;
- url += params.keyword ? "&search=" + params.keyword : "";
- url += getSorterUrl(sorter);
- const res = await get<IAnthologyListResponse>(url);
- const items: IItem[] = res.data.rows.map((item, id) => {
- return {
- sn: id + offset + 1,
- id: item.uid,
- title: item.title,
- subtitle: item.subtitle,
- publicity: item.status,
- articles: item.childrenNumber,
- studio: item.studio,
- updated_at: item.updated_at,
- };
- });
- console.log(items);
- return {
- total: res.data.count,
- succcess: true,
- data: items,
- };
- }}
- rowKey="id"
- bordered
- pagination={{
- showQuickJumper: true,
- showSizeChanger: true,
- pageSize: 10,
- }}
- search={false}
- options={{
- search: true,
- }}
- toolBarRender={() => [
- showCreate ? (
- <Popover
- content={
- <AnthologyCreate
- studio={studioName}
- onSuccess={() => {
- setOpenCreate(false);
- ref.current?.reload();
- }}
- />
- }
- placement="bottomRight"
- trigger="click"
- open={openCreate}
- onOpenChange={(open: boolean) => {
- setOpenCreate(open);
- }}
- >
- <Button key="button" icon={<PlusOutlined />} type="primary">
- {intl.formatMessage({ id: "buttons.create" })}
- </Button>
- </Popover>
- ) : undefined,
- ]}
- toolbar={{
- menu: {
- activeKey,
- items: [
- {
- key: "my",
- label: (
- <span>
- {intl.formatMessage({ id: "labels.this-studio" })}
- {renderBadge(myNumber, activeKey === "my")}
- </span>
- ),
- },
- {
- key: "collaboration",
- label: (
- <span>
- {intl.formatMessage({ id: "labels.collaboration" })}
- {renderBadge(
- collaborationNumber,
- activeKey === "collaboration"
- )}
- </span>
- ),
- },
- ],
- onChange(key) {
- console.log("show course", key);
- setActiveKey(key);
- ref.current?.reload();
- },
- },
- }}
- />
- <Modal
- destroyOnHidden={true}
- width={700}
- title={intl.formatMessage({ id: "labels.collaboration" })}
- open={isModalOpen}
- onOk={handleOk}
- onCancel={handleCancel}
- >
- <Share resId={shareResId} resType={shareResType} />
- </Modal>
- </>
- );
- };
- export default AnthologyListWidget;
|