| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- import { Link } from "react-router";
- import { useIntl } from "react-intl";
- import {
- Button,
- Popover,
- Dropdown,
- Typography,
- Modal,
- message,
- Space,
- Table,
- Badge,
- } from "antd";
- import { type ActionType, ProTable } from "@ant-design/pro-components";
- import {
- PlusOutlined,
- DeleteOutlined,
- TeamOutlined,
- ExclamationCircleOutlined,
- FolderAddOutlined,
- ReconciliationOutlined,
- } from "@ant-design/icons";
- import ArticleCreate from "./ArticleCreate";
- import { delete_, get } from "../../request";
- import type { IArticleListResponse, IDeleteResponse } from "../../api/Article";
- import { PublicityValueEnum } from "../studio/table";
- import { useEffect, useRef, useState } from "react";
- import { ArticleTplModal } from "../template/Builder/ArticleTpl";
- import Share, { EResType } from "../share/Share";
- import AddToAnthology from "./AddToAnthology";
- import AnthologySelect from "../anthology/AnthologySelect";
- import StudioName, { type IStudio } from "../auth/Studio";
- import type { IUser } from "../auth/User";
- import { getSorterUrl } from "../../utils";
- import TransferCreate from "../transfer/TransferCreate";
- import { TransferOutLinedIcon } from "../../assets/icon";
- const { Text } = Typography;
- interface IArticleNumberResponse {
- ok: boolean;
- message: string;
- data: {
- my: number;
- collaboration: number;
- };
- }
- const renderBadge = (count: number, active = false) => {
- return (
- <Badge
- count={count}
- style={{
- marginBlockStart: -2,
- marginInlineStart: 4,
- color: active ? "#1890FF" : "#999",
- backgroundColor: active ? "#E6F7FF" : "#eee",
- }}
- />
- );
- };
- interface DataItem {
- sn: number;
- id: string;
- title: string;
- subtitle: string;
- summary?: string | null;
- anthologyCount?: number;
- anthologyTitle?: string;
- publicity: number;
- studio?: IStudio;
- editor?: IUser;
- updated_at?: string;
- }
- interface IWidget {
- studioName?: string;
- editable?: boolean;
- multiple?: boolean;
- onSelect?: (
- id: string,
- title: string,
- event: React.MouseEvent<HTMLElement, MouseEvent>
- ) => void;
- }
- const ArticleListWidget = ({
- studioName,
- multiple = true,
- editable = false,
- onSelect,
- }: IWidget) => {
- const intl = useIntl(); //i18n
- const [openCreate, setOpenCreate] = useState(false);
- const [anthologyId, setAnthologyId] = useState<string>();
- const [activeKey, setActiveKey] = useState<React.Key | undefined>("my");
- const [myNumber, setMyNumber] = useState<number>(0);
- const [collaborationNumber, setCollaborationNumber] = useState<number>(0);
- const [transfer, setTransfer] = useState<string[]>();
- const [transferName, setTransferName] = useState<string>();
- const [transferOpen, setTransferOpen] = useState(false);
- const [pageSize, setPageSize] = useState(10);
- useEffect(() => {
- /**
- * 获取各种课程的数量
- */
- const url = `/v2/article-my-number?studio=${studioName}`;
- console.log("url", url);
- get<IArticleNumberResponse>(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/article/${id}`)
- .then((json) => {
- if (json.ok) {
- message.success("删除成功");
- ref.current?.reload();
- } else {
- message.error(json.message);
- }
- })
- .catch((e) => console.log("Oops errors!", e));
- },
- });
- };
- const ref = useRef<ActionType | null>(null);
- const [isModalOpen, setIsModalOpen] = useState(false);
- const [shareResId, setShareResId] = useState<string>("");
- const [shareResType, setShareResType] = useState<EResType>(EResType.article);
- const showShareModal = (resId: string, resType: EResType) => {
- setShareResId(resId);
- setShareResType(resType);
- setIsModalOpen(true);
- };
- const handleOk = () => {
- setIsModalOpen(false);
- };
- const handleCancel = () => {
- setIsModalOpen(false);
- };
- return (
- <>
- <ProTable<DataItem>
- 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) => {
- return (
- <>
- <div key={1}>
- <Typography.Link
- onClick={(
- event: React.MouseEvent<HTMLElement, MouseEvent>
- ) => {
- if (typeof onSelect !== "undefined") {
- onSelect(row.id, row.title, event);
- }
- }}
- >
- {row.title}
- </Typography.Link>
- </div>
- <div key={2}>
- <Text type="secondary">{row.subtitle}</Text>
- </div>
- {activeKey !== "my" ? (
- <div key={3}>
- <Text type="secondary">
- <StudioName data={row.studio} />
- </Text>
- </div>
- ) : undefined}
- </>
- );
- },
- },
- {
- title: intl.formatMessage({
- id: "columns.library.anthology.title",
- }),
- dataIndex: "subtitle",
- key: "subtitle",
- render: (_text, row) => {
- return (
- <Space>
- {row.anthologyTitle}
- {row.anthologyCount ? (
- <Badge color="geekblue" count={row.anthologyCount} />
- ) : undefined}
- </Space>
- );
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.summary.label",
- }),
- dataIndex: "summary",
- key: "summary",
- tooltip: "过长会自动收缩",
- ellipsis: true,
- },
- {
- 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: "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,
- valueType: "option",
- hideInTable: !editable,
- render: (_text, row, index) => {
- return [
- <Dropdown.Button
- trigger={["click", "contextMenu"]}
- key={index}
- type="link"
- menu={{
- items: [
- {
- key: "tpl",
- label: (
- <ArticleTplModal
- title={row.title}
- type="article"
- articleId={row.id}
- trigger={<>模版</>}
- />
- ),
- icon: <ReconciliationOutlined />,
- },
- {
- key: "share",
- label: intl.formatMessage({
- id: "buttons.share",
- }),
- icon: <TeamOutlined />,
- },
- {
- key: "addToAnthology",
- label: (
- <AddToAnthology
- trigger={<Button type="link">加入文集</Button>}
- studioName={studioName}
- articleIds={[row.id]}
- />
- ),
- icon: <FolderAddOutlined />,
- },
- {
- key: "transfer",
- label: intl.formatMessage({
- id: "columns.studio.transfer.title",
- }),
- icon: <TransferOutLinedIcon />,
- },
- {
- key: "remove",
- label: intl.formatMessage({
- id: "buttons.delete",
- }),
- icon: <DeleteOutlined />,
- danger: true,
- },
- ],
- onClick: (e) => {
- switch (e.key) {
- case "share":
- showShareModal(row.id, EResType.article);
- break;
- case "remove":
- showDeleteConfirm(row.id, row.title);
- break;
- case "transfer":
- setTransfer([row.id]);
- setTransferName(row.title);
- setTransferOpen(true);
- break;
- default:
- break;
- }
- },
- }}
- >
- <Link
- key={index}
- to={`/article/article/${row.id}`}
- target="_blank"
- >
- {intl.formatMessage({
- id: "buttons.view",
- })}
- </Link>
- </Dropdown.Button>,
- ];
- },
- },
- ]}
- rowSelection={
- multiple
- ? {
- // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
- // 注释该行则默认不显示下拉选项
- selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
- }
- : undefined
- }
- tableAlertRender={({ selectedRowKeys, onCleanSelected }) => (
- <Space size={24}>
- <span>
- {intl.formatMessage({ id: "buttons.selected" })}
- {selectedRowKeys.length}
- <Button type="link" onClick={onCleanSelected}>
- {intl.formatMessage({ id: "buttons.unselect" })}
- </Button>
- </span>
- </Space>
- )}
- tableAlertOptionRender={({ selectedRowKeys, onCleanSelected }) => {
- return (
- <Space>
- <Button
- type="link"
- onClick={() => {
- const resId = selectedRowKeys.map((item) => item.toString());
- setTransfer(resId);
- setTransferName(resId.length + "个文章");
- setTransferOpen(true);
- }}
- >
- 转让
- </Button>
- <AddToAnthology
- studioName={studioName}
- trigger={<Button type="link">加入文集</Button>}
- articleIds={selectedRowKeys.map((item) => item.toString())}
- onFinally={() => {
- onCleanSelected();
- }}
- />
- </Space>
- );
- }}
- request={async (params = {}, sorter) => {
- let url = `/v2/article?view=studio&view2=${activeKey}&name=${studioName}`;
- const offset =
- ((params.current ? params.current : 1) - 1) *
- (params.pageSize ? params.pageSize : pageSize);
- if (params.pageSize) {
- setPageSize(params.pageSize);
- }
- url += `&limit=${params.pageSize}&offset=${offset}`;
- url += params.keyword ? "&search=" + params.keyword : "";
- if (typeof anthologyId !== "undefined") {
- url += "&anthology=" + anthologyId;
- }
- url += getSorterUrl(sorter);
- console.log("url", url);
- const res = await get<IArticleListResponse>(url);
- const items: DataItem[] = res.data.rows.map((item, id) => {
- return {
- sn: id + offset + 1,
- id: item.uid,
- title: item.title,
- subtitle: item.subtitle,
- summary: item.summary,
- anthologyCount: item.anthology_count,
- anthologyTitle: item.anthology_first?.title,
- publicity: item.status,
- updated_at: item.updated_at,
- studio: item.studio,
- editor: item.editor,
- };
- });
- return {
- total: res.data.count,
- succcess: true,
- data: items,
- };
- }}
- rowKey="id"
- bordered
- pagination={{
- showQuickJumper: true,
- showSizeChanger: true,
- pageSize: pageSize,
- }}
- search={false}
- options={{
- search: true,
- }}
- toolBarRender={() => [
- activeKey === "my" ? (
- <AnthologySelect
- studioName={studioName}
- onSelect={(value: string) => {
- setAnthologyId(value);
- ref.current?.reload();
- }}
- />
- ) : undefined,
- <Popover
- content={
- <ArticleCreate
- studio={studioName}
- anthologyId={anthologyId}
- 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>,
- ]}
- 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);
- setAnthologyId(undefined);
- 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>
- <TransferCreate
- studioName={studioName}
- resId={transfer}
- resType="article"
- resName={transferName}
- open={transferOpen}
- onOpenChange={(visible: boolean) => setTransferOpen(visible)}
- />
- </>
- );
- };
- export default ArticleListWidget;
|