index.tsx 7.42 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { Avatar, Card, Col, Divider, Icon, Input, Row, Tag } from 'antd';
2
import React, { PureComponent } from 'react';
陈帅's avatar
陈帅 committed
3

4
import { Dispatch } from 'redux';
陈帅's avatar
陈帅 committed
5
import { GridContent } from '@ant-design/pro-layout';
陈帅's avatar
陈帅 committed
6
import Link from 'umi/link';
7
import { RouteChildrenProps } from 'react-router';
陈帅's avatar
陈帅 committed
8
import { connect } from 'dva';
9
import { ModalState } from './model';
陈帅's avatar
陈帅 committed
10
import Projects from './components/Projects';
陈帅's avatar
陈帅 committed
11 12
import Articles from './components/Articles';
import Applications from './components/Applications';
陈帅's avatar
陈帅 committed
13
import { CurrentUser, TagType } from './data.d';
陈帅's avatar
陈帅 committed
14
import styles from './Center.less';
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

const operationTabList = [
  {
    key: 'articles',
    tab: (
      <span>
        文章 <span style={{ fontSize: 14 }}>(8)</span>
      </span>
    ),
  },
  {
    key: 'applications',
    tab: (
      <span>
        应用 <span style={{ fontSize: 14 }}>(8)</span>
      </span>
    ),
  },
  {
    key: 'projects',
    tab: (
      <span>
        项目 <span style={{ fontSize: 14 }}>(8)</span>
      </span>
    ),
  },
];

43
interface BLOCK_NAME_CAMEL_CASEProps extends RouteChildrenProps {
陈帅's avatar
陈帅 committed
44
  dispatch: Dispatch<any>;
45 46 47 48
  currentUser: CurrentUser;
  currentUserLoading: boolean;
}
interface BLOCK_NAME_CAMEL_CASEState {
陈帅's avatar
陈帅 committed
49
  newTags: TagType[];
陈帅's avatar
陈帅 committed
50
  tabKey: 'articles' | 'applications' | 'projects';
51 52 53 54 55 56 57 58 59
  inputVisible: boolean;
  inputValue: string;
}

@connect(
  ({
    loading,
    BLOCK_NAME_CAMEL_CASE,
  }: {
陈帅's avatar
陈帅 committed
60
    loading: { effects: { [key: string]: boolean } };
61 62 63 64
    BLOCK_NAME_CAMEL_CASE: ModalState;
  }) => ({
    currentUser: BLOCK_NAME_CAMEL_CASE.currentUser,
    currentUserLoading: loading.effects['BLOCK_NAME_CAMEL_CASE/fetchCurrent'],
陈帅's avatar
陈帅 committed
65
  }),
66 67 68 69 70
)
class PAGE_NAME_UPPER_CAMEL_CASE extends PureComponent<
  BLOCK_NAME_CAMEL_CASEProps,
  BLOCK_NAME_CAMEL_CASEState
> {
陈帅's avatar
陈帅 committed
71 72 73 74 75 76 77
  // static getDerivedStateFromProps(
  //   props: BLOCK_NAME_CAMEL_CASEProps,
  //   state: BLOCK_NAME_CAMEL_CASEState,
  // ) {
  //   const { match, location } = props;
  //   const { tabKey } = state;
  //   const path = match && match.path;
78

陈帅's avatar
陈帅 committed
79 80 81 82 83 84
  //   const urlTabKey = location.pathname.replace(`${path}/`, '');
  //   if (urlTabKey && urlTabKey !== '/' && tabKey !== urlTabKey) {
  //     return {
  //       tabKey: urlTabKey,
  //     };
  //   }
85

陈帅's avatar
陈帅 committed
86 87
  //   return null;
  // }
88

89
  state: BLOCK_NAME_CAMEL_CASEState = {
90 91 92 93 94 95 96 97 98 99 100
    newTags: [],
    inputVisible: false,
    inputValue: '',
    tabKey: 'articles',
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'BLOCK_NAME_CAMEL_CASE/fetchCurrent',
    });
陈帅's avatar
陈帅 committed
101 102 103
    dispatch({
      type: 'BLOCK_NAME_CAMEL_CASE/fetch',
    });
104 105
  }

106
  onTabChange = (key: string) => {
107 108 109 110
    // If you need to sync state to url
    // const { match } = this.props;
    // router.push(`${match.url}/${key}`);
    this.setState({
陈帅's avatar
陈帅 committed
111
      tabKey: key as BLOCK_NAME_CAMEL_CASEState['tabKey'],
112 113 114 115
    });
  };

  showInput = () => {
116
    this.setState({ inputVisible: true }, () => this.input && this.input.focus());
117 118
  };

119
  saveInputRef = (input: Input | null) => {
120 121 122
    this.input = input;
  };

123
  handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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: '',
    });
  };
陈帅's avatar
陈帅 committed
140

陈帅's avatar
陈帅 committed
141 142
  public input: Input | null | undefined;

陈帅's avatar
陈帅 committed
143 144 145 146 147 148 149 150 151 152 153 154
  renderChildrenByTabKey = (tabKey: BLOCK_NAME_CAMEL_CASEState['tabKey']) => {
    if (tabKey === 'projects') {
      return <Projects />;
    }
    if (tabKey === 'applications') {
      return <Applications />;
    }
    if (tabKey === 'articles') {
      return <Articles />;
    }
    return null;
  };
陈帅's avatar
陈帅 committed
155

156 157
  render() {
    const { newTags, inputVisible, inputValue, tabKey } = this.state;
陈帅's avatar
陈帅 committed
158
    const { currentUser, currentUserLoading } = this.props;
159 160
    const dataLoading = currentUserLoading || !(currentUser && Object.keys(currentUser).length);
    return (
陈帅's avatar
陈帅 committed
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
      <GridContent>
        <Row gutter={24}>
          <Col lg={7} md={24}>
            <Card bordered={false} style={{ marginBottom: 24 }} loading={dataLoading}>
              {!dataLoading ? (
                <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>
陈帅's avatar
陈帅 committed
190 191 192
                    {currentUser.tags.concat(newTags).map(item => (
                      <Tag key={item.key}>{item.label}</Tag>
                    ))}
陈帅's avatar
陈帅 committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                    {inputVisible && (
                      <Input
                        ref={ref => this.saveInputRef(ref)}
                        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>
                    <Row gutter={36}>
                      {currentUser.notice &&
                        currentUser.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>
                  </div>
229
                </div>
陈帅's avatar
陈帅 committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
              ) : null}
            </Card>
          </Col>
          <Col lg={17} md={24}>
            <Card
              className={styles.tabsCard}
              bordered={false}
              tabList={operationTabList}
              activeTabKey={tabKey}
              onTabChange={this.onTabChange}
            >
              {this.renderChildrenByTabKey(tabKey)}
            </Card>
          </Col>
        </Row>
      </GridContent>
246 247 248 249 250
    );
  }
}

export default PAGE_NAME_UPPER_CAMEL_CASE;