Advertisement
Badal_hs_shah

Untitled

Jan 13th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useEffect, useState} from 'react';
  2. import {StyleSheet, Text, View, Button, Alert, Platform} from 'react-native';
  3. import {useNavigation} from '@react-navigation/native';
  4. import {TextInput} from 'react-native-gesture-handler';
  5. import {useTheme} from '../themes';
  6. import {useTranslation} from 'react-i18next';
  7. import DeviceInfo from 'react-native-device-info';
  8. import {Service} from '@src/services';
  9. import {API_URL_LOGIN} from '@src/services/url-constant';
  10. import {
  11.   getItemFromStorage,
  12.   setItemInStorage,
  13.   removeStoreItem,
  14. } from '@src/utils/Storage';
  15. import {request, check, PERMISSIONS, RESULTS} from 'react-native-permissions';
  16. import NetInfo from '@react-native-community/netinfo';
  17. import checkResponse from '@src/services/CheckResponse';
  18. import InvalidVersionAlert from '@src/components/atoms/InvalidVersionAlert';
  19.  
  20. const Login = (props: any) => {
  21.   const {theme, dark, toggle} = useTheme();
  22.   const navigation = useNavigation();
  23.   const {t} = useTranslation();
  24.   const [emailText, setEmailText] = React.useState('');
  25.   const [passwordText, setPasswordText] = React.useState('');
  26.   const [showInvalidVersionAlert, setShowInvalidVersionAlert] = useState(false);
  27.  
  28.   if (Platform.OS === 'ios') {
  29.     request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE).then(result => {
  30.       // …
  31.       console.log('Result', result);
  32.     });
  33.   } else {
  34.     request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION).then(result => {
  35.       // …
  36.       console.log('Result', result);
  37.     });
  38.   }
  39.  
  40.   const toggleSwitch = () => {
  41.     toggle();
  42.   };
  43.  
  44.   const validateEmail = () => {
  45.     let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;
  46.     if (reg.test(emailText) === false) {
  47.       return false;
  48.     } else {
  49.       return true;
  50.     }
  51.   };
  52.  
  53.   const checkConnection = () => {
  54.     NetInfo.fetch().then(state => {
  55.       console.log('Connection type', state.type);
  56.       console.log('Is connected?', state.isConnected);
  57.  
  58.       if (state.isConnected) {
  59.         //Alert.alert('You are online!');
  60.         onLogin();
  61.       } else {
  62.         Alert.alert('You are offline!');
  63.       }
  64.     });
  65.   };
  66.  
  67.   const onLogin = () => {
  68.     if (emailText === '' || passwordText === '') {
  69.       //Alert.alert('Invalid email or password.');
  70.     } else {
  71.       callLoginAPI();
  72.     }
  73.   };
  74.  
  75.   let DeviceType = '';
  76.   if (DeviceInfo.getSystemName() === 'iOS') {
  77.     DeviceType = 'iPhone';
  78.   } else {
  79.     DeviceType = 'Android';
  80.   }
  81.   const sendData = {
  82.     ApplicationVersion: '12.1.3',
  83.     BatteryLevelPercent: '-100',
  84.     BatteryState: 'Unknown',
  85.     CarrierName: 'Unknown',
  86.     ConnectionType: 'WiFi',
  87.     DeviceGuid: '1A5FAF66-EAA7-488A-A518-99321BB2FB59',
  88.     DeviceModel: 'x86_64',
  89.     DeviceType: 'iPhone',
  90.     DiskSpaceAvailableInMB: '85185.89',
  91.     DiskSpaceSizeInMB: '233752.4',
  92.     //Email: 'kpatel3@wiser.com',
  93.     Email: emailText,
  94.     IsUploadNeeded: 0,
  95.     MachineName: 'iPhone 14',
  96.     OsVersion: '16.0',
  97.     Password: passwordText,
  98.     //Password: 'rw3kpatel',
  99.  
  100.     // ApplicationVersion: '12.1.5',
  101.     // BatteryLevelPercent: DeviceInfo.getBatteryLevel(),
  102.     // BatteryState: DeviceInfo.isBatteryCharging(),
  103.     // CarrierName: DeviceInfo.getCarrier(),
  104.     // ConnectionType: 'WiFi',
  105.     // DeviceGuid: DeviceInfo.getUniqueId(),
  106.     // DeviceModel: DeviceInfo.supportedAbis(),
  107.     // DeviceType: DeviceType,
  108.     // DiskSpaceAvailableInMB: DeviceInfo.getFreeDiskStorage(),
  109.     // DiskSpaceSizeInMB: DeviceInfo.getTotalDiskCapacity(),
  110.     // Email: emailText,
  111.     // IsUploadNeeded: 0,
  112.     // MachineName: DeviceInfo.getModel(),
  113.     // OsVersion: DeviceInfo.getSystemVersion(),
  114.     // Password: passwordText,
  115.   };
  116.  
  117.   const callLoginAPI = async () => {
  118.     try {
  119.       const res = await Service.send({
  120.         method: 'POST',
  121.         url: API_URL_LOGIN,
  122.         obj: JSON.stringify(sendData),
  123.         isRawData: true,
  124.         contentType: 'application/json',
  125.       });
  126.       checkResponse({
  127.         res: res,
  128.         delegate: navigation,
  129.       });
  130.  
  131.       if (res.data.LoginResultCode === 'Success') {
  132.         deleteData();
  133.         saveData(res);
  134.         navigation.navigate('appscreens' as never);
  135.       }
  136.       // else {
  137.       //   Alert.alert(res.data.LoginErrorMessage);
  138.       // }
  139.       getSaveddata();
  140.     } catch (err) {
  141.       console.log('catch', err);
  142.     }
  143.   };
  144.  
  145.   const deleteData = async () => {
  146.     await removeStoreItem('RW3Email');
  147.     await removeStoreItem('RW3Password');
  148.     await removeStoreItem('RW3PeopleID');
  149.     await removeStoreItem('RW3SyncGUID');
  150.   };
  151.  
  152.   const saveData = async (res: any) => {
  153.     let peopleId = res.data.PeopleId;
  154.     let syncGuid = res.data.SyncGuid;
  155.     await setItemInStorage('RW3Email', emailText);
  156.     await setItemInStorage('RW3Password', passwordText);
  157.     await setItemInStorage('RW3PeopleID', peopleId.toString());
  158.     await setItemInStorage('RW3SyncGUID', syncGuid.toString());
  159.   };
  160.  
  161.   const getSaveddata = async () => {
  162.     const emailFromStorage = await getItemFromStorage('RW3Email');
  163.     console.log('Email val ----->>', emailFromStorage);
  164.   };
  165.  
  166.   return (
  167.     <View
  168.       style={{
  169.         flex: 1,
  170.         backgroundColor: theme.palette.backgroundcolor,
  171.         padding: 15,
  172.       }}>
  173.       {showInvalidVersionAlert && <InvalidVersionAlert />}
  174.       <Text style={[styles.title, {color: theme.palette.textcolor}]}>
  175.         {`${t('PRICECHECK')}`}
  176.       </Text>
  177.       <View style={[styles.textfieldView, {marginTop: 30}]}>
  178.         <TextInput
  179.           style={styles.commontextField}
  180.           autoCapitalize={'none'}
  181.           placeholder={`${t('EMAIL')}`}
  182.           placeholderTextColor={theme.palette.textplaceholdercolor}
  183.           keyboardType={'email-address'}
  184.           returnKeyType={'next'}
  185.           onChangeText={newText => setEmailText(newText)}
  186.         />
  187.       </View>
  188.       <View style={[styles.textfieldView, , {marginTop: 15, marginBottom: 15}]}>
  189.         <TextInput
  190.           style={styles.commontextField}
  191.           autoCapitalize={'none'}
  192.           placeholder={`${t('PASSWORD')}`}
  193.           placeholderTextColor={theme.palette.textplaceholdercolor}
  194.           keyboardType={'email-address'}
  195.           secureTextEntry={true}
  196.           returnKeyType={'next'}
  197.           onChangeText={newText => setPasswordText(newText)}
  198.         />
  199.       </View>
  200.       <Button
  201.         onPress={checkConnection}
  202.         title={`${t('LOGIN')}`}
  203.         color={theme.palette.appthemecolor}
  204.       />
  205.       {/* <Switch onChange={toggleSwitch} value={dark} /> */}
  206.       <Text style={styles.bottomTitle}>{`${t(
  207.         'SPLASHSCREEN_BOTTOM_TITLE',
  208.       )}`}</Text>
  209.     </View>
  210.   );
  211. };
  212.  
  213. const styles = StyleSheet.create({
  214.   title: {
  215.     alignSelf: 'center',
  216.     marginTop: 150,
  217.     fontWeight: 'bold',
  218.     fontSize: 40,
  219.   },
  220.   textfieldView: {
  221.     borderColor: 'lightgray',
  222.     borderWidth: 0.5,
  223.     borderRadius: 5.0,
  224.     padding: 5,
  225.   },
  226.   commontextField: {
  227.     fontSize: 17,
  228.     padding: 5,
  229.   },
  230.   loginButton: {
  231.     padding: 10,
  232.     borderColor: '#000000',
  233.     borderRadius: 10,
  234.     borderWidth: 1,
  235.     overflow: 'hidden',
  236.     color: 'black',
  237.   },
  238.   bottomTitle: {
  239.     alignSelf: 'center',
  240.     position: 'absolute',
  241.     bottom: 0,
  242.     marginBottom: 20,
  243.   },
  244. });
  245. export default Login;
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement