SystemMessageContainer.js 3.11 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import Immutable, {Map} from 'immutable';


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

import SystemMessage from '../components/message/SystemMessage';
import * as messageActions from '../reducers/message/messageActions';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';


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

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

function mapStateToProps(state) {
    return {
        ...state
    };
}

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

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

class SystemMessageContainer extends React.Component {
    constructor(props) {
        super(props);

        this._onPressSection = this._onPressSection.bind(this);
        this._onRefresh = this._onRefresh.bind(this);
        this._onEndReached = this._onEndReached.bind(this);
        this._onPressPost = this._onPressPost.bind(this);
        this._onPressSection = this._onPressSection.bind(this);
    }

    componentDidMount() {
        this._onEndReached();
    }

    _onPressPost(id) {
        console.log('post id = ' + id);
        if (id) {
            this.props.actions.goToPost(id);
        }
    }

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


    _onRefresh() {
        // console.log('_onRefresh  id =' + id);
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.systemMessage(true);
        });
    }

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

    render() {

        let {system} = this.props.message;
        let {ptr, isFetching, list, endReached} = system;
        let isRefreshing = ptr;
        let isLoadingMore = isFetching;

        return (
            <View style={styles.container}>
                <SystemMessage
                    list={list}
                    isFetching={isFetching}
                    ptr={ptr}
                    endReached={endReached}
                    isRefreshing={isRefreshing}
                    isLoadingMore={isLoadingMore}
                    onRefresh={this._onRefresh}
                    onPressSection={this._onPressSection}
                    onEndReached={this._onEndReached}
                    onPressPost={this._onPressPost}
                />
            </View>
        );
    }
}


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

let styles = StyleSheet.create({
    container: {
        // top: 0,
        height: height,
        backgroundColor: 'transparent'
    },

});


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