changeUserInfo.js 4.76 KB
/**
 * Created by Cassie on 2018/03/21
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    TextInput,
    Animated,
    ActivityIndicator,
    NativeModules,
    NativeEventEmitter,
    DeviceEventEmitter,
    Alert,
    Modal,
    Platform,
    AsyncStorage,
    StatusBar,
    Dimensions,
    Button
} from 'react-native';
import moment from 'moment';
import {zoomW,zoomH} from '../../utils/getSize';
import xnService from "../../service/AppService";

import {clock,xnToast,timeReduce,timePlus,timeEvery,getDay} from '../../utils/utils';

const StatusHeight = Platform.OS == 'ios' ?isIphoneX()?44+10/zoomH:20+10/zoomH:StatusBar.currentHeight;

export default class changeUserInfo extends Component {
    static navigationOptions = ({navigation}) => ({
        headerTitle:navigation.state.params.title,
        headerLeft:(
            <TouchableOpacity style={styles.backWrap} onPress={() => navigation.goBack()}>
                <Image source={require('../../img/back_gray.png')} resizeMode="contain" />
            </TouchableOpacity>
        ),
        headerRight:(
            <Button
                onPress={navigation.state.params?navigation.state.params.save:()=>console.log('onPress')}
                title="保存"
                color="#4b85e0"
            />
        )
    });

    constructor(props){
        super(props);
        this.state = {
            loading:false,
            text:this.props.navigation.state.params.defultValue
        };
    };

    componentWillMount(){

    };

    componentDidMount(){
        this.props.navigation.setParams({
            save:this.save,
        })
    }

    save=()=>{

        if (this.state.text == this.props.navigation.state.params.defultValue){
            xnToast('您并未做出修改');
            return;
        }

        this.setState({
            loading:true,
        });
        let params;

        if (this.props.navigation.state.params.type == 'nickName'){
            params = {
                id:global.user.id,
                nickName:this.state.text
            };
            global.user.nickName = this.state.text;
        }else if (this.props.navigation.state.params.type == 'age'){
            params = {
                id:global.user.id,
                age:Number(this.state.text)
            };
            global.user.age = this.state.text;

        }

        xnService.undateUserInfo(params).then((data) => {
            this.setState({
                loading: false
            });
            if (data.message) {
                xnToast(data.message);
                return;
            }
            if (data.errors.length > 0) {
                xnToast(data.errors[0].message);

                if (this.props.navigation.state.params.type == 'nickName'){

                    global.user.nickName = this.props.navigation.state.params.defultValue;
                }else if (this.props.navigation.state.params.type == 'age'){

                    global.user.age = this.props.navigation.state.params.defultValue;
                }

            } else {

                if (this.props.navigation.state.params.callback){
                    this.props.navigation.state.params.callback();
                    this.props.navigation.goBack();
                }
            }
        })
    }



    render(){
        return (
            <View style={styles.background}>
                <View style={styles.itemLine}>
                    <TextInput style={{fontSize:14,color:'#404040',flex:1}}
                               defaultValue={this.props.navigation.state.params.defultValue}
                               onChangeText={(text)=>this.state.text = text}
                               keyboardType={this.props.navigation.state.params.type == 'age'?'numeric':'default'}
                    ></TextInput>
                </View>
                {this.state.loading && <View style={styles.loadingBg}>
                    <ActivityIndicator size="large" />
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    backWrap: {
        justifyContent: 'center',
        paddingLeft: 18.5/zoomW,
        paddingRight: 18.5/zoomW,
        height: 44/zoomH,
    },
    background:{
        flex:1,
        backgroundColor:'#f6f6f6',
        display:'flex',
        alignItems:'center',

    },
    itemLine:{
        height:44/zoomH,
        width:'100%',
        backgroundColor:'white',
        paddingLeft:15/zoomW,
        paddingRight:15/zoomW,
        marginTop:12/zoomH
    }



});

// 是否 isIphoneX
export function isIphoneX() {
    const dimen = Dimensions.get('window');
    return (
        Platform.OS === 'ios' &&
        !Platform.isPad &&
        !Platform.isTVOS &&
        (dimen.height === 812 || dimen.width === 812)
    );
}