RecommentProducts.js 4 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import {getSlicedUrl} from '../../../classify/utils/Utils';
import Immutable, {Map} from 'immutable';
import YH_Image from '../../../common/components/YH_Image';
import DeviceInfo from 'react-native-device-info';

const {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  ListView,
  Dimensions,
  TouchableOpacity,
  PixelRatio,
  Platform,
} = ReactNative;

export default class RecommentProducts extends React.Component {
    constructor(props) {
      super(props);
      this.dataSource = new ListView.DataSource({
          rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
      });
    }

    shouldComponentUpdate(nextProps){

        if (Immutable.is(nextProps.resource, this.props.resource)) {
            return false;
        } else {
            return true;
        }
    }

    renderRow(rowData,sectionID,rowID,highlightRow) {

        let newSrc = getSlicedUrl(rowData.get('pic_url'), 140, 100, 2);
		let salePrice = rowData?parseFloat(rowData.get('sales_price')):0;
        let price = '¥' + salePrice.toFixed(2);
        let product_id = rowData.get('product_id')?rowData.get('product_id'):0;
        let rec_id = rowData.get('rec_id')?rowData.get('rec_id'):'';
        return (
            <TouchableOpacity activeOpacity={0.5} onPress={() => {
                let pos_id = 104;
                let params = {
                    REC_POSE: 100023,
                    PRD_ID: product_id,
                    PRD_NUM: rowID,
                    ACTION_ID: 1,
                    REC_ID: rec_id,
                };
                ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_CHOOSE_FOR_YOU', params);

				this.props.onPressProduct && this.props.onPressProduct(rowData,rowID,pos_id);
			}}>
                <View style={styles.thumbnailV}>
                    <YH_Image
                        url={newSrc}
                        style={styles.thumbnail}
                    />
					<View style={styles.titleView}>
						<Text style={styles.price}>{price}</Text>
	                </View>
                </View>
            </TouchableOpacity>
        );
    }

    render() {
		let {resource} = this.props;
        let data = resource.get('data');
        let dataGlobal = resource.get('dataGlobal');
        let dataLimit = resource.get('dataLimit');

        let productList = data.concat(dataGlobal);
        productList = productList.concat(dataLimit);

        if (!productList || productList.size == 0) {
			return (<View style={{height:1,width:width,backgroundColor:'white'}}/>);
		}

        return (
            <View style={styles.cellList}>
				<View style={{width: width,height: 15,backgroundColor: '#f0f0f0'}}/>
                <ListView
                    dataSource={this.dataSource.cloneWithRows(productList.toArray())}
                    horizontal={true}
                    renderRow={this.renderRow.bind(this)}
					showsHorizontalScrollIndicator={false}
                />
            </View>
        );
    }
};


let {width, height} = Dimensions.get('window');
const DEVICE_WIDTH_RATIO = width / 750;
let imageHeight = Math.ceil(225 * DEVICE_WIDTH_RATIO);
let imageWidth =   Math.ceil(175 * DEVICE_WIDTH_RATIO);
let itemHeight = imageHeight + 30 + 15;

let styles = StyleSheet.create({
    cellList:{
        justifyContent: 'center',
        width: Dimensions.get('window').width,
        height: itemHeight,
		backgroundColor: 'white',
    },
    thumbnailV: {
        marginLeft: 15,
		marginTop: 15,
		width: imageWidth,
		height: imageHeight,
        backgroundColor: 'white',
	},
    thumbnail: {
		width: imageWidth,
		height: imageHeight,
        backgroundColor: 'white',
	},
	titleView: {
        width: imageWidth,
        height: 20,
        position: 'absolute',
        backgroundColor: 'rgba(0,0,0,0.5)',
        top: imageHeight - 20,
        justifyContent: 'center',
    },
	price: {
        width: imageWidth,
        fontSize: 15,
        backgroundColor: 'transparent',
        color: 'white',
		textAlign: 'center',
    },
});