SubjectPostContainer.js 8.62 KB
'use strict';

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

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
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';
import * as appActions from '../reducers/app/appActions';
import {number10KFormater} from '../utils/numberFormater';
import timeago from '../utils/timeago';

const actions = [
    subjectPostActions,
    homeActions,
    appActions,
];

/**
 *  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.onPressDeleteComment = this.onPressDeleteComment.bind(this);
        this.sid = this.props.subject.sid;
    }

    onPressDeleteComment(id) {
        const BUTTONS = ['删除','取消'];
        ActionSheetIOS.showActionSheetWithOptions({
            options: BUTTONS,
            cancelButtonIndex: 1,
        },

        (buttonIndex) => {
            if (buttonIndex == 0) {
                this.props.actions.deleteComment(this.sid,id);
            }
        });

    }

    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);
            this.props.actions.prepareShareData(this.sid,item.id);
        });
    }

    componentWillReceiveProps(nextProps) {

    }

    componentWillUnmount() {
        this.props.actions.subjectClean(this.sid);
    }

    onEndReached() {
        let item = this.props.subject.items.get(this.sid);
        if (item.isCommentsFetching || item.commentReachEnd) {
            return;
        }
        this.props.actions.requestPostComments(this.sid, item.id, item.lastedTime);
    }

    onPressLargeImage(index) {
        let item = this.props.subject.items.get(this.sid);
        NativeModules.YH_CommunityHelper.showLargeImages(item.largeImages,index);
    }

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

    commentWithParams(params, fakeComment) {
        let item = this.props.subject.items.get(this.sid);
        let {blocks, replyTo} = fakeComment;
        let commentId = new Date();
        let cidTo = this.props.user.profile.uid;
        let timeago = '刚刚';
        let headIcon = this.props.user.profile.avatar;
        let nickName = this.props.user.profile.nickName;
        let LZ = false;
        let newFakeComment = {
            commentId,
            cidTo,
            timeago,
            headIcon,
            nickName,
            LZ,
            blocks,
            replyTo,
        }
        console.log(newFakeComment);
        let currentCommentList = item.commentList.toJS();
        let newList = [...currentCommentList,newFakeComment];
        params.forumCode = item.forumCode;
        this.props.actions.commentWithParams(this.sid,params,newList);
    }

    onPressShareGoods() {
        let item = this.props.subject.items.get(this.sid);
        let params = {
            productId: item.shareGoods.productId,
        };
        NativeModules.YH_CommunityHelper.displayProductDetailWithProductId(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,item.forumCode);
        } 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,item.forumCode);
        }else if(item.hasPraise == 'N'){
            this.props.actions.subjectLike(this.sid,item.id,item.forumCode);
        }
    }

    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(item.createTime);
        let header = {
            uid,
            headIcon,
            nickName,
            forumCode: item.forumCode,
            forumName: item.forumName,
            timeago: timeagoStr,
            LZ: item.LZ,
        }
        let praiseUsers = item.praiseUsers.toJS();
        if (praiseUsers.length>3) {
            praiseUsers = praiseUsers.slice(1,4);
        }
        let likeData = {
            praiseUsers,
            praise: number10KFormater(item.praise),
            browse: number10KFormater(item.browse),
            postId: item.id,
        }
        let shareGoods = [];
        if (item.shareGoods.productName.length) {
            shareGoods.push(item.shareGoods);
        }
        let titleBlob = [];
        if (item.postsTitle&&item.postsTitle.length) {
            titleBlob = [item.postsTitle];
        }
        let dataBlob = {
            header : [header],
            title: titleBlob,
            content: [item.blocks.toJS()],
            shareGoods,
            like: [likeData],
            comments: item.commentList.toJS(),
        };

        return (
            <View style={styles.container}>
                <SubjectPost
                    dataBlob={dataBlob}
                    onEndReached={this.onEndReached}
                    endReached={item.commentReachEnd}
                    isFetching={item.isCommentsFetching}
                    assetsSelected={this.assetsSelected}
                    assets={item.assets.toJS()}
                    commentCount={number10KFormater(item.commentCount||0)}
                    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}
                    onPressDeleteComment={this.onPressDeleteComment}
                    isContentFetching={item.isContentFetching}
                />
            </View>
        );
    }
}

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

let styles = StyleSheet.create({
    container: {
        top: navbarHeight,
        marginBottom: navbarHeight,
        flex: 1,
    },

});

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