Center.js 11.7 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2
import React, { PureComponent } from 'react';
import { connect } from 'dva';
ddcat1115's avatar
ddcat1115 committed
3
import { Link } from 'dva/router';
ddcat1115's avatar
ddcat1115 committed
4 5
import moment from 'moment';
import numeral from 'numeral';
jim's avatar
jim committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import {
  List,
  Card,
  Row,
  Col,
  Icon,
  Dropdown,
  Menu,
  Avatar,
  Tag,
  Divider,
  Tooltip,
  Spin,
  Input,
} from 'antd';
ddcat1115's avatar
ddcat1115 committed
21 22 23
import AvatarList from '../../components/AvatarList';
import { formatWan } from '../../utils/utils';
import stylesProjects from '../List/Projects.less';
ddcat1115's avatar
ddcat1115 committed
24
import styles from './Center.less';
jim's avatar
jim committed
25 26 27
import stylesArticles from '../List/Articles.less';
import stylesApplications from '../List/Applications.less';
import GridContent from '../../layouts/GridContent';
ddcat1115's avatar
ddcat1115 committed
28 29 30 31 32 33 34 35 36

@connect(({ list, loading, user, project }) => ({
  list,
  listLoading: loading.effects['list/fetch'],
  currentUser: user.currentUser,
  currentUserLoading: loading.effects['user/fetchCurrent'],
  project,
  projectLoading: loading.effects['project/fetchNotice'],
}))
ddcat1115's avatar
ddcat1115 committed
37
export default class Center extends PureComponent {
ddcat1115's avatar
ddcat1115 committed
38 39
  state = {
    key: 'article',
ddcat1115's avatar
ddcat1115 committed
40 41 42
    newTags: [],
    inputVisible: false,
    inputValue: '',
jim's avatar
jim committed
43
  };
ddcat1115's avatar
ddcat1115 committed
44 45 46

  componentDidMount() {
    const { dispatch } = this.props;
陈帅's avatar
陈帅 committed
47
    dispatch({
ddcat1115's avatar
ddcat1115 committed
48 49 50 51 52 53 54 55 56 57 58 59 60
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'list/fetch',
      payload: {
        count: 8,
      },
    });
    dispatch({
      type: 'project/fetchNotice',
    });
  }

陈帅's avatar
陈帅 committed
61
  onTabChange = key => {
ddcat1115's avatar
ddcat1115 committed
62
    this.setState({ key });
jim's avatar
jim committed
63
  };
ddcat1115's avatar
ddcat1115 committed
64

ddcat1115's avatar
ddcat1115 committed
65 66
  showInput = () => {
    this.setState({ inputVisible: true }, () => this.input.focus());
jim's avatar
jim committed
67
  };
ddcat1115's avatar
ddcat1115 committed
68

陈帅's avatar
陈帅 committed
69
  saveInputRef = input => {
ddcat1115's avatar
ddcat1115 committed
70
    this.input = input;
jim's avatar
jim committed
71
  };
ddcat1115's avatar
ddcat1115 committed
72

陈帅's avatar
陈帅 committed
73
  handleInputChange = e => {
ddcat1115's avatar
ddcat1115 committed
74
    this.setState({ inputValue: e.target.value });
jim's avatar
jim committed
75
  };
ddcat1115's avatar
ddcat1115 committed
76 77 78 79 80

  handleInputConfirm = () => {
    const { state } = this;
    const { inputValue } = state;
    let { newTags } = state;
陈帅's avatar
陈帅 committed
81 82
    if (inputValue && newTags.filter(tag => tag.label === inputValue).length === 0) {
      newTags = [...newTags, { key: `new-${newTags.length}`, label: inputValue }];
ddcat1115's avatar
ddcat1115 committed
83 84 85 86 87 88
    }
    this.setState({
      newTags,
      inputVisible: false,
      inputValue: '',
    });
jim's avatar
jim committed
89
  };
ddcat1115's avatar
ddcat1115 committed
90

ddcat1115's avatar
ddcat1115 committed
91 92 93 94 95 96 97
  renderArticles = (list, loading) => {
    const IconText = ({ type, text }) => (
      <span>
        <Icon type={type} style={{ marginRight: 8 }} />
        {text}
      </span>
    );
陈帅's avatar
陈帅 committed
98
    const ListContent = ({ data: { content, updatedAt, avatar, owner, href } }) => (
ddcat1115's avatar
ddcat1115 committed
99 100 101
      <div className={stylesArticles.listContent}>
        <div className={stylesArticles.description}>{content}</div>
        <div className={stylesArticles.extra}>
jim's avatar
jim committed
102 103
          <Avatar src={avatar} size="small" />
          <a href={href}>{owner}</a> 发布在 <a href={href}>{href}</a>
ddcat1115's avatar
ddcat1115 committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
          <em>{moment(updatedAt).format('YYYY-MM-DD HH:mm')}</em>
        </div>
      </div>
    );
    return (
      <List
        size="large"
        className={styles.articleList}
        loading={loading}
        rowKey="id"
        itemLayout="vertical"
        dataSource={list}
        renderItem={item => (
          <List.Item
            key={item.id}
            actions={[
              <IconText type="star-o" text={item.star} />,
              <IconText type="like-o" text={item.like} />,
              <IconText type="message" text={item.message} />,
            ]}
          >
            <List.Item.Meta
jim's avatar
jim committed
126
              title={
陈帅's avatar
陈帅 committed
127
                <a className={stylesArticles.listItemMetaTitle} href={item.href}>
jim's avatar
jim committed
128 129 130
                  {item.title}
                </a>
              }
ddcat1115's avatar
ddcat1115 committed
131 132 133 134 135 136 137 138 139 140 141 142 143
              description={
                <span>
                  <Tag>Ant Design</Tag>
                  <Tag>设计语言</Tag>
                  <Tag>蚂蚁金服</Tag>
                </span>
              }
            />
            <ListContent data={item} />
          </List.Item>
        )}
      />
    );
jim's avatar
jim committed
144
  };
ddcat1115's avatar
ddcat1115 committed
145 146 147 148 149

  renderApplications = (list, loading) => {
    const itemMenu = (
      <Menu>
        <Menu.Item>
陈帅's avatar
陈帅 committed
150
          <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
jim's avatar
jim committed
151 152
            1st menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
153 154
        </Menu.Item>
        <Menu.Item>
陈帅's avatar
陈帅 committed
155
          <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">
jim's avatar
jim committed
156 157
            2nd menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
158 159
        </Menu.Item>
        <Menu.Item>
陈帅's avatar
陈帅 committed
160
          <a target="_blank" rel="noopener noreferrer" href="http://www.tmall.com/">
jim's avatar
jim committed
161 162
            3d menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        </Menu.Item>
      </Menu>
    );
    const CardInfo = ({ activeUser, newUser }) => (
      <div className={stylesApplications.cardInfo}>
        <div>
          <p>活跃用户</p>
          <p>{activeUser}</p>
        </div>
        <div>
          <p>新增用户</p>
          <p>{newUser}</p>
        </div>
      </div>
    );
    return (
      <List
        rowKey="id"
        className={stylesApplications.filterCardList}
        grid={{ gutter: 24, xxl: 3, xl: 2, lg: 2, md: 2, sm: 2, xs: 1 }}
        loading={loading}
        dataSource={list}
        renderItem={item => (
          <List.Item key={item.id}>
            <Card
              hoverable
              bodyStyle={{ paddingBottom: 20 }}
              actions={[
jim's avatar
jim committed
191 192 193 194 195 196 197 198 199 200 201 202
                <Tooltip title="下载">
                  <Icon type="download" />
                </Tooltip>,
                <Tooltip title="编辑">
                  <Icon type="edit" />
                </Tooltip>,
                <Tooltip title="分享">
                  <Icon type="share-alt" />
                </Tooltip>,
                <Dropdown overlay={itemMenu}>
                  <Icon type="ellipsis" />
                </Dropdown>,
ddcat1115's avatar
ddcat1115 committed
203 204
              ]}
            >
陈帅's avatar
陈帅 committed
205
              <Card.Meta avatar={<Avatar size="small" src={item.avatar} />} title={item.title} />
ddcat1115's avatar
ddcat1115 committed
206 207 208 209 210 211 212 213 214 215 216
              <div className={stylesApplications.cardItemContent}>
                <CardInfo
                  activeUser={formatWan(item.activeUser)}
                  newUser={numeral(item.newUser).format('0,0')}
                />
              </div>
            </Card>
          </List.Item>
        )}
      />
    );
jim's avatar
jim committed
217
  };
ddcat1115's avatar
ddcat1115 committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231

  renderProjects = (list, loading) => {
    return (
      <List
        className={stylesProjects.coverCardList}
        rowKey="id"
        loading={loading}
        grid={{ gutter: 24, xxl: 3, xl: 2, lg: 2, md: 2, sm: 2, xs: 1 }}
        dataSource={list}
        renderItem={item => (
          <List.Item>
            <Card
              className={stylesProjects.card}
              hoverable
ddcat1115's avatar
ddcat1115 committed
232
              cover={<img alt={item.title} src={item.cover} />}
ddcat1115's avatar
ddcat1115 committed
233
            >
陈帅's avatar
陈帅 committed
234
              <Card.Meta title={<a href="#">{item.title}</a>} description={item.subDescription} />
ddcat1115's avatar
ddcat1115 committed
235 236 237 238
              <div className={stylesProjects.cardItemContent}>
                <span>{moment(item.updatedAt).fromNow()}</span>
                <div className={stylesProjects.avatarList}>
                  <AvatarList size="mini">
jim's avatar
jim committed
239 240 241 242 243 244 245
                    {item.members.map(member => (
                      <AvatarList.Item
                        key={`${item.id}-avatar-${member.id}`}
                        src={member.avatar}
                        tips={member.name}
                      />
                    ))}
ddcat1115's avatar
ddcat1115 committed
246 247 248 249 250 251 252 253
                  </AvatarList>
                </div>
              </div>
            </Card>
          </List.Item>
        )}
      />
    );
jim's avatar
jim committed
254
  };
陈帅's avatar
陈帅 committed
255

jim's avatar
jim committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  renderContent() {
    const { newTags, inputVisible, inputValue } = this.state;
    const {
      currentUser,
      project: { notice },
      projectLoading,
    } = this.props;
    return (
      <div>
        <div className={styles.avatarHolder}>
          <img alt="" src={currentUser.avatar} />
          <div className={styles.name}>{currentUser.name}</div>
          <div>{currentUser.signature}</div>
        </div>
        <div className={styles.detail}>
          <p>
            <i className={styles.title} />
            {currentUser.title}
          </p>
          <p>
            <i className={styles.group} />
            {currentUser.group}
          </p>
          <p>
            <i className={styles.address} />
            {currentUser.geographic.province.label}
            {currentUser.geographic.city.label}
          </p>
        </div>
        <Divider dashed />
        <div className={styles.tags}>
          <div className={styles.tagsTitle}>标签</div>
          {currentUser.tags.map(item => <Tag key={item.key}>{item.label}</Tag>)}
          <Tag style={{ background: '#fff', borderStyle: 'dashed' }}>
            <Icon type="plus" />
          </Tag>
        </div>
        <Divider dashed />
        <div className={styles.team}>
          <div className={styles.teamTitle}>团队</div>
          <Spin spinning={projectLoading}>
            <Row gutter={36}>
              {notice.map(item => (
                <Col key={item.id} className={styles.item} lg={24} xl={12}>
                  <Avatar size="small" src={item.logo} />
                  {item.member}
                </Col>
              ))}
            </Row>
          </Spin>
        </div>
        <Divider dashed />
        <div className={styles.tags}>
          <div className={styles.tagsTitle}>标签</div>
陈帅's avatar
陈帅 committed
310
          {currentUser.tags.concat(newTags).map(item => <Tag key={item.key}>{item.label}</Tag>)}
jim's avatar
jim committed
311 312 313 314 315 316 317 318 319 320 321 322 323
          {inputVisible && (
            <Input
              ref={this.saveInputRef}
              type="text"
              size="small"
              style={{ width: 78 }}
              value={inputValue}
              onChange={this.handleInputChange}
              onBlur={this.handleInputConfirm}
              onPressEnter={this.handleInputConfirm}
            />
          )}
          {!inputVisible && (
陈帅's avatar
陈帅 committed
324
            <Tag onClick={this.showInput} style={{ background: '#fff', borderStyle: 'dashed' }}>
jim's avatar
jim committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
              <Icon type="plus" />
            </Tag>
          )}
        </div>
        <Divider style={{ marginTop: 16 }} dashed />
        <div className={styles.team}>
          <div className={styles.teamTitle}>团队</div>
          <Spin spinning={projectLoading}>
            <Row gutter={36}>
              {notice.map(item => (
                <Col key={item.id} lg={24} xl={12}>
                  <Link to={item.href}>
                    <Avatar size="small" src={item.logo} />
                    {item.member}
                  </Link>
                </Col>
              ))}
            </Row>
          </Spin>
        </div>
      </div>
    );
ddcat1115's avatar
ddcat1115 committed
347
  }
陈帅's avatar
陈帅 committed
348

ddcat1115's avatar
ddcat1115 committed
349
  render() {
jim's avatar
jim committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    const {
      list: { list },
      listLoading,
      currentUser,
      currentUserLoading,
    } = this.props;
    const operationTabList = [
      {
        key: 'article',
        tab: (
          <span>
            文章 <span style={{ fontSize: 14 }}>(8)</span>
          </span>
        ),
      },
      {
        key: 'application',
        tab: (
          <span>
            应用 <span style={{ fontSize: 14 }}>(8)</span>
          </span>
        ),
      },
      {
        key: 'project',
        tab: (
          <span>
            项目 <span style={{ fontSize: 14 }}>(8)</span>
          </span>
        ),
      },
    ];
ddcat1115's avatar
ddcat1115 committed
382 383 384 385 386
    const contentMap = {
      article: this.renderArticles(list, listLoading),
      application: this.renderApplications(list, listLoading),
      project: this.renderProjects(list, listLoading),
    };
陈帅's avatar
陈帅 committed
387
    const { key } = this.state;
ddcat1115's avatar
ddcat1115 committed
388
    return (
jim's avatar
jim committed
389 390 391 392
      <GridContent>
        <div className={styles.userCenter}>
          <Row gutter={24}>
            <Col lg={7} md={24}>
陈帅's avatar
陈帅 committed
393
              <Card bordered={false} style={{ marginBottom: 24 }} loading={currentUserLoading}>
jim's avatar
jim committed
394 395 396 397 398 399 400 401 402 403 404 405
                {currentUser && Object.keys(currentUser).length
                  ? this.renderContent()
                  : 'loading...'}
              </Card>
            </Col>
            <Col lg={17} md={24}>
              <Card
                className={styles.tabsCard}
                bordered={false}
                tabList={operationTabList}
                onTabChange={this.onTabChange}
              >
陈帅's avatar
陈帅 committed
406
                {contentMap[key]}
jim's avatar
jim committed
407 408 409 410 411
              </Card>
            </Col>
          </Row>
        </div>
      </GridContent>
ddcat1115's avatar
ddcat1115 committed
412 413 414
    );
  }
}