diff options
author | 2022-05-18 23:02:58 +0100 | |
---|---|---|
committer | 2022-05-18 23:02:58 +0100 | |
commit | d77d7c440333ded46eeb8d28e22ec5517f3b15b8 (patch) | |
tree | 34bc54ef125433b722184777f33da90e1badf745 /App.js | |
parent | 9173d98070a0463b352625c2e50d20543f7c12fb (diff) | |
download | notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.tar.gz notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.tar.bz2 notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.zip |
small fixes
Diffstat (limited to 'App.js')
-rw-r--r-- | App.js | 81 |
1 files changed, 75 insertions, 6 deletions
@@ -1,20 +1,89 @@ import { StatusBar } from 'expo-status-bar'; -import { StyleSheet, Text, View } from 'react-native'; +import { StyleSheet, View, Text } from 'react-native'; +import { useState, useEffect } from 'react'; +import AsyncStorage from '@react-native-async-storage/async-storage'; +import { Login, List, Edit } from './components' 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 showError = (e) => { + setError(e) + setTimeout(() => { setError(null) }, 2000) + } + + useEffect(() => { + AsyncStorage.getItem('session') + .then(s => setSession(JSON.parse(s))) + .catch(() => setSession(null)) + }, []); + + if (error) return ( + <View style={styles.container}> + <Text style={styles.error}>{error}</Text> + </View> + ) + + if (session === undefined) return ( + <View style={styles.container}> + <Text>Loading...</Text> + </View> + ); + return ( <View style={styles.container}> - <Text>Open up App.js to start working on your app!</Text> + { + session === null + ? <Login login={login} /> + : edit + ? ( + <Edit + edit={edit} + setEdit={setEdit} + session={session} + setSession={setSession} + showError={showError} + /> + ) : ( + <List + session={session} + showError={showError} + setEdit={setEdit} + /> + ) + } <StatusBar style="auto" /> </View> ); -} +}; const styles = StyleSheet.create({ container: { flex: 1, - backgroundColor: '#fff', - alignItems: 'center', - justifyContent: 'center', + backgroundColor: '#000', + height: '100%', }, + error: { + color: 'red', + } }); |