diff options
Diffstat (limited to 'App.js')
-rw-r--r-- | App.js | 75 |
1 files changed, 19 insertions, 56 deletions
@@ -1,31 +1,13 @@ -import { StatusBar } from 'expo-status-bar'; -import { StyleSheet, SafeAreaView, Text } from 'react-native'; -import { useState, useEffect } from 'react'; -import AsyncStorage from '@react-native-async-storage/async-storage'; -import { Login, List, Edit } from './components' +import { StatusBar } from 'expo-status-bar' +import { StyleSheet, SafeAreaView, Text } from 'react-native' +import { useState, useEffect } from 'react' +import AsyncStorage from '@react-native-async-storage/async-storage' +import Main from './components/Main' +import Login from './components/Login' export default function App() { - const [session, setSession] = useState(); - const [error, setError] = useState(null); - const [edit, setEdit] = useState(null) - - const login = async (e, p) => { - try { - const response = await fetch('https://apps.pruss.it/api/login', { - method: 'POST', - headers: { 'Content-Type': 'plain/text; charset=utf-8' }, - body: JSON.stringify({ email: e, password: p }) - }) - const cookies = response.headers?.map?.['set-cookie'] - const data = await response.json() - if (data?.isLoggedIn) { - await AsyncStorage.setItem('session', JSON.stringify({...data, cookies})) - setSession({ ...data, cookies }) - } - } catch(e) { - showError('Error while logging in') - } - } + const [session, setSession] = useState() + const [error, setError] = useState(null) const showError = (e) => { setError(e) @@ -34,48 +16,29 @@ export default function App() { useEffect(() => { AsyncStorage.getItem('session') - .then(s => setSession(JSON.parse(s))) + .then(session => setSession(JSON.parse(session))) .catch(() => setSession(null)) - }, []); + }, []) - if (error) return ( + if (error || session === undefined) return ( <SafeAreaView style={styles.container}> - <Text style={styles.error}>{error}</Text> + {error + ? <Text style={styles.error}>{error}</Text> + : <Text>Loading...</Text>} </SafeAreaView> ) - if (session === undefined) return ( - <SafeAreaView style={styles.container}> - <Text>Loading...</Text> - </SafeAreaView> - ); - return ( <SafeAreaView style={styles.container}> { session === null - ? <Login login={login} /> - : edit - ? ( - <Edit - edit={edit} - setEdit={setEdit} - session={session} - setSession={setSession} - showError={showError} - /> - ) : ( - <List - session={session} - showError={showError} - setEdit={setEdit} - /> - ) + ? <Login setSession={setSession} showError={showError} /> + : <Main session={session} setSession={setSession} showError={showError} /> } <StatusBar style="auto" /> </SafeAreaView> - ); -}; + ) +} const styles = StyleSheet.create({ container: { @@ -85,4 +48,4 @@ const styles = StyleSheet.create({ error: { color: 'red', } -}); +}) |