UserCenter.js 12.2 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
import AvatarList from '../../components/AvatarList';
import { formatWan } from '../../utils/utils';
ddcat1115's avatar
ddcat1115 committed
23
import styles from './UserCenter.less';
ddcat1115's avatar
ddcat1115 committed
24
import stylesProjects from '../List/Projects.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 37 38 39

@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'],
}))
export default class UserCenter extends PureComponent {
  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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  componentDidMount() {
    const { dispatch } = this.props;
    this.props.dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'list/fetch',
      payload: {
        count: 8,
      },
    });
    dispatch({
      type: 'project/fetchNotice',
    });
  }

  onTabChange = (key) => {
    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 69 70

  saveInputRef = (input) => {
    this.input = input;
jim's avatar
jim committed
71
  };
ddcat1115's avatar
ddcat1115 committed
72 73 74

  handleInputChange = (e) => {
    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;
jim's avatar
jim committed
81 82 83 84 85 86 87 88
    if (
      inputValue &&
      newTags.filter(tag => tag.label === inputValue).length === 0
    ) {
      newTags = [
        ...newTags,
        { key: `new-${newTags.length}`, label: inputValue },
      ];
ddcat1115's avatar
ddcat1115 committed
89 90 91 92 93 94
    }
    this.setState({
      newTags,
      inputVisible: false,
      inputValue: '',
    });
jim's avatar
jim committed
95
  };
ddcat1115's avatar
ddcat1115 committed
96

ddcat1115's avatar
ddcat1115 committed
97 98 99 100 101 102 103
  renderArticles = (list, loading) => {
    const IconText = ({ type, text }) => (
      <span>
        <Icon type={type} style={{ marginRight: 8 }} />
        {text}
      </span>
    );
jim's avatar
jim committed
104 105 106
    const ListContent = ({
      data: { content, updatedAt, avatar, owner, href },
    }) => (
ddcat1115's avatar
ddcat1115 committed
107 108 109
      <div className={stylesArticles.listContent}>
        <div className={stylesArticles.description}>{content}</div>
        <div className={stylesArticles.extra}>
jim's avatar
jim committed
110 111
          <Avatar src={avatar} size="small" />
          <a href={href}>{owner}</a> 发布在 <a href={href}>{href}</a>
ddcat1115's avatar
ddcat1115 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
          <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
134 135 136 137 138 139 140 141
              title={
                <a
                  className={stylesArticles.listItemMetaTitle}
                  href={item.href}
                >
                  {item.title}
                </a>
              }
ddcat1115's avatar
ddcat1115 committed
142 143 144 145 146 147 148 149 150 151 152 153 154
              description={
                <span>
                  <Tag>Ant Design</Tag>
                  <Tag>设计语言</Tag>
                  <Tag>蚂蚁金服</Tag>
                </span>
              }
            />
            <ListContent data={item} />
          </List.Item>
        )}
      />
    );
jim's avatar
jim committed
155
  };
ddcat1115's avatar
ddcat1115 committed
156 157 158 159 160

  renderApplications = (list, loading) => {
    const itemMenu = (
      <Menu>
        <Menu.Item>
jim's avatar
jim committed
161 162 163 164 165 166 167
          <a
            target="_blank"
            rel="noopener noreferrer"
            href="http://www.alipay.com/"
          >
            1st menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
168 169
        </Menu.Item>
        <Menu.Item>
jim's avatar
jim committed
170 171 172 173 174 175 176
          <a
            target="_blank"
            rel="noopener noreferrer"
            href="http://www.taobao.com/"
          >
            2nd menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
177 178
        </Menu.Item>
        <Menu.Item>
jim's avatar
jim committed
179 180 181 182 183 184 185
          <a
            target="_blank"
            rel="noopener noreferrer"
            href="http://www.tmall.com/"
          >
            3d menu item
          </a>
ddcat1115's avatar
ddcat1115 committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        </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
214 215 216 217 218 219 220 221 222 223 224 225
                <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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
              ]}
            >
              <Card.Meta
                avatar={<Avatar size="small" src={item.avatar} />}
                title={item.title}
              />
              <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
243
  };
ddcat1115's avatar
ddcat1115 committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257

  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
258
              cover={<img alt={item.title} src={item.cover} />}
ddcat1115's avatar
ddcat1115 committed
259 260 261 262 263 264 265 266 267
            >
              <Card.Meta
                title={<a href="#">{item.title}</a>}
                description={item.subDescription}
              />
              <div className={stylesProjects.cardItemContent}>
                <span>{moment(item.updatedAt).fromNow()}</span>
                <div className={stylesProjects.avatarList}>
                  <AvatarList size="mini">
jim's avatar
jim committed
268 269 270 271 272 273 274
                    {item.members.map(member => (
                      <AvatarList.Item
                        key={`${item.id}-avatar-${member.id}`}
                        src={member.avatar}
                        tips={member.name}
                      />
                    ))}
ddcat1115's avatar
ddcat1115 committed
275 276 277 278 279 280 281 282
                  </AvatarList>
                </div>
              </div>
            </Card>
          </List.Item>
        )}
      />
    );
jim's avatar
jim committed
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 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
  };
  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>
          {currentUser.tags
            .concat(newTags)
            .map(item => <Tag key={item.key}>{item.label}</Tag>)}
          {inputVisible && (
            <Input
              ref={this.saveInputRef}
              type="text"
              size="small"
              style={{ width: 78 }}
              value={inputValue}
              onChange={this.handleInputChange}
              onBlur={this.handleInputConfirm}
              onPressEnter={this.handleInputConfirm}
            />
          )}
          {!inputVisible && (
            <Tag
              onClick={this.showInput}
              style={{ background: '#fff', borderStyle: 'dashed' }}
            >
              <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
380 381
  }
  render() {
jim's avatar
jim committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    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
414 415 416 417 418 419 420
    const contentMap = {
      article: this.renderArticles(list, listLoading),
      application: this.renderApplications(list, listLoading),
      project: this.renderProjects(list, listLoading),
    };

    return (
jim's avatar
jim committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
      <GridContent>
        <div className={styles.userCenter}>
          <Row gutter={24}>
            <Col lg={7} md={24}>
              <Card
                bordered={false}
                style={{ marginBottom: 24 }}
                loading={currentUserLoading}
              >
                {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}
              >
                {contentMap[this.state.key]}
              </Card>
            </Col>
          </Row>
        </div>
      </GridContent>
ddcat1115's avatar
ddcat1115 committed
448 449 450
    );
  }
}