-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathResetPassword.js
144 lines (125 loc) · 4.96 KB
/
ResetPassword.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
import React, {
Component,
ScrollView,
Text,
TouchableOpacity,
TouchableHighlight,
View,
TextInput,
ToastAndroid
} from 'react-native';
import api from './Server';
import styles from './Style';
export default class extends Component {
constructor(props) {
super(props);
this.state = {
data: {
phone: undefined,
resetPassCode: undefined,
password: undefined,
passwordd: undefined,
role: 2
},
loading: false,
messages: []
};
}
render() {
let fields = [
{ref: 'phone', placeholder: 'Phone Number', keyboardType: 'numeric', secureTextEntry: false, message: '* Phone number cannot be blank', style: [styles.inputText]},
{ref: 'resetPassCode', placeholder: 'Reset Code', keyboardType: 'default', secureTextEntry: false, message: '* Reset Code cannot be blank', style: [styles.inputText]},
{ref: 'password', placeholder: 'Password', keyboardType: 'default', secureTextEntry: true, message: '* Password cannot be blank', style: [styles.inputText]},
{ref: 'passwordd', placeholder: 'Password Confirmation', keyboardType: 'default', secureTextEntry: true, message: '* Password Confirmation cannot be blank', style: [styles.inputText]},
];
return(
<ScrollView ref={'resetForm'} {...this.props}>
<TouchableOpacity activeOpacity={1} style={styles.titleContainer}>
<Text style={styles.title}>{'RESET PASSWORD'}</Text>
</TouchableOpacity>
<View key={'messages'}>
{this.renderMessages()}
</View>
<View key={'phone'} style={styles.inputContainer}>
<TextInput {...fields[0]} onFocus={() => this.onFocus({...fields[0]})} onChangeText={(text) => this.state.data.phone = text} />
</View>
<View key={'resetPassCode'} style={styles.inputContainer}>
<TextInput {...fields[1]} onFocus={() => this.onFocus({...fields[1]})} onChangeText={(text) => this.state.data.resetPassCode = text} />
</View>
<View key={'password'} style={styles.inputContainer}>
<TextInput {...fields[2]} onFocus={() => this.onFocus({...fields[2]})} onChangeText={(text) => this.state.data.password = text} />
</View>
<View key={'passwordd'} style={styles.inputContainer}>
<TextInput {...fields[3]} onFocus={() => this.onFocus({...fields[3]})} onChangeText={(text) => this.state.data.passwordd = text} />
</View>
<TouchableHighlight style={this.state.loading ? styles.buttonDisabled : styles.button} underlayColor={'#2bbbad'} onPress={() => this.onSubmit(fields)}>
<Text style={styles.buttonText}>{this.state.loading ? 'Please Wait . . .' : 'Submit'}</Text>
</TouchableHighlight>
</ScrollView>
);
}
renderMessages() {
if (this.state.messages.length > 0) {
let messages = this.state.messages.map((val, key) => {
if (val.message) return <Text style={styles.message} key={key}>{val.message}</Text>;
});
return messages;
}
}
onFocus(argument) {
setTimeout(() => {
let scrollResponder = this.refs.resetForm.getScrollResponder();
scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
React.findNodeHandle(this.refs[argument.ref]), 110, true
);
}, 50);
}
onSubmit(argument) {
if (this.state.loading) {
ToastAndroid.show('Please Wait . . .', ToastAndroid.SHORT);
return null;
}
let keys = Object.keys(this.state.data).map((val, key) => {
if ([null, undefined, 'null', 'undefined', ''].indexOf(this.state.data[val]) > -1) return val;
});
this.setState({messages: []});
argument.map((val, key) => {
if (keys.indexOf(val.ref) > -1) this.setState({messages: this.state.messages.concat(val)});
});
if (this.state.messages.length > 0) return null;
this.setState({loading: true});
api.auth.reset(this.state.data)
.then((response) => {
if (!response.ok) throw Error(response.statusText || response._bodyText);
return response.json();
})
.then((responseData) => {
console.log(responseData);
ToastAndroid.show(JSON.stringify(responseData), ToastAndroid.LONG);
this.replaceRoute('login');
})
.catch((error) => {
console.log(error);
ToastAndroid.show(String(error).replace('Error: ',''), ToastAndroid.LONG);
})
.done(() => {
this.setState({loading: false});
});
}
goBack() {
if (this.props.navigator) {
this.props.navigator.pop();
}
}
gotoRoute(name) {
if (this.props.navigator && this.props.navigator.getCurrentRoutes()[this.props.navigator.getCurrentRoutes().length-1].name != name) {
this.props.navigator.push({name: name});
}
}
replaceRoute(name) {
if (this.props.navigator && this.props.navigator.getCurrentRoutes()[this.props.navigator.getCurrentRoutes().length-1].name != name) {
this.props.navigator.replace({name: name});
}
}
}