HomeContainer.js 6.61 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 Home from '../components/home/Home';

import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';

import {Actions} from 'react-native-router-flux';

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

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

    constructor(props) {
        super(props);

        this._onPressBanner = this._onPressBanner.bind(this);
        this._onPressNotice = this._onPressNotice.bind(this);
        this._onPressSection = this._onPressSection.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);

        if (parseInt(this.props.app.container) === 1) {
            this.subscription = NativeAppEventEmitter.addListener(
                'ChannelReminder',
                (reminder) => {
                    this.props.actions.setChannel(reminder.channel);
                    Actions.refresh({channel: reminder.channel});
                }
            );
        }
    }

    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            this._onRefresh();
        });
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.showNativeTabBar) {
            ReactNative.NativeModules.YH_CommunityHelper.showTabBar();
        }
    }

    componentWillUnmount() {
        if (parseInt(this.props.app.container) === 1) {
            this.subscription && this.subscription.remove();
        }
    }

    _onPressBanner(url) {
        console.log('banner');

    }

    _onPressNotice(url) {
        console.log('notice');
    }

    _onPressSection(section) {
        this.props.actions.goToSection(section, this.props.navigationState.name);
    }

    _onPressPost(url) {
        console.log('post');
    }

    _onPressAvatar(url) {
        console.log('avatar');
    }

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

    _onPressComment(url) {
        console.log('comment');
    }

    _onPressLike(url) {
        console.log('like');
    }

    _onRefresh() {
        this.props.actions.bannerNoticeSection();
        this.props.actions.recommendation(true);
    }

    _onEndReached() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.recommendation(false);
        });
    }

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

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

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

	render() {
        let progressing = [{
            uploadState:0,//(0发布中,1发布成功,2发布失败)
            progress:0.2,//(0~1)
            image:'https://img10.static.yhbimg.com/yhb-img01/2016/06/28/17/0180ef2077db7ec5117756e47c0b61a7b1.jpg?imageView2/2/w/640/h/240',
        },{
            uploadState:1,//(0发布中,1发布成功,2发布失败)
            progress:0.2,//(0~1)
            image:'',
        },{
            uploadState:2,//(0发布中,1发布成功,2发布失败)
            progress:0.2,//(0~1)
            image:'',
        }];

        progressing = Immutable.fromJS(progressing);

        let {banner, notice, section, recommendation, isFetching, ptr, sync} = this.props.home;
        let bannerData = Immutable.fromJS([banner.list]);
        let noticeData = notice.open === 'Y' ? Immutable.fromJS([notice.list]) : List();
        let sectionData = Immutable.fromJS([section]);
        let recommendationData = Immutable.fromJS(recommendation.list);

        return (
            <View style={styles.container}>
                <StatusBar
                    hidden={false}
                    barStyle={'light-content'}
                />
                <Home
                    progressing={progressing}
                    banner={bannerData}
                    bannerDuration={banner.duration}
                    notice={noticeData}
                    noticeOpen={notice.open}
                    noticeDuration={notice.duration}
                    section={sectionData}
                    recommendation={recommendationData}
                    onPressBanner={this._onPressBanner}
                    onPressNotice={this._onPressNotice}
                    onPressSection={this._onPressSection}
                    onPressPost={this._onPressPost}
                    onPressAvatar={this._onPressAvatar}
                    onPressSectionTag={this._onPressSectionTag}
                    onPressComment={this._onPressComment}
                    onPressLike={this._onPressLike}
                    onRefresh={this._onRefresh}
                    isFetching={isFetching}
                    ptr={ptr}
                    isRefreshing={ptr && isFetching}
                    isLoadingMore={!ptr && isFetching}
                    onEndReached={this._onEndReached}
                    onUploadSuccess={this._onUploadSuccess}
                    onUploadRetry={this._onUploadRetry}
                    onUploadDelete={this._onUploadDelete}
                    isSyncFetching={sync.isFetching}

                />
            </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,
        flex: 1,
    },

});

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