LoginContainer.js 3.47 KB
'use strict';

import React from 'react-native';

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

import {Map} from 'immutable';

import * as userActions from '../reducers/user/userActions';
import * as deviceActions from '../reducers/device/deviceActions';
import Login from '../components/Login';

let {
    Component,
    View,
    Text,
    StyleSheet,
    Image,
    TextInput,
    Dimensions,
    Alert
} = React;

/**
 * ## Actions
 * 3 of our actions will be available as ```actions```
 */
const actions = [
    userActions,
    deviceActions,
];

/**
 *  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
    };
}

export default class LoginContainer extends Component {

    constructor(props) {
        super(props);
        this.onLoginPress = this.onLoginPress.bind(this);
        this.onDeleteUserPress = this.onDeleteUserPress.bind(this);
        this.onShowPwdPress = this.onShowPwdPress.bind(this);
        this.onAccountChange = this.onAccountChange.bind(this);
        this.onPwdChange = this.onPwdChange.bind(this);
    }

    componentDidMount() {

    }

    componentWillUnmount() {

    }
    onAccountChange(text:string){
        this.props.actions.accountChange(text);
    }

    onPwdChange(text:string){
        this.props.actions.pwdChange(text);
    }

    onLoginPress() {
      Alert.alert(
              '登录',
              this.props.user.profile.account+'+'+this.props.user.profile.password,
              [
                {text: 'OK', onPress: () => this.props.actions.login(this.props.user.profile.account,this.props.user.profile.password)},
              ]
            );

    }
    onDeleteUserPress() {
      this.props.actions.accountClean();
    }
    onShowPwdPress() {
      this.props.actions.pwdDisplay(this.props.user.isShowPwd?false:true);
    }
	render() {
        return (
          <Login onLoginPress={this.onLoginPress}
                 onDeleteUserPress={this.onDeleteUserPress}
                 onShowPwdPress={this.onShowPwdPress}
                 onAccountChange={this.onAccountChange}
                 onPwdChange={this.onPwdChange}
                 isFetching={this.props.user.isFetching}
                 account={this.props.user.profile.account}
                 pwd={this.props.user.profile.password}
                 isDisplayPwd={this.props.user.isShowPwd}/>

        );
    }
}
let {height, width} = Dimensions.get('window');
let textPaddingTop = height * 7 / 10;
let buttonMargin = (width - 165) / 2;
var styles = StyleSheet.create({

  logo: {
    width:256,
    height:40,
    marginBottom: 10,
  },
  username: {
    width:335,
    height:40,
    marginTop:55,
    borderColor: 'gray',
    borderWidth: 1
  },
  pwd: {
    width:335,
    height:40,
    marginTop:13,
    borderColor: 'gray',
    borderWidth: 1
  },
  button: {
      marginTop: 40,
      marginLeft: buttonMargin,
      marginRight: buttonMargin,
      backgroundColor: '#000000',
      borderRadius:5,
  },
  buttonText: {
      color: '#ffffff',
  },
});

/**
 * Connect the properties
 */
export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);