UserThatNotMeContainer.js 3.98 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 {Actions} from 'react-native-router-flux';
import UserThatNotMe from '../components/userThatNotMe/UserThatNotMe';
import UserNavBar from '../components/user/UserNavBar';
import * as userThatNotMeActions from '../reducers/userThatNotMe/userThatNotMeActions';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';

const {
    StatusBar,
    ScrollView,
    View,
    StyleSheet,
    Dimensions,
    Platform,
    ActionSheetIOS,
    InteractionManager,
    Animated,
} = ReactNative;

const actions = [
    userThatNotMeActions,
    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 UserThatNotMeContainer extends React.Component {
    constructor(props) {
        super(props);

        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._onEndReached = this._onEndReached.bind(this);

        this.sid = this.props.userThatNotMe.sid;
    }

    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.getUserThatNotMeInfo(this.sid);
        });
        this._onEndReached();
    }

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

    _onPressAvatar(uid) {

    }

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

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

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

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

    _onEndReached() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.userThatNotMePosts(this.sid);
        });
    }

    render() {
        let userThatNotMe = this.props.userThatNotMe.items.get(this.sid);
        if (!userThatNotMe) {
            __DEV__ && console.log('=========illegal state=========');
            return <UserNavBar
                scrollValue={new Animated.Value(0)}
                channel={this.props.app.channel}
                useDefault={true}
            />;
        }

        let {profile, posts} = userThatNotMe;
        let postsData = Immutable.fromJS(posts.list);
        return (
            <View style={styles.container}>
                <UserThatNotMe
                    channel={this.props.app.channel}
                    user={profile}
                    posts={postsData}
                    isFetching={posts.isFetching}
                    endReached={posts.endReached}
                    isLoadingMore={posts.isFetching}
                    onEndReached={this._onEndReached}
                    onPressAvatar={this._onPressAvatar}
                    onPressSectionTag={this._onPressSectionTag}
                    onPressComment={this._onPressComment}
                    onPressLike={this._onPressLike}
                    onPressPost={this._onPressPost}
                />
            </View>
        );
    }

}

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

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

});

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