UserCenter.js 11.3 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 6
import moment from 'moment';
import numeral from 'numeral';
import { List, Card, Row, Col, Icon, Dropdown,
ddcat1115's avatar
ddcat1115 committed
7
  Menu, Avatar, Tag, Divider, Tooltip, Spin, Input } from 'antd';
ddcat1115's avatar
ddcat1115 committed
8 9
import AvatarList from '../../components/AvatarList';
import { formatWan } from '../../utils/utils';
ddcat1115's avatar
ddcat1115 committed
10
import styles from './UserCenter.less';
ddcat1115's avatar
ddcat1115 committed
11 12 13
import stylesArticles from '../List/Articles.less';
import stylesApplications from '../List/Applications.less';
import stylesProjects from '../List/Projects.less';
ddcat1115's avatar
ddcat1115 committed
14 15 16 17 18 19 20 21 22 23 24 25

@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
26 27 28
    newTags: [],
    inputVisible: false,
    inputValue: '',
ddcat1115's avatar
ddcat1115 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  }

  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 });
  }

ddcat1115's avatar
ddcat1115 committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  showInput = () => {
    this.setState({ inputVisible: true }, () => this.input.focus());
  }

  saveInputRef = (input) => {
    this.input = input;
  }

  handleInputChange = (e) => {
    this.setState({ inputValue: e.target.value });
  }

  handleInputConfirm = () => {
    const { state } = this;
    const { inputValue } = state;
    let { newTags } = state;
    if (inputValue && newTags.filter(tag => tag.label === inputValue).length === 0) {
      newTags = [...newTags, { key: `new-${newTags.length}`, label: inputValue }];
    }
    this.setState({
      newTags,
      inputVisible: false,
      inputValue: '',
    });
  }

ddcat1115's avatar
ddcat1115 committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 191 192 193 194 195 196 197 198 199 200 201 202 203
  renderArticles = (list, loading) => {
    const IconText = ({ type, text }) => (
      <span>
        <Icon type={type} style={{ marginRight: 8 }} />
        {text}
      </span>
    );
    const ListContent = ({ data: { content, updatedAt, avatar, owner, href } }) => (
      <div className={stylesArticles.listContent}>
        <div className={stylesArticles.description}>{content}</div>
        <div className={stylesArticles.extra}>
          <Avatar src={avatar} size="small" /><a href={href}>{owner}</a> 发布在 <a href={href}>{href}</a>
          <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
              title={(
                <a className={stylesArticles.listItemMetaTitle} href={item.href}>{item.title}</a>
              )}
              description={
                <span>
                  <Tag>Ant Design</Tag>
                  <Tag>设计语言</Tag>
                  <Tag>蚂蚁金服</Tag>
                </span>
              }
            />
            <ListContent data={item} />
          </List.Item>
        )}
      />
    );
  }

  renderApplications = (list, loading) => {
    const itemMenu = (
      <Menu>
        <Menu.Item>
          <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">1st menu item</a>
        </Menu.Item>
        <Menu.Item>
          <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">2nd menu item</a>
        </Menu.Item>
        <Menu.Item>
          <a target="_blank" rel="noopener noreferrer" href="http://www.tmall.com/">3d menu item</a>
        </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={[
                <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>,
              ]}
            >
              <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>
        )}
      />
    );
  }

  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
204
              cover={<img alt={item.title} src={item.cover} />}
ddcat1115's avatar
ddcat1115 committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
            >
              <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">
                    {
                      item.members.map(member => (
                        <AvatarList.Item
                          key={`${item.id}-avatar-${member.id}`}
                          src={member.avatar}
                          tips={member.name}
                        />
                      ))
                    }
                  </AvatarList>
                </div>
              </div>
            </Card>
          </List.Item>
        )}
      />
    );
  }

  render() {
ddcat1115's avatar
ddcat1115 committed
234
    const { key, newTags, inputVisible, inputValue } = this.state;
ddcat1115's avatar
ddcat1115 committed
235 236 237 238
    const { list: { list }, listLoading, currentUser, currentUserLoading,
      project: { notice }, projectLoading } = this.props;
    const operationTabList = [{
      key: 'article',
ddcat1115's avatar
ddcat1115 committed
239
      tab: <span>文章 <span style={{ fontSize: 14 }}>(8)</span></span>,
ddcat1115's avatar
ddcat1115 committed
240 241
    }, {
      key: 'application',
ddcat1115's avatar
ddcat1115 committed
242
      tab: <span>应用 <span style={{ fontSize: 14 }}>(8)</span></span>,
ddcat1115's avatar
ddcat1115 committed
243 244
    }, {
      key: 'project',
ddcat1115's avatar
ddcat1115 committed
245
      tab: <span>项目 <span style={{ fontSize: 14 }}>(8)</span></span>,
ddcat1115's avatar
ddcat1115 committed
246 247 248 249 250 251 252 253 254 255 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
    }];
    const contentMap = {
      article: this.renderArticles(list, listLoading),
      application: this.renderApplications(list, listLoading),
      project: this.renderProjects(list, listLoading),
    };

    return (
      <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 ?
                  (
                    <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>
                        {
ddcat1115's avatar
ddcat1115 committed
283 284
                          currentUser.tags.concat(newTags).map(item =>
                            <Tag key={item.key}>{item.label}</Tag>)
ddcat1115's avatar
ddcat1115 committed
285
                        }
ddcat1115's avatar
ddcat1115 committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                        {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>
                        )}
ddcat1115's avatar
ddcat1115 committed
306
                      </div>
ddcat1115's avatar
ddcat1115 committed
307
                      <Divider style={{ marginTop: 16 }} dashed />
ddcat1115's avatar
ddcat1115 committed
308 309 310 311 312 313
                      <div className={styles.team}>
                        <div className={styles.teamTitle}>团队</div>
                        <Spin spinning={projectLoading}>
                          <Row gutter={36}>
                            {
                              notice.map(item => (
ddcat1115's avatar
ddcat1115 committed
314 315 316 317 318
                                <Col key={item.id} lg={24} xl={12}>
                                  <Link to={item.href}>
                                    <Avatar size="small" src={item.logo} />
                                    {item.member}
                                  </Link>
ddcat1115's avatar
ddcat1115 committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                                </Col>
                              ))
                            }
                          </Row>
                        </Spin>
                      </div>
                    </div>
                  ) : 'loading...'
              }
            </Card>
          </Col>
          <Col lg={17} md={24}>
            <Card
              className={styles.tabsCard}
              bordered={false}
              tabList={operationTabList}
              onTabChange={this.onTabChange}
            >
ddcat1115's avatar
ddcat1115 committed
337
              {contentMap[key]}
ddcat1115's avatar
ddcat1115 committed
338 339 340 341 342 343 344
            </Card>
          </Col>
        </Row>
      </div>
    );
  }
}