SubjectPostContainer.js 6.34 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);
    }

    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.requestPostContent(this.props.postId);
            this.props.actions.requestPostComments(this.props.postId);
        });
    }

    componentWillUnmount() {

    }

    onEndReached() {
        if (this.props.subject.currentPage>=this.props.subject.totalPages) {
            return;
        }
        this.props.actions.requestPostComments(300, this.props.subject.lastedTime);
    }

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

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

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

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

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

    onPressLikeOrDislike() {
        if (this.props.subject.inLikeAction) {
            return;
        }
        if (this.props.subject.hasPraise == 'Y') {
            this.props.actions.subjectUnlike(this.props.subject.id);
        }else if(this.props.subject.hasPraise == 'N'){
            this.props.actions.subjectLike(this.props.subject.id);
        }
    }

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

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

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