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