diff --git a/app/_layout.tsx b/app/_layout.tsx index 773594f..28892cd 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -19,13 +19,14 @@ import { NavigationContainer } from '@react-navigation/native'; import ViewHealthEvents from './viewHealthEvents'; import ViewCalendarEvents from './viewCalendarEvents'; import ViewMealEvents from './viewMealEvents'; +import TaskScreen from './home'; const Stack = createStackNavigator(); const CustomTopBar = ({ navigation }: StackHeaderProps) => { return ( - navigation.navigate('index')}> + navigation.navigate('home')}> navigation.navigate('healthTracker')}> @@ -61,6 +62,7 @@ export default function Layout() { }} > + diff --git a/app/accountSetting.tsx b/app/accountSetting.tsx index 7af8ab5..c6d2e47 100644 --- a/app/accountSetting.tsx +++ b/app/accountSetting.tsx @@ -1,10 +1,10 @@ -import React, { useCallback, useState } from 'react'; +import React, { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, StyleSheet } from 'react-native'; -import { cLog } from './log'; +// import { cLog } from './log'; import { IPAddr } from './constants'; import axios from 'axios'; -import { useFocusEffect } from '@react-navigation/native'; -import GenericViewPageForm from './viewEventPage'; +// import { useFocusEffect } from '@react-navigation/native'; +// import GenericViewPageForm from './viewEventPage'; export default function AccountSetting () { const initialData = { userId: 1, name: "", username: "", email: "" }; diff --git a/app/constants.js b/app/constants.js index 7cf720d..f0191e4 100644 --- a/app/constants.js +++ b/app/constants.js @@ -1,5 +1,5 @@ // export const IPAddr = "http://34.204.83.156:8080" // For AWS -export const IPAddr = "http://137.112.197.27:8080" // For local testing on laptop +export const IPAddr = "http://137.112.196.132:8080" // For local testing on laptop export const logging = true; export const repeatingData = [ diff --git a/app/home.tsx b/app/home.tsx new file mode 100644 index 0000000..bca614c --- /dev/null +++ b/app/home.tsx @@ -0,0 +1,60 @@ +// import { IPAddr } from './constants'; +// import axios from 'axios'; +import GenericMainPageForm from './mainPageTemplate'; +import React, { useState, useCallback } from 'react'; +import { formatTime, IPAddr } from './constants'; +import { Task } from '../components/Types'; +import axios from 'axios'; +import { useFocusEffect } from '@react-navigation/native'; +import { cLog } from './log' + +export default function TaskScreen() { + const [tasks, setTasks] = useState([]); + const fetchAllEvents = async () => { + const hit = IPAddr + '/get_all_events/1'; + cLog('Fetching all events from:' + hit); + axios.get(hit) + .then(response => { + const events = response.data.map((event: any) => ({ + id: event.id.toString(), + title: `${event.title} at ${event.eventDate}, ${formatTime(event.eventTime)}`, + done: false, + icon: getEventIcon(event.event_type), + event: event + })).slice(0,10);; + setTasks(events); + }) + .catch(error => console.error('Error fetching events:', error)); + } + + const getEventIcon = (eventType: string) => { + switch (eventType.toLowerCase()) { + case 'finance': + return 'wallet-outline'; + case 'calendar': + return 'calendar-outline'; + case 'health': + return 'fitness-outline'; + case 'meal': + return 'fast-food-outline'; + default: + return 'help-outline'; + } + }; + + useFocusEffect( + useCallback(() => { + fetchAllEvents(); + }, []) + ); + + return ( + + ); +} diff --git a/app/index.tsx b/app/index.tsx index 844c1f9..fc42a3d 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -1,60 +1,72 @@ // import { IPAddr } from './constants'; // import axios from 'axios'; -import GenericMainPageForm from './mainPageTemplate'; import React, { useState, useCallback } from 'react'; -import { formatTime, IPAddr } from './constants'; -import { Task } from '../components/Types'; +import { IPAddr } from './constants'; +import { RootStackParamList } from '../components/Types'; import axios from 'axios'; -import { useFocusEffect } from '@react-navigation/native'; +import { styles } from './styles'; +import { useFocusEffect, useNavigation } from '@react-navigation/native'; import { cLog } from './log' +import AsyncStorage from "@react-native-async-storage/async-storage"; +import { StackNavigationProp } from '@react-navigation/stack'; +import { Button, TextInput, View, Text } from 'react-native'; export default function TaskScreen() { - const [tasks, setTasks] = useState([]); - const fetchAllEvents = async () => { - const hit = IPAddr + '/get_all_events/1'; - cLog('Fetching all events from:' + hit); - axios.get(hit) - .then(response => { - const events = response.data.map((event: any) => ({ - id: event.id.toString(), - title: `${event.title} at ${event.eventDate}, ${formatTime(event.eventTime)}`, - done: false, - icon: getEventIcon(event.event_type), - event: event - })).slice(0,10);; - setTasks(events); - }) - .catch(error => console.error('Error fetching events:', error)); - } + type Prop = StackNavigationProp; + const navigation = useNavigation(); - const getEventIcon = (eventType: string) => { - switch (eventType.toLowerCase()) { - case 'finance': - return 'wallet-outline'; - case 'calendar': - return 'calendar-outline'; - case 'health': - return 'fitness-outline'; - case 'meal': - return 'fast-food-outline'; - default: - return 'help-outline'; + const [username, setUsername] = useState(''); + const [password, setPassword] = useState(''); + + const checkLogin = async () => { + try { + const userId = await AsyncStorage.getItem('userId'); + if (userId !== null) { + cLog("Navigating Home"); + navigation.navigate('home'); + } else { + cLog('User not logged in'); + } + } catch (error) { + console.error('Error checking login:', error); + } + } + + const handleLogin = async (username: string, password: string) => { + try { + const hit = IPAddr + '/login'; + const response = await axios.post(hit, { username: username, password: password }); + cLog('Login response:', response.data); + await AsyncStorage.setItem('userId', response.data.userId.toString()); + cLog('User id saved:', response.data.userId); + navigation.navigate('home'); + } catch (error) { + console.error('Error logging in:', error); + } } - }; - useFocusEffect( - useCallback(() => { - fetchAllEvents(); - }, []) - ); + useFocusEffect( + useCallback(() => { + checkLogin(); + }, []) + ); - return ( - - ); + return ( + + Login + setUsername(text)} + /> + setPassword(text)} + /> +