diff --git a/apps/mobile/src/views/Login/index.js b/apps/mobile/src/views/Login/index.js deleted file mode 100644 index d0fc7d60a..000000000 --- a/apps/mobile/src/views/Login/index.js +++ /dev/null @@ -1,474 +0,0 @@ -/* import {useIsFocused} from '@react-navigation/native'; -import React, {createRef, useEffect, useState} from 'react'; -import { - ActivityIndicator, - BackHandler, - Text, - TouchableOpacity, - View, -} from 'react-native'; -import * as Animatable from 'react-native-animatable'; -import {TextInput} from 'react-native-gesture-handler'; -import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; -import {opacity, pv, SIZE, WEIGHT} from '../../common/common'; -import {Button} from '../../components/Button'; -import {useTracked} from '../../provider'; -import {ACTIONS} from '../../provider/actions'; -import {eSendEvent} from '../../services/eventManager'; -import { - eCloseSideMenu, - eOpenSideMenu, - eSetModalNavigator, - eStartSyncer, - refreshNotesPage, -} from '../../services/events'; -import {validatePass, validateUsername} from '../../services/validation'; -import {db, DDS, ToastEvent} from '../../utils/utils'; -import Seperator from '../../components/Seperator'; - -export const Login = ({route, navigation}) => { - const [state, dispatch] = useTracked(); - const {colors, isLoginNavigator} = state; - const [status, setStatus] = useState('Logging in...'); - const _username = createRef(); - const [loggingIn, setLoggingIn] = useState(false); - const [email, setEmail] = useState(null); - const [password, setPassword] = useState(null); - const [invalidEmail, setInvalidEmail] = useState(false); - const [invalidPassword, setInvalidPassword] = useState(false); - const [username, setUsername] = useState(null); - const [invalidUsername, setInvalidUsername] = useState(false); - const [secureEntry, setSecureEntry] = useState(true); - - const _email = createRef(); - const _pass = createRef(); - const _passContainer = createRef(); - - const isFocused = useIsFocused(); - - const handleBackPress = () => { - return true; - }; - - useEffect(() => { - eSendEvent(eCloseSideMenu); - let backhandler; - if (isFocused) { - backhandler = BackHandler.addEventListener( - 'hardwareBackPress', - handleBackPress, - ); - } else { - if (backhandler) { - backhandler.remove(); - backhandler = null; - } - } - - return () => { - eSendEvent(eOpenSideMenu); - if (!backhandler) return; - backhandler.remove(); - backhandler = null; - }; - }, [isFocused]); - - const login = async () => { - if ( - !password || - password.length < 8 || - !username || - invalidPassword || - invalidUsername - ) { - ToastEvent.show('username or password is invalid', 'error'); - return; - } - - setLoggingIn(true); - _username.current.blur(); - _pass.current.blur(); - setStatus('Logging in...'); - - try { - let res = await db.user.login(username.toLowerCase(), password); - - if (res) { - setStatus('Fetching data...'); - } - } catch (e) { - setTimeout(() => { - ToastEvent.show(e.message, 'error'); - setLoggingIn(false); - }, 500); - - return; - } - - let user; - - try { - user = await db.user.get(); - if (!user) throw new Error('Username or password incorrect'); - dispatch({type: ACTIONS.USER, user: user}); - dispatch({type: ACTIONS.SYNCING, syncing: true}); - setStatus('Syncing your notes...'); - await db.sync(); - eSendEvent(eStartSyncer); - navigation.goBack(); - dispatch({type: ACTIONS.ALL}); - eSendEvent(refreshNotesPage); - dispatch({type: ACTIONS.SYNCING, syncing: false}); - ToastEvent.show(`Logged in as ${username}`, 'success'); - } catch (e) { - dispatch({type: ACTIONS.SYNCING, syncing: false}); - setLoggingIn(false); - ToastEvent.show(e.message, 'error'); - } - }; - - useEffect(() => { - if (isFocused) { - dispatch({ - type: ACTIONS.HEADER_STATE, - state: { - type: null, - menu: false, - canGoBack: true, - route: route, - color: null, - navigation: navigation, - ind: !route.params.root, - }, - }); - dispatch({ - type: ACTIONS.CONTAINER_BOTTOM_BUTTON, - state: { - visible: false, - ind: !route.params.root, - }, - }); - dispatch({ - type: ACTIONS.CONTAINER_STATE, - state: { - noSelectionHeader: false, - }, - }); - dispatch({ - type: ACTIONS.HEADER_VERTICAL_MENU, - state: false, - }); - - dispatch({ - type: ACTIONS.HEADER_TEXT_STATE, - state: { - heading: 'Login', - ind: !route.params.root, - }, - }); - - dispatch({ - type: ACTIONS.CURRENT_SCREEN, - screen: 'login', - }); - - dispatch({ - type: ACTIONS.SEARCH_STATE, - state: { - noSearch: true, - ind: !route.params.root, - }, - }); - if (!route.params.root) { - eSendEvent(eSetModalNavigator, true); - } - } - }, [isFocused]); - - return ( - - {loggingIn ? ( - - - - - {status} - - - ) : null} - - {loggingIn ? null : ( - <> - - - - - - { - if (!invalidUsername) { - _username.current.setNativeProps({ - style: { - borderColor: colors.accent, - }, - }); - } - }} - defaultValue={username} - onBlur={() => { - if (!validateUsername(username) && username?.length > 0) { - setInvalidUsername(true); - _username.current.setNativeProps({ - style: { - borderColor: colors.errorText, - }, - }); - } else { - setInvalidUsername(false); - _username.current.setNativeProps({ - style: { - borderColor: colors.nav, - }, - }); - } - }} - textContentType="username" - onChangeText={(value) => { - setUsername(value); - - if (invalidUsername && validateUsername(username)) { - setInvalidUsername(false); - _username.current.setNativeProps({ - style: { - borderColor: colors.accent, - }, - }); - } - }} - onSubmitEditing={() => { - if (!validateUsername(username)) { - setInvalidUsername(true); - } - }} - style={{ - padding: pv, - borderWidth: 1.5, - borderColor: colors.nav, - borderRadius: 5, - fontSize: SIZE.sm, - fontFamily: WEIGHT.regular, - }} - placeholder="Username" - placeholderTextColor={colors.icon} - /> - - - {invalidUsername ? ( - - {' '} - Username is invalid - - ) : null} - - - - - { - if (!invalidPassword) { - _passContainer.current?.setNativeProps({ - style: { - borderColor: colors.accent, - }, - }); - } - }} - onBlur={() => { - if (!validatePass(password) && password?.length > 0) { - setInvalidPassword(true); - - _passContainer.current?.setNativeProps({ - style: { - borderColor: colors.errorText, - }, - }); - } else { - setInvalidPassword(false); - _passContainer.current?.setNativeProps({ - style: { - borderColor: colors.nav, - }, - }); - } - }} - onChangeText={(value) => { - setPassword(value); - if (invalidPassword && validatePass(password)) { - setInvalidPassword(false); - - _passContainer.current.setNativeProps({ - style: { - borderColor: colors.accent, - }, - }); - } - }} - onSubmitEditing={() => { - if (!validatePass(password)) { - setInvalidPassword(true); - _pass.current.setNativeProps({ - style: { - color: colors.errorText, - }, - }); - } - }} - style={{ - paddingVertical: pv, - paddingHorizontal: 0, - fontSize: SIZE.sm, - fontFamily: WEIGHT.regular, - width: '85%', - maxWidth: '85%', - }} - secureTextEntry={secureEntry} - placeholder="Password" - placeholderTextColor={colors.icon} - /> - { - setSecureEntry(!secureEntry); - }} - style={{ - width: 25, - }} - color={secureEntry ? colors.icon : colors.accent} - /> - - - {invalidPassword ? ( - - {' '} - Password is invalid - - ) : null} - - - -