onLoginFound={function(data){ console.log("Existing login found."); console.log(data); _this.setState({ username: data.profile.email, password: data.profile.id, }); Parse.User.logIn(_this.state.username, _this.state.password, { success: (user) => { console.log('login success'+ JSON.stringify(user, null, 4)); _this.props.navigator.immediatelyResetRouteStack([{name: 'home'}]); // console.log(user); }, error: (data, error) => { var errorText; switch(error.code){ case 101: errorText="Invalid username or password." break; case 100: errorText="Unable to connect to the internet." break; default : errorText="Something went wrong." break; } console.log('login error: '+ JSON.stringify(error, null, 4)); _this.onFacebookAuthSignUp(_this.state.username, _this.state.password); } }); _this.setState({ loader: HiddenLoader }) }}
API: function(){ var _this = this; this.setState({ isReloadRequired: false, loaded: false, isRefreshing: true, isEnabled: false }); Parse.Cloud.run('fetchBookingListForUserCloudFunction', { user_id: Parse.User.current().getUsername() }).then( function(result){ var cleanData = []; for(var i=0;i<result.length;i++){ cleanData.push(result[i].toJSON()); } _this.setState({ rawData: cleanData, dataSource: _this.state.dataSource.cloneWithRows(cleanData), loaded: true, isReloadRequired: false, isRefreshing: false, isEnabled: true }); _this.updateCache(); }, function(error){ _this.setState({ isReloadRequired: true, loaded: false, isRefreshing: false, isEnabled: true }) console.log("[HOME API] Error: "+ JSON.stringify(error, null, 2)); } ); },
onFacebookAuthSignUp: function(email, id){ var _this = this; console.log('inside signup'); Parse.User.logOut(); var user = new Parse.User(); user.set('username', email); user.set('password', id); console.log('calling api...'); user.signUp(null, { success: (user) => { console.log(user); _this.props.navigator.immediatelyResetRouteStack([{name: 'home'}]); }, error: (user, error) => { console.log(error); _this.setState({ error: error.message }) } }); }
onPressConfirm: function(){ if(this.state.disableSubmit){ return; } if(!this.state.title.trim()){ this.setState({errorTitle: 'You need a cool title!'}); return; } var _this = this; this.setState({ disableSubmit: true, buttonColor: '#939393' }); var _bookFromTime = parseFloat(Moment(this.props.params.inTime, "H.m").subtract(Moment().utcOffset(), "minutes").format("H.mm")); var _bookToTime = parseFloat(Moment(this.props.params.outTime, "H.m").subtract(Moment().utcOffset(), "minutes").format("H.mm")); var _bookDate = Moment(Moment(this.props.params.date).format("D-M-YYYY") + " " + Moment(this.props.params.inTime, "H:m").format("H.m"), "D-M-YYYY H:m").subtract(Moment().utcOffset(), "minutes").format("D-M-YYYY"); var _roomMacId = this.props.data.room_mac_id; var _userId = Parse.User.current().getUsername(); var _title = this.state.title.trim(); var _description = this.state.description.trim(); var _statusId = 1; Parse.Cloud.run('bookRoomFromAppCloudFunction', { book_fromtime: _bookFromTime, book_totime: _bookToTime, book_date: _bookDate, room_mac_id: _roomMacId, user_id: _userId, title: _title, description: _description, status_id: _statusId }).then( async function(result){ await AsyncStorage.removeItem('FORCE_UPDATE'); await AsyncStorage.setItem('FORCE_UPDATE', JSON.stringify(true)); _this.props.navigator.replace({name: 'success', data: { date: Moment(_bookDate, "D-M-YYYY").format("MMMM Do YYYY"), room: _this.props.data.room_name, loadData: _this.props.params.loadData} }); console.log("[NEW BOOKING API] Success: "+ JSON.stringify(result, null, 2)); }, function(error){ _this.setState({ disableSubmit: false, buttonColor: '#2196F3' }); if(error.code===141){ _this.props.navigator.replace({name: 'failure', data: { date: Moment(_bookDate, "D-M-YYYY").format("MMMM Do YYYY"), room: _this.props.data.room_name, loadData: _this.props.params.loadData} }); } console.log("[NEW BOOKING API] Error: "+ JSON.stringify(error, null, 2)); } ); },
onSignInPress: function(){ if(this.state.username===""){ return this.setState({ error: 'Username is missing.' }); } if(this.state.password===""){ return this.setState({ error: 'Password is missing.' }); } this.setState({ loader: VisibleLoader }); Parse.User.logIn(this.state.username, this.state.password, { success: (user) => { this.props.navigator.immediatelyResetRouteStack([{name: 'home'}]); console.log(user); }, error: (data, error) => { var errorText; switch(error.code){ case 101: errorText="Invalid username or password." break; case 100: errorText="Unable to connect to the internet." break; default : errorText="Something went wrong." break; } this.setState({ success: false, error: errorText, loader: HiddenLoader }); console.log(data, error); } }); },
success: async function(user){ _this.clearInterval(interval); if(_this.isMounted()){ _this.setState({ currentPhrase: '', interactionDisabled: false }); } Parse.Cloud.run('fetchBookingListForUserCloudFunction', { user_id: Parse.User.current().getUsername() }).then( async function(result){ var keys = ['IS_LOGGED_IN', 'FORCE_UPDATE', 'MEETING_LIST']; var cleanData = []; for(var i=0;i<result.length;i++){ cleanData.push(result[i].toJSON()); } try{ await AsyncStorage.multiRemove(keys, (error)=>{ console.log(error); }); await AsyncStorage.setItem('MEETING_LIST', JSON.stringify(cleanData)); await AsyncStorage.setItem('IS_LOGGED_IN', 'SECRET_KEY'); _this.props.navigator.immediatelyResetRouteStack([{name: 'home'}]); } catch(e){ console.warn(e); } }, function(error){ console.warn(error); console.log("[MEETING API] Error: "+ JSON.stringify(error, null, 2)); } ); },
onLoginPress: function(){ var _this = this; dismissKeyboard(); if(this.state.interactionDisabled){ return; } if(this.state.username.trim()===""){ return this.setState({ currentPhrase: 'Username is missing.', phraseColor: '#1A237E' }); } if(this.state.password.trim()===""){ return this.setState({ currentPhrase: 'Password is missing.', phraseColor: '#1A237E' }); } if(this.isMounted()){ this.setState({ currentPhrase: 'Signing in...', phraseColor: '#ffffff', interactionDisabled: true }); } this.setRandomMessage(); Parse.User.logIn(this.state.username, this.state.password, { success: async function(user){ _this.clearInterval(interval); if(_this.isMounted()){ _this.setState({ currentPhrase: '', interactionDisabled: false }); } Parse.Cloud.run('fetchBookingListForUserCloudFunction', { user_id: Parse.User.current().getUsername() }).then( async function(result){ var keys = ['IS_LOGGED_IN', 'FORCE_UPDATE', 'MEETING_LIST']; var cleanData = []; for(var i=0;i<result.length;i++){ cleanData.push(result[i].toJSON()); } try{ await AsyncStorage.multiRemove(keys, (error)=>{ console.log(error); }); await AsyncStorage.setItem('MEETING_LIST', JSON.stringify(cleanData)); await AsyncStorage.setItem('IS_LOGGED_IN', 'SECRET_KEY'); _this.props.navigator.immediatelyResetRouteStack([{name: 'home'}]); } catch(e){ console.warn(e); } }, function(error){ console.warn(error); console.log("[MEETING API] Error: "+ JSON.stringify(error, null, 2)); } ); }, error: (data, error) => { _this.clearInterval(interval); console.log(data, error); var errorText; switch(error.code){ case 101: errorText="Invalid username or password." break; case 100: errorText="Unable to connect to the internet." break; default : errorText="Something went wrong." break; } if(_this.isMounted()){ _this.setState({ currentPhrase: errorText, phraseColor: '#1A237E', interactionDisabled: false }); } } }); this.setTimeout(function(){ if(_this.state.interactionDisabled === true){ if(_this.isMounted()){ _this.setState({ currentPhrase: 'Something went wrong. Please try again.', phraseColor: '#1A237E', interactionDisabled: false }) } } }, 10000); },