|
| 1 | +/* |
| 2 | + * |
| 3 | + * PostsTable |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react' |
| 8 | +import PropTypes from 'prop-types' |
| 9 | +import { Table, Button } from 'antd' |
| 10 | + |
| 11 | +import { OperationWrapper } from './styles' |
| 12 | + |
| 13 | +import Pagi from '../Pagi' |
| 14 | +import { TableLoading } from '../LoadingEffects' |
| 15 | +import { Space } from '../BaseStyled' |
| 16 | +import UserCell from '../UserCell' |
| 17 | +import CommunityCell from '../CommunityCell' |
| 18 | +import TagsCell from '../TagsCell' |
| 19 | +import TimeStampCell from '../TimeStampCell' |
| 20 | + |
| 21 | +import { makeDebugger } from '../../utils' |
| 22 | + |
| 23 | +/* eslint-disable no-unused-vars */ |
| 24 | +const debug = makeDebugger('c:PostsTable:index') |
| 25 | +/* eslint-enable no-unused-vars */ |
| 26 | + |
| 27 | +class PostsTable extends React.Component { |
| 28 | + /* |
| 29 | + constructor(props) { |
| 30 | + super(props) |
| 31 | + } |
| 32 | + */ |
| 33 | + |
| 34 | + columns() { |
| 35 | + const { |
| 36 | + setCommunity, |
| 37 | + unsetCommunity, |
| 38 | + unsetTag, |
| 39 | + setTag, |
| 40 | + onEdit, |
| 41 | + onDelete, |
| 42 | + } = this.props |
| 43 | + |
| 44 | + return [ |
| 45 | + { |
| 46 | + title: 'id', |
| 47 | + dataIndex: 'id', |
| 48 | + align: 'center', |
| 49 | + width: 80, |
| 50 | + fixed: 'left', |
| 51 | + }, |
| 52 | + { |
| 53 | + title: '标题', |
| 54 | + width: 250, |
| 55 | + dataIndex: 'title', |
| 56 | + align: 'left', |
| 57 | + fixed: 'left', |
| 58 | + render: text => { |
| 59 | + return <div>{text}</div> |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + title: '摘要', |
| 64 | + width: 300, |
| 65 | + dataIndex: 'digest', |
| 66 | + align: 'center', |
| 67 | + render: text => { |
| 68 | + return <div>{text}</div> |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + title: '作者', |
| 73 | + width: 200, |
| 74 | + dataIndex: 'author', |
| 75 | + align: 'center', |
| 76 | + render: author => <UserCell user={author} />, |
| 77 | + }, |
| 78 | + { |
| 79 | + title: '社区', |
| 80 | + width: 150, |
| 81 | + dataIndex: 'communities', |
| 82 | + align: 'center', |
| 83 | + render: (communities, record) => ( |
| 84 | + <CommunityCell |
| 85 | + array={communities} |
| 86 | + source={record} |
| 87 | + thread="POST" |
| 88 | + onDelete={unsetCommunity} |
| 89 | + onAdd={setCommunity} |
| 90 | + withSetter |
| 91 | + /> |
| 92 | + ), |
| 93 | + }, |
| 94 | + { |
| 95 | + title: '标签', |
| 96 | + width: 250, |
| 97 | + dataIndex: 'tags', |
| 98 | + align: 'center', |
| 99 | + render: (tags, record) => ( |
| 100 | + <TagsCell |
| 101 | + thread="POST" |
| 102 | + source={record} |
| 103 | + onDelete={unsetTag} |
| 104 | + onAdd={setTag} |
| 105 | + /> |
| 106 | + ), |
| 107 | + }, |
| 108 | + { |
| 109 | + title: '浏览', |
| 110 | + width: 100, |
| 111 | + dataIndex: 'views', |
| 112 | + align: 'center', |
| 113 | + }, |
| 114 | + { |
| 115 | + title: '评论数', |
| 116 | + width: 100, |
| 117 | + dataIndex: 'commentsCount', |
| 118 | + align: 'center', |
| 119 | + }, |
| 120 | + { |
| 121 | + title: '收藏', |
| 122 | + width: 100, |
| 123 | + dataIndex: 'favoritedCount', |
| 124 | + align: 'center', |
| 125 | + }, |
| 126 | + { |
| 127 | + title: '点赞', |
| 128 | + width: 100, |
| 129 | + dataIndex: 'starredCount', |
| 130 | + align: 'center', |
| 131 | + }, |
| 132 | + { |
| 133 | + title: '时间戳', |
| 134 | + width: 120, |
| 135 | + align: 'center', |
| 136 | + render: (text, record) => <TimeStampCell data={record} />, |
| 137 | + }, |
| 138 | + { |
| 139 | + title: '操作', |
| 140 | + width: 200, |
| 141 | + dataIndex: '', |
| 142 | + align: 'center', |
| 143 | + render: (text, record) => { |
| 144 | + return ( |
| 145 | + <OperationWrapper> |
| 146 | + <Button |
| 147 | + size="small" |
| 148 | + type="primary" |
| 149 | + ghost |
| 150 | + onClick={onEdit.bind(this, record)} |
| 151 | + > |
| 152 | + 编辑 |
| 153 | + </Button> |
| 154 | + <Space right="10px" /> |
| 155 | + <Button |
| 156 | + size="small" |
| 157 | + type="red" |
| 158 | + ghost |
| 159 | + onClick={onDelete.bind(this, record)} |
| 160 | + > |
| 161 | + 删除 |
| 162 | + </Button> |
| 163 | + </OperationWrapper> |
| 164 | + ) |
| 165 | + }, |
| 166 | + }, |
| 167 | + ] |
| 168 | + } |
| 169 | + |
| 170 | + render() { |
| 171 | + const { data, loading, pageChange } = this.props |
| 172 | + |
| 173 | + return ( |
| 174 | + <React.Fragment> |
| 175 | + {data ? ( |
| 176 | + <div> |
| 177 | + <Table |
| 178 | + columns={this.columns()} |
| 179 | + dataSource={data.entries} |
| 180 | + scroll={{ x: 2000 }} |
| 181 | + loading={TableLoading(loading)} |
| 182 | + pagination={false} |
| 183 | + /> |
| 184 | + <Pagi |
| 185 | + left="-10px" |
| 186 | + pageNumber={data.pageNumber} |
| 187 | + pageSize={data.pageSize} |
| 188 | + totalCount={data.totalCount} |
| 189 | + onChange={pageChange} |
| 190 | + /> |
| 191 | + </div> |
| 192 | + ) : null} |
| 193 | + </React.Fragment> |
| 194 | + ) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +PostsTable.propTypes = { |
| 199 | + data: PropTypes.any.isRequired, |
| 200 | + loading: PropTypes.bool, |
| 201 | + pageChange: PropTypes.func, |
| 202 | + setCommunity: PropTypes.func, |
| 203 | + unsetCommunity: PropTypes.func, |
| 204 | + unsetTag: PropTypes.func, |
| 205 | + setTag: PropTypes.func, |
| 206 | + onEdit: PropTypes.func, |
| 207 | + onDelete: PropTypes.func, |
| 208 | +} |
| 209 | + |
| 210 | +PostsTable.defaultProps = { |
| 211 | + loading: false, |
| 212 | + pageChange: debug, |
| 213 | + setCommunity: debug, |
| 214 | + unsetCommunity: debug, |
| 215 | + unsetTag: debug, |
| 216 | + setTag: debug, |
| 217 | + onEdit: debug, |
| 218 | + onDelete: debug, |
| 219 | +} |
| 220 | + |
| 221 | +export default React.memo(PostsTable) |
0 commit comments