Commit 202280be authored by Lihinikaduwa D.N.R.  's avatar Lihinikaduwa D.N.R.

Merge branch 'it18257632' into 'master'

Login changed

See merge request !145
parents 55d2d8ec 5b8b75b4
import {useNavigation} from '@react-navigation/native';
import Orientation from 'react-native-orientation-locker';
import React, { useState } from "react";
import { SafeAreaView, ScrollView, View, StyleSheet, ImageBackground, Text, TextInput, TouchableOpacity, Image } from "react-native";
import {useSelector, useDispatch} from 'react-redux';
import {userLogin} from '../../store/actions/login';
import client from "../client/Client";
const isValidObjectField = (obj) => {
return Object.values(obj).every(value => value.trim());
}
import React, {useState} from 'react';
import {
SafeAreaView,
ScrollView,
View,
StyleSheet,
ImageBackground,
Text,
TextInput,
TouchableOpacity,
Image,
} from 'react-native';
import client from '../client/Client';
const isValidObjectField = obj => {
return Object.values(obj).every(value => value.trim());
};
const updateError = (error, stateUpdater) => {
stateUpdater(error);
......@@ -26,23 +34,10 @@ const isValidEmail = value => {
const Login = () => {
const navigation = useNavigation();
const dispatch = useDispatch();
const users = useSelector(state=>state.users.users);
const navigation = useNavigation();
React.useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => {
Orientation.unlockAllOrientations();
Orientation.lockToPortrait();
});
return unsubscribe;
}, [navigation]);
const [userInfo, setUserInfo] = useState({
email: '',
password: '',
React.useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
Orientation.unlockAllOrientations();
Orientation.lockToPortrait();
});
return unsubscribe;
}, [navigation]);
......@@ -67,75 +62,115 @@ const Login = () => {
return updateError('Required all fields!', setError);
}
const loginHandler = () => {
const loggedInUser = users.find(user=>user.email === email && user.password === password)
if(loggedInUser){
console.log(loggedInUser);
dispatch(userLogin({
name: loggedInUser.name,
id: loggedInUser.uid
}));
navigation.navigate('Home');
}else{
console.log('Invalid user credentials...');
}
}
const submitForm = () => {
if (isValidForm()){
loginFun(userInfo)
}
if (!isValidEmail(email)) {
return updateError('Invalid email !', setError);
}
if (!password.trim() || password.length < 8) {
return updateError('Password is less than 8 characters !', setError);
}
return(
<SafeAreaView>
<ScrollView>
<View style={styles.continer}>
<ImageBackground style={styles.backgroundImage} source={require('../../assets/login/login_background.png')} resizeMode="cover">
<View>
<Image style={styles.logo} source={require('../../assets/login/logo1.png')} resizeMode="contain"></Image>
</View>
<View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text>
{error ? (<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>{error}</Text>) : null}
<View style={styles.form_input}>
<TextInput id="email" value={email} autoCapitalize="none" onChangeText={value => handleOnChangeText(value, 'email')} style={styles.text_input} placeholder="Enter Email"></TextInput>
</View>
<View style={styles.form_input}>
<TextInput id="password" value={password} autoCapitalize="none" secureTextEntry onChangeText={value => handleOnChangeText(value, 'password')} style={styles.text_input} placeholder="Enter Password"></TextInput>
</View>
<View style={styles.form_input}>
<TouchableOpacity onPress={ ()=>{navigation.navigate("Start");}} style={styles.btn}>
<Text style={styles.btn_text}>
Sign In
</Text>
</TouchableOpacity>
</View>
{/* onPress={submitForm} */}
<View style={styles.text_if}>
<TouchableOpacity onPress={()=> { navigation.navigate("Register")}}>
<Text style={styles.btn_text2}>
If you don't have account? Sign Up
</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
</View>
</ScrollView>
</SafeAreaView>
)
}
return true;
};
const submitForm = () => {
if (isValidForm()) {
loginFun(userInfo);
}
};
const loginFun = () => {
var formData = JSON.stringify(userInfo);
client
.post('login', formData, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then(response => {
console.log(response.data);
if (response.data.status == 404) {
return updateError('Please register our system', setError);
}
if (response.data.status == 200) {
return navigation.navigate('Start');
}
})
.catch(err => {
console.log(err);
});
};
return (
<SafeAreaView>
<ScrollView>
<View style={styles.continer}>
<ImageBackground
style={styles.backgroundImage}
source={require('../../assets/login/login_background.png')}
resizeMode="cover">
<View>
<Image
style={styles.logo}
source={require('../../assets/login/logo1.png')}
resizeMode="contain"></Image>
</View>
<View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text>
{error ? (
<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>
{error}
</Text>
) : null}
<View style={styles.form_input}>
<TextInput
id="email"
value={email}
autoCapitalize="none"
onChangeText={value => handleOnChangeText(value, 'email')}
style={styles.text_input}
placeholder="Enter Email"></TextInput>
</View>
<View style={styles.form_input}>
<TextInput
id="password"
value={password}
autoCapitalize="none"
secureTextEntry
onChangeText={value => handleOnChangeText(value, 'password')}
style={styles.text_input}
placeholder="Enter Password"></TextInput>
</View>
<View style={styles.form_input}>
<TouchableOpacity onPress={submitForm} style={styles.btn}>
<Text style={styles.btn_text}>Sign In</Text>
</TouchableOpacity>
</View>
{/* onPress={submitForm} */}
<View style={styles.text_if}>
<TouchableOpacity
onPress={() => {
navigation.navigate('Register');
}}>
<Text style={styles.btn_text2}>
If you don't have account? Sign Up
</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
continer: {
......
......@@ -2,6 +2,6 @@ import axios from 'axios';
// export default axios.create({ baseURL: 'http://192.168.8.102:5000/', timeout: 15000, });
export default axios.create({
baseURL: 'http://192.168.1.101:5000/',
baseURL: 'http://192.168.1.100:5000/',
timeout: 15000,
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment