topic-mine.tsx 4.8 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import * as React from 'react';
import { Tabs, Table, Button } from 'component/antd';
import { cluster } from 'store/cluster';
import { observer } from 'mobx-react';
import { topic } from 'store/topic';
import { app } from 'store/app';
import { SearchAndFilterContainer } from 'container/search-filter';
import { ITopic } from 'types/base-type';
import { getExpireColumns, getMyTopicColumns } from './config';
import { applyTopic, deferTopic, applyOnlineModal } from 'container/modal';
import { pagination } from 'constants/table';
import { tableFilter } from 'lib/utils';
import { topicStatusMap } from 'constants/status-map';
import './index.less';

const { TabPane } = Tabs;

@observer
export class MineTopic extends SearchAndFilterContainer {
  public state = {
    searchKey: '',
    filterAccess: false,
  };

  public getData<T extends ITopic>(origin: T[]) {
    let data: T[] = [];
    let { searchKey } = this.state;
    searchKey = (searchKey + '').trim().toLowerCase();

    if (cluster.active !== -1 || app.active !== '-1' || searchKey !== '') {
      data = origin.filter(d =>
        ((d.topicName !== undefined && d.topicName !== null) && d.topicName.toLowerCase().includes(searchKey as string)
孙超 已提交
33
          || ((d.appName !== undefined && d.appName !== null) && d.appName.toLowerCase().includes(searchKey as string)))
Z
zengqiao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 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
        && (cluster.active === -1 || d.clusterId === cluster.active)
        && (app.active === '-1' || d.appId === (app.active + '')),
      );
    } else {
      data = origin;
    }
    return data;
  }

  public componentDidMount() {
    topic.getTopic();
    topic.getExpired();

    if (!cluster.clusterData.length) {
      cluster.getClusters();
    }
    if (!app.data.length) {
      app.getAppList();
    }
  }

  public renderOperationPanel(key: number) {
    return (
      <div className="table-operation-panel">
        <ul>
          {this.renderApp('关联应用:')}
          {this.renderCluster('集群:')}
          {this.renderSearch('名称:', '请输入Topic名称或者应用')}
          {
            key === 1 && <li className="right-btn-1">
              <Button type="primary" onClick={() => { applyTopic(); }}>
                申请Topic
              </Button>
            </li>
          }
        </ul>

      </div>
    );
  }

  public getColumns = (mytopicData: ITopic[]) => {
    const access = Object.assign({
      title: '权限',
      dataIndex: 'access',
      key: 'access',
      width: '10%',
      filters: tableFilter<ITopic>(mytopicData, 'access', topicStatusMap),
      onFilter: (text: number, record: ITopic) => record.access === text,
      render: (val: number) => (
        <div className={val === 0 ? '' : 'success'}>
          {topicStatusMap[val] || ''}
        </div>
      ),
    }, this.renderColumnsFilter('filterAccess')) as any;

    const columns = getMyTopicColumns(this.urlPrefix);

    columns.splice(-2, 0, access);

    return columns;
  }

  public renderMyTopicTable(mytopicData: ITopic[]) {
    return (
      <div>
        <Table
          rowKey="key"
          loading={topic.loading}
          dataSource={mytopicData}
          columns={this.getColumns(mytopicData)}
          pagination={pagination}
        />
      </div>
    );
  }

  public renderDeprecatedTopicTable(expireData: ITopic[]) {
    const operationCol = {
      title: '操作',
      dataIndex: 'action',
      key: 'action',
      width: '15%',
      render: (val: string, item: ITopic, index: number) => (
        <>
          <span>
            <a style={{ marginRight: 16 }} onClick={() => deferTopic(item)}>申请延期</a>
            <a style={{ marginRight: 16 }} onClick={() => applyOnlineModal(item)}>申请下线</a>
          </span>
        </>
      ),
    } as any;
    const expireColumns = getExpireColumns(this.urlPrefix);
    expireColumns.push(operationCol);

    return (
      <div>
        <Table
          rowKey="key"
          dataSource={expireData}
          columns={expireColumns}
          pagination={pagination}
          loading={topic.expiredLoading}
        />
      </div>
    );
  }

  public handleTabKey(key: string) {
    location.hash = key;
    cluster.changeCluster(-1);
    app.changeActiveApp('-1');
    this.setState({
      searchKey: '',
    });
    topic.setLoading(false);
  }

  public render() {
    return (
      <>
孙超 已提交
155 156 157 158 159 160 161 162 163 164 165 166
        <div className="min-width">
          <Tabs activeKey={location.hash.substr(1) || '1'} type="card" onChange={(key) => this.handleTabKey(key)}>
            <TabPane tab="有效Topic" key="1" >
              {this.renderOperationPanel(1)}
              {this.renderMyTopicTable(this.getData(topic.mytopicData))}
            </TabPane>
            <TabPane tab="已过期Topic" key="2">
              {this.renderOperationPanel(2)}
              {this.renderDeprecatedTopicTable(this.getData(topic.expireData))}
            </TabPane>
          </Tabs>
        </div>
Z
zengqiao 已提交
167 168 169 170
      </>
    );
  }
}