Browse Source

feat(login): add login function

master
farhantock 5 months ago
parent
commit
a63448b44a
  1. 74
      src/screens/Login.js

74
src/screens/Login.js

@ -1,19 +1,24 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { useForm, Controller } from 'react-hook-form'; import { useForm, Controller } from 'react-hook-form';
import { KeyboardAvoidingView, ScrollView, StatusBar, View, StyleSheet, Image, Platform, Dimensions } from 'react-native'; import { KeyboardAvoidingView, ScrollView, StatusBar, View, StyleSheet, Image, Platform, Dimensions, Alert } from 'react-native';
import { Button, TextInput, HelperText, Text } from 'react-native-paper'; import { Button, TextInput, HelperText, Text, Modal } from 'react-native-paper';
import { useDispatch, useSelector } from 'react-redux'; import { setIsLogin, setUser, setRegister } from '../appredux/actions';
import { setIsLogin, setUser } from '../appredux/actions';
import { store } from '../appredux/store'; import { store } from '../appredux/store';
import { colors } from '../utils/color'; import { colors } from '../utils/color';
import LOGIN_BANNER from '../assets/images/logo_nawakara.png'; import LOGIN_BANNER from '../assets/images/logo_nawakara.png';
import { strings } from '../utils/i18n'; import { strings } from '../utils/i18n';
import { initFirebase } from '../utils/Auth'; import { initFirebase } from '../utils/Auth';
import RequestModule from '../services/api/request';
import Toast from 'react-native-toast-message';
const LoginScreen = ({ route, navigation }) => { const LoginScreen = ({ route, navigation }) => {
const { scannedCode } = useSelector(state => state.userReducer) const request = new RequestModule('');
const [visible, setVisible] = useState(false);
const showModal = () => setVisible(true);
const hideModal = () => setVisible(false);
const [loginProcess, setLoginProcess] = useState(false); const [loginProcess, setLoginProcess] = useState(false);
const [showPassword, setShowPassword] = useState(false); const [showPassword, setShowPassword] = useState(false);
const { control, handleSubmit, formState: { errors, isDirty, isValid, isSubmitting, isLoading } } = useForm({ const { control, handleSubmit, formState: { errors, isDirty, isValid, isSubmitting } } = useForm({
defaultValues: { defaultValues: {
username: '', username: '',
password: '' password: ''
@ -23,16 +28,45 @@ const LoginScreen = ({ route, navigation }) => {
useEffect(() => { useEffect(() => {
initFirebase(); initFirebase();
}, []) }, []);
const onSubmitLogin = async (payload) => { const onSubmitLogin = async (payload) => {
setLoginProcess(true);
try {
const result = await request.login(payload);
if (result && result.status === 200 && result.data.data) {
store.dispatch(setIsLogin(true));
store.dispatch(setUser(result.data.data));
if (result.data.data.assigment_hr) {
store.dispatch(setRegister(true));
navigation.navigate('App'); navigation.navigate('App');
} else {
store.dispatch(setRegister(false));
navigation.navigate('RegisterScreen');
}
} else {
Toast.show({
type: 'error',
text1: 'Please check username or password',
position: 'bottom',
});
}
} catch (error) {
Toast.show({
type: 'error',
text1: 'An error occurred during login',
position: 'bottom',
});
} finally {
setLoginProcess(false);
}
} }
return ( return (
<> <>
<StatusBar backgroundColor={colors.white} barStyle='dark-content' translucent={true} /> <StatusBar backgroundColor={colors.white} barStyle='dark-content' translucent={true} />
<KeyboardAvoidingView ehavior={Platform.OS === "ios" ? "padding" : null} style={{ flex: 1 }}> <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : null} style={{ flex: 1 }}>
<ScrollView contentContainerStyle={{ flexGrow: 1 }}> <ScrollView contentContainerStyle={{ flexGrow: 1 }}>
<View style={styles.container}> <View style={styles.container}>
<Image <Image
@ -48,7 +82,7 @@ const LoginScreen = ({ route, navigation }) => {
control={control} control={control}
rules={{ rules={{
required: true, required: true,
minLength: 3 minLength: 0
}} }}
render={({ field: { onChange, onBlur, value } }) => ( render={({ field: { onChange, onBlur, value } }) => (
<TextInput <TextInput
@ -60,6 +94,8 @@ const LoginScreen = ({ route, navigation }) => {
onChangeText={onChange} onChangeText={onChange}
style={{ marginTop: 10 }} style={{ marginTop: 10 }}
value={value} value={value}
accessible={true}
accessibilityLabel="Username"
/> />
)} )}
name="username" name="username"
@ -72,6 +108,7 @@ const LoginScreen = ({ route, navigation }) => {
control={control} control={control}
rules={{ rules={{
required: true, required: true,
minLength: 3
}} }}
render={({ field: { onChange, onBlur, value } }) => ( render={({ field: { onChange, onBlur, value } }) => (
<TextInput <TextInput
@ -89,8 +126,9 @@ const LoginScreen = ({ route, navigation }) => {
onPress={() => setShowPassword(!showPassword)} onPress={() => setShowPassword(!showPassword)}
style={styles.eyeIcon} style={styles.eyeIcon}
/>} />}
accessible={true}
accessibilityLabel="Password"
/> />
)} )}
name="password" name="password"
/> />
@ -103,15 +141,18 @@ const LoginScreen = ({ route, navigation }) => {
mode="contained" mode="contained"
onPress={handleSubmit(onSubmitLogin)} onPress={handleSubmit(onSubmitLogin)}
style={{ marginTop: 15, backgroundColor: colors.blue, borderRadius: 10 }} style={{ marginTop: 15, backgroundColor: colors.blue, borderRadius: 10 }}
disabled={!isDirty || !isValid || isSubmitting} disabled={!isDirty || !isValid || isSubmitting || loginProcess}
labelStyle={{ color: colors.white }} labelStyle={{ color: colors.white }}
loading={isSubmitting || loginProcess}
> >
{isSubmitting ? "Loading..." : strings('loginPage.signInBtn')} {strings('loginPage.signInBtn')}
</Button> </Button>
</View> </View>
</ScrollView > </ScrollView >
</KeyboardAvoidingView> </KeyboardAvoidingView>
</> </>
) )
} }
@ -119,8 +160,13 @@ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
padding: 20, padding: 20,
backgroundColor: colors.pureWhite justifyContent: 'center',
backgroundColor: colors.white,
paddingBottom: 100
},
eyeIcon: {
marginTop: 12
} }
}) })
export default LoginScreen export default LoginScreen;

Loading…
Cancel
Save