index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { batchDeletePredictions, deletePrediction, getPredictions } from '@/services/api'; // 假设这些API已经实现
  2. import type { ActionType, ProColumns } from '@ant-design/pro-components';
  3. import { FooterToolbar, PageContainer, ProTable } from '@ant-design/pro-components';
  4. import { Button, message, Popconfirm } from 'antd';
  5. import React, { useRef, useState } from 'react';
  6. const PredictionList: React.FC = () => {
  7. const actionRef = useRef<ActionType>();
  8. // const [currentRow, setCurrentRow] = useState<API.PredictionItem>();
  9. const [selectedRows, setSelectedRows] = useState<API.PredictionItem[]>([]);
  10. const [pageSize, setPageSize] = useState<number>(10);
  11. // const [updateModalOpen, handleUpdateModalOpen] = useState<boolean>(false);
  12. // const formRef = useRef<FormInstance>(null);
  13. const columns: ProColumns<API.PredictionItem>[] = [
  14. {
  15. title: '用户',
  16. dataIndex: 'username',
  17. valueType: 'text',
  18. },
  19. {
  20. title: '比赛',
  21. dataIndex: 'matchInfo',
  22. valueType: 'text',
  23. // ellipsis: true,
  24. },
  25. {
  26. title: '比赛时间',
  27. dataIndex: 'matchTime',
  28. valueType: 'text',
  29. // ellipsis: true,
  30. },
  31. {
  32. title: '胜负预测',
  33. dataIndex: 'whoWillWin',
  34. valueEnum: {
  35. home: { text: '主队胜' },
  36. away: { text: '客队胜' },
  37. draw: { text: '平局' },
  38. },
  39. },
  40. {
  41. title: '首先得分',
  42. dataIndex: 'firstTeamToScore',
  43. valueEnum: {
  44. home: { text: '主队' },
  45. away: { text: '客队' },
  46. no_goal: { text: '无进球' },
  47. },
  48. },
  49. {
  50. title: '总进球数',
  51. dataIndex: 'totalGoals',
  52. valueType: 'digit',
  53. },
  54. {
  55. title: '胜负预测结果',
  56. dataIndex: 'whoWillWinResult',
  57. valueEnum: {
  58. correct: { text: '正确', status: 'Success' },
  59. incorrect: { text: '错误', status: 'Error' },
  60. pending: { text: '-' },
  61. },
  62. },
  63. {
  64. title: '首先得分预测结果',
  65. dataIndex: 'firstTeamToScoreResult',
  66. valueEnum: {
  67. correct: { text: '正确', status: 'Success' },
  68. incorrect: { text: '错误', status: 'Error' },
  69. // pending: { text: '待定', status: 'Warning' },
  70. pending: { text: '-' },
  71. },
  72. },
  73. {
  74. title: '总进球数预测结果',
  75. dataIndex: 'totalGoalsResult',
  76. valueEnum: {
  77. correct: { text: '正确', status: 'Success' },
  78. incorrect: { text: '错误', status: 'Error' },
  79. // pending: { text: '待定', status: 'Warning' },
  80. pending: { text: '-' },
  81. },
  82. },
  83. {
  84. title: '获得积分',
  85. dataIndex: 'pointsEarned',
  86. valueType: 'digit',
  87. },
  88. {
  89. title: '操作',
  90. dataIndex: 'option',
  91. valueType: 'option',
  92. render: (_, record) => [
  93. // <a
  94. // key="edit"
  95. // onClick={() => {
  96. // handleUpdateModalOpen(true);
  97. // setCurrentRow(record);
  98. // }}
  99. // >
  100. // 编辑
  101. // </a>,
  102. <Popconfirm
  103. key="delete"
  104. title="确认删除"
  105. onConfirm={() => handleDelete(record)}
  106. okText="确定"
  107. cancelText="取消"
  108. >
  109. <a>删除</a>
  110. </Popconfirm>,
  111. ],
  112. },
  113. ];
  114. const handleDelete = async (record: API.PredictionItem) => {
  115. const res = await deletePrediction(record._id);
  116. if (res.success) {
  117. message.success('预测记录已成功删除');
  118. actionRef.current?.reload();
  119. }
  120. };
  121. return (
  122. <PageContainer>
  123. <ProTable<API.PredictionItem, API.PageParams>
  124. actionRef={actionRef}
  125. rowKey="_id"
  126. search={{
  127. labelWidth: 120,
  128. }}
  129. // toolBarRender={() => [
  130. // <Button
  131. // type="primary"
  132. // key="primary"
  133. // onClick={() => {
  134. // handleModalOpen(true);
  135. // }}
  136. // >
  137. // <PlusOutlined /> 新建预测
  138. // </Button>,
  139. // ]}
  140. request={getPredictions}
  141. columns={columns}
  142. scroll={{ x: 1240 }}
  143. pagination={{
  144. showSizeChanger: true,
  145. pageSize: pageSize,
  146. onChange: (page, pageSize) => {
  147. setPageSize(pageSize);
  148. },
  149. }}
  150. rowSelection={{
  151. onChange: (_, selectedRows) => {
  152. setSelectedRows(selectedRows);
  153. },
  154. }}
  155. />
  156. {selectedRows?.length > 0 && (
  157. <FooterToolbar
  158. extra={
  159. <div>
  160. 已选择 <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> 项 &nbsp;&nbsp;
  161. </div>
  162. }
  163. >
  164. <Button
  165. onClick={async () => {
  166. console.log('selectedRows', selectedRows);
  167. const selectedRowKeys = selectedRows.map((row) => row._id);
  168. await batchDeletePredictions(selectedRowKeys);
  169. setSelectedRows([]);
  170. actionRef.current?.reloadAndRest?.();
  171. }}
  172. >
  173. 批量删除
  174. </Button>
  175. </FooterToolbar>
  176. )}
  177. {/* <UpdateForm
  178. onSubmit={async (value) => {
  179. const res = await updatePrediction(value._id, value as API.PredictionItem);
  180. if (res.success) {
  181. message.success('修改成功');
  182. handleUpdateModalOpen(false);
  183. setCurrentRow(undefined);
  184. actionRef.current?.reload();
  185. }
  186. }}
  187. onCancel={() => {
  188. handleUpdateModalOpen(false);
  189. }}
  190. updateModalOpen={updateModalOpen}
  191. values={currentRow || {}}
  192. /> */}
  193. {/* <ModalForm
  194. title="编辑预测"
  195. width="400px"
  196. open={updateModalOpen}
  197. onOpenChange={handleUpdateModalOpen}
  198. onFinish={async (value) => {
  199. const res = await updatePrediction(currentRow?._id, value as API.PredictionItem);
  200. if (res.success) {
  201. message.success('修改成功');
  202. handleUpdateModalOpen(false);
  203. setCurrentRow(undefined);
  204. actionRef.current?.reload();
  205. }
  206. }}
  207. initialValues={currentRow}
  208. >
  209. </ModalForm> */}
  210. </PageContainer>
  211. );
  212. };
  213. export default PredictionList;