RankBody.js 3.52 KB
import React, { Component, PureComponent } from 'react';
import { Dimensions, Image, StyleSheet, Text, TouchableOpacity, View, VirtualizedList } from 'react-native';


import YH_Image from '../../common/components/YH_Image';
import { getSlicedUrl } from '../../classify/utils/Utils';

const DEVICE_WIDTH_RATIO = Dimensions.get('window').width / 375;

export default class RankBody extends Component {
  render() {
    return (
      <View style={this.props.style}>
        <VirtualizedList
          ListHeaderComponent={this._header}
          keyExtractor={(item, index) => index}
          renderItem={({item, index}) => this._renderItem(item, index)}
          getItemLayout={(data, index) => ({length: 70 * DEVICE_WIDTH_RATIO, offset: 70 * DEVICE_WIDTH_RATIO * index, index})}
          getItemCount={(data) => data.size || 0}
          getItem={(data, index) => data.get(index)}
          data={this.props.dataSource}
          onEndReachedThreshold={0.5}
          onEndReached={() => this.props.fetchNextPage(this.props.monthRank ? 1 : 2)}
          showsVerticalScrollIndicator={false}>
        </VirtualizedList>
      </View>
    );
  }

  _renderItem(item, index) {
    return <RankItem key={index} monthRank={this.props.monthRank} item={item}/>
  }
  _header() {
    return <View style={styles.view10}/>
  }
}

class RankItem extends PureComponent {
  render() {
    let { item, monthRank } = this.props;
    item = item.toJS();
    const { nickname, amountStr, rankNum, image } = item;
    return (
      <View style={styles.itemContainer}>
        <View style={styles.row}>
          <Text style={styles.rank} numberOfLines={1}>{rankNum}</Text>
          {this._renderImage(image)}
          <Text style={styles.name} numberOfLines={1}>{nickname}</Text>
          <View style={styles.rightContainer}>
            <Text style={styles.amount} numberOfLines={1}>{amountStr}</Text>
            <Text style={styles.amountName}>{`${monthRank ? '本月' : '总'}预估佣金`}</Text>
          </View>
        </View>
      </View>
    )
  }

  _renderImage(url) {
    if (url) {
      url = getSlicedUrl(url, 40 * DEVICE_WIDTH_RATIO, 40 * DEVICE_WIDTH_RATIO, 2);
      return <YH_Image style={styles.avatar} url={url} circle={true} />
    } else {
      return <Image source={require('../../groupPurchase/images/PT_head.png')} style={styles.avatar} />
    }
  }
}

let { width } = Dimensions.get('window');

const styles = StyleSheet.create({
  itemContainer: {
    height: 70 * DEVICE_WIDTH_RATIO,
    paddingVertical: 2,
    borderBottomWidth: 0.5 * DEVICE_WIDTH_RATIO,
    borderColor: '#E0E0E0',
  },
  row: {
    flexDirection: 'row',
    flex: 1,
    alignItems: 'center',
  },
  rank: {
    fontSize: 18,
    color: '#444444',
    width: 23,
    marginLeft: 18 * DEVICE_WIDTH_RATIO,
  },
  avatar: {
    height: 40 * DEVICE_WIDTH_RATIO,
    width: 40 * DEVICE_WIDTH_RATIO,
    overflow: 'hidden',
    borderRadius: 20 * DEVICE_WIDTH_RATIO,
    marginLeft: 15 * DEVICE_WIDTH_RATIO,
  },
  name: {
    fontFamily: 'PingFang-SC-Regular',
    fontSize: 14,
    marginLeft: 10 * DEVICE_WIDTH_RATIO,
    maxWidth: width - 230 * DEVICE_WIDTH_RATIO,
  },
  rightContainer: {
    flex: 1,
    alignItems: 'flex-end',
    paddingRight: 15 * DEVICE_WIDTH_RATIO,
    justifyContent: 'center',
  },
  amount: {
    fontSize: 16,
    color: '#444444',
    fontWeight: 'bold',
  },
  amountName: {
    fontFamily: 'PingFangSC-Regular',
    marginTop: 4 * DEVICE_WIDTH_RATIO,
    fontSize: 12,
    color: '#B0B0B0'
  },
  view10: {
    height: 10 * DEVICE_WIDTH_RATIO,
    backgroundColor: '#F0F0F0',
  },
})