list.js 5.1 KB
Newer Older
崔佳豪's avatar
崔佳豪 committed
1 2 3
import React from 'react';

import { List, Spin, ConfigProvider } from 'antd';
杨思琦's avatar
杨思琦 committed
4
// eslint-disable-next-line import/no-unresolved
崔佳豪's avatar
崔佳豪 committed
5 6
import classNames from 'classnames';
import './style/list.less';
7
import noMessage from '@/assets/images/system/notifications/没有消息.png';
崔佳豪's avatar
崔佳豪 committed
8 9 10 11 12 13 14
import { Alarm, Case, Notice, Unknown } from './templates';
import { MESSAGE_TYPE } from './constants';
const Empty = ({ emptyText, ...props }) => {
  const { getPrefixCls } = React.useContext(ConfigProvider.ConfigContext);
  const prefixCls = props.prefixCls || getPrefixCls();
  return (
    <div className={`${prefixCls}-notFound`}>
15 16
      {/* <img src="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg" alt="not found" /> */}
      <img src={noMessage} alt="not found" />
崔佳豪's avatar
崔佳豪 committed
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 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
      <div>{emptyText}</div>
    </div>
  );
};

class NoticeList extends React.Component {
  constructor(props) {
    super(props);
    this.emptyText = props.emptyText;
    this.confirmRead = props.confirmRead;
    this.handlerSysDetail = props.handlerSysDetail;
    this.handlerUnknowDetail = props.handlerUnknowDetail;
    this.loadMore = props.loadMore;
    this.hasMore = props.hasMore;
    this.container = React.createRef();
    this.state = {
      isLoading: false,
    };
    this.mounted = false;
    this.handleScrollCallback = this.throttle(this.handleScroll.bind(this), 30).bind(this);
  }

  componentDidMount() {
    this.mounted = true;
    if (this.container.current) {
      this.container.current.addEventListener('scroll', this.handleScrollCallback);
    }
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  throttle(fn, wait) {
    /* eslint-disable */
    let pre = Date.now();
    return function () {
      const context = this;
      // eslint-disable-next-line prefer-rest-params
      const args = arguments;
      const now = Date.now();
      if (now - pre >= wait) {
        fn.apply(context, args);
        pre = Date.now();
      }
    };
  }

  handleScroll(e) {
    e.stopPropagation();
    if (!this.mounted) return;
    if (!this.container.current) return;
    const { current } = this.container;
    if (current.scrollHeight - current.scrollTop - current.offsetHeight <= 100) {
      this.handleLoadMore();
    }
  }

  handleLoadMore() {
    if (this.state.isLoading) return;
    if (!this.hasMore()) return;
    this.setState(
      {
        isLoading: true,
      },
      () => {
        if (!this.loadMore) return;
        this.loadMore().then((data) => {
          if (!this.mounted) return;
          this.setState({
            isLoading: false,
          });
        });
      },
    );
  }

  render() {
    const { getPrefixCls } = this.context;
    const prefixCls =
      this.props.prefixCls || getPrefixCls ? getPrefixCls('head-notifier-list') : '';

    if (!this.props.data || this.props.data.length === 0) {
      return <Empty emptyText={this.emptyText} prefixCls={prefixCls} />;
    }
    return (
      <div className={`${prefixCls}-container`} ref={this.container}>
        <List
          className={`${prefixCls}-list`}
          dataSource={this.props.data}
          renderItem={(item, i) => {
            const itemCls = classNames(`${prefixCls}-item`, {
              [`${prefixCls}-read`]: item.read,
            });
            let messageTemplate = <></>;
            switch (item.infoClasses) {
              case MESSAGE_TYPE.ALARM_TYPE:
                messageTemplate = (
杨思琦's avatar
杨思琦 committed
115
                  <Alarm message={item} confirmRead={this.confirmRead} config={this.props.config} handlerUnknowDetail={this.handlerUnknowDetail} />
崔佳豪's avatar
崔佳豪 committed
116 117 118
                );
                break;
              case MESSAGE_TYPE.CASE_TYPE:
杨思琦's avatar
杨思琦 committed
119
                messageTemplate = <Case message={item} confirmRead={this.confirmRead} handlerUnknowDetail={this.handlerUnknowDetail} />;
崔佳豪's avatar
崔佳豪 committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                break;
              case MESSAGE_TYPE.SYS_TYPE:
                messageTemplate = (
                  <Notice
                    message={item}
                    confirmRead={this.confirmRead}
                    config={this.props.config}
                    handlerSysDetail={this.handlerSysDetail}
                  />
                );
                break;
              default:
                messageTemplate = (
                  <Unknown
                    message={item}
                    confirmRead={this.confirmRead}
                    handlerUnknowDetail={this.handlerUnknowDetail}
                  />
                );
                break;
            }
            return (
杨思琦's avatar
杨思琦 committed
142
              <List.Item className={itemCls} key={`${item.id}-${item.dateTime}` || i}>
崔佳豪's avatar
崔佳豪 committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                {messageTemplate}
              </List.Item>
            );
          }}
        />
        <div className={`${prefixCls}-bottomBar`}>
          {this.state.isLoading ? (
            <>
              <Spin /> 加载中...
            </>
          ) : this.hasMore() ? (
            <span>下拉加载更多</span>
          ) : (
            <span style={{ fontSize: '12px', color: 'rgba(0, 0, 0, 0.6)' }}>
              已经没有更多消息了
            </span>
          )}
        </div>
      </div>
    );
  }
}
NoticeList.contextType = ConfigProvider.ConfigContext;
export default NoticeList;