From d77d7c440333ded46eeb8d28e22ec5517f3b15b8 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Wed, 18 May 2022 23:02:58 +0100 Subject: small fixes --- App.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 6 deletions(-) (limited to 'App.js') diff --git a/App.js b/App.js index 09f879b..1163cbe 100644 --- a/App.js +++ b/App.js @@ -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 ( + + {error} + + ) + + if (session === undefined) return ( + + Loading... + + ); + return ( - Open up App.js to start working on your app! + { + session === null + ? + : edit + ? ( + + ) : ( + + ) + } ); -} +}; const styles = StyleSheet.create({ container: { flex: 1, - backgroundColor: '#fff', - alignItems: 'center', - justifyContent: 'center', + backgroundColor: '#000', + height: '100%', }, + error: { + color: 'red', + } }); -- cgit v1.2.3