SystemMessageContainer.js 3.02 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) {
        if (id) {
            this.props.actions.goToPost(id);
        }
    }

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


    _onRefresh() {
        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: navbarHeight,
        marginBottom: navbarHeight,
        flex: 1,
    },

});


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