import { useRequest } from "ahooks"; import { Avatar, Button, Flex, Input, Radio, Space, Table } from "antd"; import { useCallback, useRef, useState } from "react"; import type { ClubInfo } from "../types"; import { EyeOutlined, StarFilled, StarOutlined } from "@ant-design/icons"; import { useClubStore } from "../store/useClubStore"; type ClickType = 'VIEW' | 'FAV'; interface Props { handleClick?: (type: ClickType, club: ClubInfo) => void; } enum ClubType { 积分俱乐部, 俱乐部, 我的收藏, } type Data = { clubs: ClubInfo[], total: number, page: number; }; const initData = { clubs: [], total: 0, page: 1 }; export const ClubSearchTable = (props: Props) => { const [searchKey, setSearchKey] = useState(''); const [datasource, setDatasource] = useState(initData); const dataRef = useRef>>({}); const searchClub = useRequest(async ( searchKey: string, page = 1, clubType = ClubType.积分俱乐部, ) => { const resp: Data = await fetch( `/api/club/find?key=${searchKey}&page=${page}${clubType ? '&normalClub=1' : ''}` ).then(e => e.json()); dataRef.current[clubType] = { ...resp, page, }; return resp; }, { manual: true }); const clubStore = useClubStore(store => store); const [clubType, setClubType] = useState(ClubType.积分俱乐部); const handleSearch = useCallback(async (searchKey: string, clubType: ClubType, page = 1) => { switch (clubType) { case ClubType.积分俱乐部: case ClubType.俱乐部: { const resp = await searchClub.runAsync(searchKey, page, clubType); setDatasource({ ...resp, page }); break; } } }, []); const handleTypeChange = useCallback(async (type: ClubType) => { setClubType(type); switch (type) { case ClubType.积分俱乐部: case ClubType.俱乐部: { if (!searchKey) { setDatasource(dataRef.current[type] ?? initData); break; } await handleSearch(searchKey, type); break; } case ClubType.我的收藏: setDatasource({ clubs: clubStore.clubs, total: clubStore.clubs.length, page: 1 }); break; default: break; } }, [searchKey, searchClub, clubStore]); return ( handleTypeChange(e.target.value)} options={[ { label: '积分俱乐部', value: ClubType.积分俱乐部 }, { label: '俱乐部', value: ClubType.俱乐部 }, { label: '我的收藏', value: ClubType.我的收藏 }, ]} /> {clubType !== ClubType.我的收藏 ? ( { setSearchKey(e); handleSearch(e, clubType); }} onPressEnter={() => handleSearch(searchKey, clubType)} value={searchKey} onChange={e => setSearchKey(e.target.value)} /> ) : null} e.id} loading={searchClub.loading} dataSource={datasource.clubs} style={{ width: '100%' }} scroll={{ x: 400 }} pagination={clubType !== ClubType.我的收藏 ? { current: datasource.page, showSizeChanger: false, total: datasource.total, onChange: (page) => { handleSearch(searchKey, clubType, page); } } : false} > { return ( {name} ); }} /> `${num} 人`} /> { return ( ); }} />
); }