SubjectPostContainer.js 6.6 KB
'use strict';

import React, {Component} from 'react';
import {
    View,
    StyleSheet,
    Dimensions,
    Platform,
    InteractionManager,
    NativeModules,
} from 'react-native';

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import timeago from 'timeago.js';
import Immutable, {List, Record} from 'immutable';
import {Actions} from 'react-native-router-flux';

import SubjectPost from '../components/subjectPost/SubjectPost';

import * as subjectPostActions from '../reducers/subject/subjectPostActions';
import * as homeActions from '../reducers/home/homeActions';


const actions = [
    subjectPostActions,
    homeActions,
];

/**
 *  Save that state
 */
function mapStateToProps(state) {
    return {
        ...state
    };
}

/**
 * Bind all the functions from the ```actions``` and bind them with
 * ```dispatch```
 */
function mapDispatchToProps(dispatch) {

    const creators = Map()
        .merge(...actions)
        .filter(value => typeof value === 'function')
        .toObject();

    return {
        actions: bindActionCreators(creators, dispatch),
        dispatch
    };
}

class SubjectPostContainer extends Component {
    constructor(props) {
        super(props);
        this.onEndReached = this.onEndReached.bind(this);
        this.assetsSelected = this.assetsSelected.bind(this);
        this.commentWithParams = this.commentWithParams.bind(this);
        this.onPressAvatar = this.onPressAvatar.bind(this);
        this.onPressSection = this.onPressSection.bind(this);
        this.onPressReportOrDelete = this.onPressReportOrDelete.bind(this);
        this.onPressLikeCell = this.onPressLikeCell.bind(this);
        this.onPressLikeOrDislike = this.onPressLikeOrDislike.bind(this);
        this.onPressShareGoods = this.onPressShareGoods.bind(this);
        this.onPressLargeImage = this.onPressLargeImage.bind(this);
        this.sid = this.props.subject.sid;
    }

    componentDidMount() {
        let item = this.props.subject.items.get(this.sid);
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.requestPostContent(this.sid,item.id);
            this.props.actions.requestPostComments(this.sid,item.id);
        });
    }

    componentWillUnmount() {

    }

    onEndReached() {
        let item = this.props.subject.items.get(this.sid);
        if (item.currentPage>=item.totalPages) {
            return;
        }
        this.props.actions.requestPostComments(this.sid, item.id, item.lastedTime);
    }

    onPressLargeImage(index) {
        NativeModules.YH_CommunityHelper.showLargeImages(this.props.subject.largeImages,index);
    }

    assetsSelected(assets) {
        this.props.actions.assetsSelected(this.sid,assets);
    }

    commentWithParams(params) {
        this.props.actions.commentWithParams(this.sid,params);
    }

    onPressShareGoods() {
        let item = this.props.subject.items.get(this.sid);
        let params = {
            productSkn: item.shareProductSkn,
        };
        NativeModules.YH_CommunityHelper.displayProductDetail(params);
    }
    onPressAvatar(uid) {
        this.props.actions.goToUserOrMe(uid);
    }

    onPressSection(section) {
        this.props.actions.goToSection(section);
    }

    onPressReportOrDelete() {
        let item = this.props.subject.items.get(this.sid);
        if (item.LZ) {
            this.props.actions.doDelete(this.sid,item.id);
        } else {
            let params = {
                postsId: item.id,
                loginUid: this.props.user.profile.uid,
                contentData: '涉黄',
            }
            this.props.actions.doReport(this.sid,params);
        }
    }

    onPressLikeOrDislike() {
        let item = this.props.subject.items.get(this.sid);
        if (item.inLikeAction) {
            return;
        }
        if (item.hasPraise == 'Y') {
            this.props.actions.subjectUnlike(this.sid,item.id);
        }else if(this.props.subject.hasPraise == 'N'){
            this.props.actions.subjectLike(this.sid,item.id);
        }
    }

    onPressLikeCell(postId, likeCount) {
        this.props.actions.goToLikeList(postId, likeCount);
    }

    render() {
        let item = this.props.subject.items.get(this.sid);
        let {headIcon,nickName,uid} = item.authorInfo;
        let timeagoStr = timeago().format(item.createTime, 'zh_CN');
        let header = {
            uid,
            headIcon,
            nickName,
            forumCode: item.forumCode,
            forumName: item.forumName,
            timeago: timeagoStr,
            LZ: item.LZ,
        }
        let likeData = {
            praiseUsers: item.praiseUsers.toJS(),
            praise: item.praise,
            browse: item.browse,
            postId: item.id,
        }
        let shareGoods = [];
        if (item.shareGoods.productName.length) {
            shareGoods.push(item.shareGoods);
        }
        let dataBlob = {
            header : [header],
            title: [item.postsTitle],
            content: [item.blocks.toJS()],
            shareGoods,
            like: [likeData],
            comments: item.commentList.toJS(),
        };

        return (
            <View style={styles.container}>
                <SubjectPost
                    dataBlob={dataBlob}
                    onEndReached={this.onEndReached}
                    endReached={item.currentPage>=item.totalPages}
                    isFetching={item.isCommentsFetching}
                    assetsSelected={this.assetsSelected}
                    assets={item.assets.toJS()}
                    commentCount={item.commentCount}
                    commentWithParams={this.commentWithParams}
                    postId={item.id}
                    cidFrom={this.props.user.profile.uid}
                    authorUid={item.authorInfo.uid}
                    onPressAvatar={this.onPressAvatar}
                    onPressSection={this.onPressSection}
                    onPressReportOrDelete={this.onPressReportOrDelete}
                    onPressLikeCell={this.onPressLikeCell}
                    onPressLikeOrDislike={this.onPressLikeOrDislike}
                    likeState={item.hasPraise}
                    onPressShareGoods={this.onPressShareGoods}
                    onPressLargeImage={this.onPressLargeImage}
                />
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');
let navbarHeight = (Platform.OS === 'android') ? 50 : 64;

let styles = StyleSheet.create({
    container: {
        top: navbarHeight,
        height: height - navbarHeight - 49,
    },

});

export default connect(mapStateToProps, mapDispatchToProps)(SubjectPostContainer);