123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { Button, Form, FormInstance, Input, Table } from "antd";
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
- import { debounce } from "lodash";
- import { DageTableActions } from "@dage/pc-components";
- import { useNavigate, useSearchParams } from "react-router-dom";
- import { questionaireApi } from "@/api";
- const DEFAULT_PARAMS = {
- pageNum: 1,
- pageSize: 20,
- searchKey: "",
- };
- export default function InformationPage() {
- const navigate = useNavigate();
- const [searchParams, setSearchParams] = useSearchParams();
- const formRef = useRef<FormInstance>(null);
- const [loading, setLoading] = useState(false);
- const [list, setList] = useState<[]>([]);
- const [params, setParams] = useState({
- ...DEFAULT_PARAMS,
- searchKey: searchParams.get("searchKey") || "",
- });
- const [total, setTotal] = useState(0);
- useEffect(() => {
- setSearchParams({
- searchKey: params.searchKey,
- });
- }, [params.searchKey, setSearchParams]);
- const getList = useCallback(async () => {
- setLoading(true);
- try {
- const data = await questionaireApi.getList(params);
- setList(data.records);
- setTotal(data.total);
- } finally {
- setLoading(false);
- }
- }, [params]);
- useEffect(() => {
- getList();
- }, [getList]);
- const handleDelete = useCallback(
- async (id: number) => {
- await questionaireApi.delete(id);
- getList();
- },
- [getList]
- );
- const COLUMNS = useMemo(() => {
- return [
- {
- title: "标题",
- dataIndex: "name",
- },
- {
- title: "问题数量",
- dataIndex: "pcsQuestion",
- },
- {
- title: "已收集份数",
- dataIndex: "pcsGather",
- },
- {
- title: "发布日期",
- dataIndex: "publishDate",
- },
- {
- title: "展示状态",
- render: (item: any) => {
- return item.display ? "展示" : "不展示";
- },
- },
- {
- title: "操作",
- render: (item: any) => {
- return (
- <DageTableActions
- onEdit={() => navigate(`/questionnaire/edit/${item.id}`)}
- onDelete={handleDelete.bind(undefined, item.id)}
- />
- );
- },
- },
- ];
- }, [navigate, handleDelete]);
- const handleReset = useCallback(() => {
- formRef.current?.resetFields();
- }, [formRef]);
- const debounceSearch = useMemo(
- () =>
- debounce((changedVal: unknown, vals: any) => {
- setParams({ ...params, ...vals });
- }, 500),
- [params]
- );
- const paginationChange = useCallback(
- () => (pageNum: number, pageSize: number) => {
- setParams({ ...params, pageNum, pageSize });
- },
- [params]
- );
- return (
- <div className="information">
- <Form
- ref={formRef}
- layout="inline"
- initialValues={params}
- onValuesChange={debounceSearch}
- >
- <Form.Item label="搜索项" name="searchKey">
- <Input
- placeholder="请输入标题,最多10字"
- maxLength={10}
- showCount
- allowClear
- />
- </Form.Item>
- <Form.Item>
- <Button
- type="primary"
- onClick={() => navigate("/questionnaire/create")}
- >
- 新增
- </Button>
- </Form.Item>
- <Form.Item>
- <Button onClick={handleReset}>重置</Button>
- </Form.Item>
- </Form>
- <Table
- loading={loading}
- className="page-table"
- dataSource={list}
- columns={COLUMNS}
- rowKey="id"
- pagination={{
- showQuickJumper: true,
- position: ["bottomCenter"],
- showSizeChanger: true,
- current: params.pageNum,
- pageSize: params.pageSize,
- total,
- onChange: paginationChange(),
- }}
- />
- </div>
- );
- }
|