SectionContainer.js 7.12 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';

import Immutable, {Map, List} from 'immutable';

import Section from '../components/section/Section';

import * as sectionActions from '../reducers/section/sectionActions';
import * as postingActions from '../reducers/posting/postingActions';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';
import {Actions} from 'react-native-router-flux';

const {
    View,
    StyleSheet,
    Dimensions,
    Platform,
    NativeModules,
    InteractionManager,
} = ReactNative;

/**
 * ## Actions
 * 3 of our actions will be available as ```actions```
 */
const actions = [
    sectionActions,
    postingActions,
    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 SectionContainer extends React.Component {

    constructor(props) {
        super(props);

        this._onPressBanner = this._onPressBanner.bind(this);
        this._onPressNotice = this._onPressNotice.bind(this);
        this._onPressPost = this._onPressPost.bind(this);
        this._onPressAvatar = this._onPressAvatar.bind(this);
        this._onPressSectionTag = this._onPressSectionTag.bind(this);
        this._onPressComment = this._onPressComment.bind(this);
        this._onPressLike = this._onPressLike.bind(this);
        this._onRefresh = this._onRefresh.bind(this);
        this._onEndReached = this._onEndReached.bind(this);

        this._onUploadRetry = this._onUploadRetry.bind(this);
        this._onUploadDelete = this._onUploadDelete.bind(this);
        this._onUploadSuccess = this._onUploadSuccess.bind(this);
        this._onSaveingTheWorld = this._onSaveingTheWorld.bind(this);

        this.sid = this.props.section.sid;
    }

    componentDidMount() {

    }

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

    _onPressBanner(url) {
        this.props.actions.goAction(url);
    }

    _onPressNotice(url) {
        this.props.actions.goAction(url);
    }

    _onPressPost(id) {
        this.props.actions.goToPost(id);
    }

    _onPressAvatar(uid) {
        this.props.actions.goToUserOrMe(uid);
    }

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

    _onPressComment(id) {
        this.props.actions.goToPost(id);
    }

    _onPressLike(post) {
        this.props.actions.likeOperation(post);
    }

    _onRefresh() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.header(this.sid);
            this.props.actions.hotPost(this.sid, true);
            this.props.actions.newPost(this.sid, true);
        });
    }

    _onEndReached(page) {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.setActiveTab(this.sid, page);
            if (page === 1) {
                this.props.actions.hotPost(this.sid, false);
            } else {
                this.props.actions.newPost(this.sid, false);
            }
        });
    }

    _onSaveingTheWorld() {
        this.props.actions.goToUser();
    }

    _onUploadSuccess() {

        this.props.actions.resetState();

        console.log('upload success!');
    }

    _onUploadRetry() {

        this.props.actions.startPosting(this.props.posting.assets.toJS());

        console.log('upload retry!');
    }

    _onUploadDelete() {
        this.props.actions.resetState();

        console.log('upload delete!');
    }

	render() {

        let {
            inPosting,
        	postFail,
        	finishedCount,
        	postPercent,
            assets,
            postingBannerShow,
            uploadState,
        } = this.props.posting;

        let assetsData = assets.toJS();
        let percent = postPercent;
        let uri = '';
        if (assetsData.length) {
            uri = assetsData[0].uri;
        }else {
            percent = 1;
        }

        let progressing = [];
        if (postingBannerShow) {
            progressing = [{
                uploadState,//(0发布中,1发布成功,2发布失败)
                progress:percent,//(0~1)
                image:uri,
                postingBannerShow,
            }];
        }
        progressing = Immutable.fromJS(progressing);


        let section = this.props.section.items.get(this.sid);
        let {activeTab, new: newPost, hot: hotPost, header, notice, ptr, isFetching : bnsIsFetching} = section;

        let isRefreshing, isLoadingMore, isFetching, endReached;
        if (activeTab === 0) {
            isRefreshing = bnsIsFetching;
            isLoadingMore = !ptr && newPost.isFetching;
            isFetching = newPost.isFetching;
            endReached = newPost.endReached;
        } else {
            isRefreshing = bnsIsFetching;
            isLoadingMore = !ptr && hotPost.isFetching;
            isFetching = hotPost.isFetching;
            endReached = hotPost.endReached;
        }

        let headerData = Immutable.fromJS([header]);
        let listData = Immutable.fromJS([{new: newPost.list, hot: hotPost.list}]);
        let noticeData = notice.open === 'Y' ? Immutable.fromJS([notice.list]) : List();
        let {profile} = this.props.user;

        return (
            <View style={styles.container}>
                <Section
                    progressing={progressing}
                    header={headerData}
                    notice={noticeData}
                    noticeOpen={notice.open}
                    noticeDuration={notice.duration}
                    list={listData}
                    user={profile}
                    onPressBanner={this._onPressBanner}
                    onPressNotice={this._onPressNotice}
                    onPressPost={this._onPressPost}
                    onPressAvatar={this._onPressAvatar}
                    onPressSectionTag={this._onPressSectionTag}
                    onPressComment={this._onPressComment}
                    onPressLike={this._onPressLike}
                    onRefresh={this._onRefresh}
                    isFetching={isFetching}
                    ptr={ptr}
                    endReached={endReached}
                    isRefreshing={isRefreshing}
                    isLoadingMore={isLoadingMore}
                    onEndReached={this._onEndReached}
                    onSaveingTheWorld={this._onSaveingTheWorld}
                />
            </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)(SectionContainer);