diff --git a/react-native/screens/HomeScreen.tsx b/react-native/screens/HomeScreen.tsx index 9b6f223..f53aff6 100644 --- a/react-native/screens/HomeScreen.tsx +++ b/react-native/screens/HomeScreen.tsx @@ -1,12 +1,15 @@ import React, { useEffect, useState } from 'react'; -import { StyleSheet, View, SafeAreaView, TouchableHighlight, Image, ImageBackground } from 'react-native'; -import { Text } from 'native-base' +import { StyleSheet, View, SafeAreaView, TouchableHighlight, Image, ImageBackground, Alert } from 'react-native'; +import { Text, Box } from 'native-base' +import { Ionicons } from '@expo/vector-icons'; import { theme } from '../core/theme'; import type { Navigation, UserData } from '../types'; import { useAuth } from '../contexts/Auth'; +import { StackActions } from '@react-navigation/native'; + export default function HomeScreen({ navigation }: Navigation) { - const [events, setEvents] = useState<{cid: number, cname: string, events: {time: string, content: string}[]}[]>(); + const [events, setEvents] = useState<{event_num: number, children: { cid: number, cname: string, event: string[] }[]}>(); const [totalEventsCount, setTotalEventsCount] = useState(4); const [nowSelectedChildId, setNowSelectedChildId] = useState(1); const [user, setUser] = useState(); @@ -16,27 +19,48 @@ export default function HomeScreen({ navigation }: Navigation) { setUser(auth?.userData); // TODO: get events by send header(`auth.AuthData`) to server - setEvents([{ - cid: 1, - cname: "Soo", - events: [{ - time: "10:00", - content: "the 17th Graduate Seremony" - }, { - time: "13:00", - content: "Do-Dream Festival" - }] - }, { - cid: 2, - cname: "Hee", - events: [{ - time: "11:00", - content: "the 18th Matriculation" - }, { - time: "13:00", - content: "Do-Dream Festival" - }] - }]) + // mockup data + setEvents({ + event_num: 4, + children: [ + { + cid: 1, + cname: "Soo", + event: [ + "the 17th Graduate Seremony", + "Do-Dream Festival" + ] + }, { + cid: 2, + cname: "Hee", + event: [ + // "17th Graduate Seremony", + // "Do-Dream Festival" + ] + } + ] + }) + + if (auth?.authData?.jwt_token) { + fetch('http://localhost:8080/user/children', { + method: 'GET', + headers: { + 'JWT_TOKEN': auth.authData.jwt_token + }, + redirect: 'follow' + }) + .then(response => response.json()) + .then(data => setEvents(data)) // TODO: console.log(data) + .catch((error) => { + console.log(error) + if(error?.response?.status==401) { + //redirect to login + Alert.alert("The session has expired. Please log in again."); + auth.signOut(); + navigation.dispatch(StackActions.popToTop()) + } + }); + } // TODO: fetch API // .then => set nowSelectedChild }, [auth]) @@ -54,14 +78,14 @@ export default function HomeScreen({ navigation }: Navigation) { {"Hi, " + user.username + "!"} - You've got {totalEventsCount} events today. + You've got {events.event_num} events today. Today's Events - {events?.map((notice, index) => + {events.children?.map((notice, index) => handleNowSelectedChildId(notice.cid)}> @@ -72,12 +96,20 @@ export default function HomeScreen({ navigation }: Navigation) { )} - {events.filter(notice => notice.cid == nowSelectedChildId)[0].events.map((event, index) => - - {event.time} - {event.content} - - )} + {events.children?.filter(notice => notice.cid === nowSelectedChildId)[0].event?.length ? ( + events.children?.filter(notice => notice.cid === nowSelectedChildId)[0].event.map((item, index) => + + {/* {item.time} */} + {index+1 + '. ' + item} + + ) + ) : ( + + + There is no event today! + + ) + } @@ -139,7 +171,7 @@ const styles = StyleSheet.create({ noticeWrapper: { width: "88%", flex: 1, - marginBottom: 18 + marginBottom: 18, }, childButtonWrapper: { flexDirection: "row", @@ -158,10 +190,11 @@ const styles = StyleSheet.create({ }, todayNoticeWrapper: { alignSelf: "flex-start", - marginTop: 18, - marginLeft: 12, + paddingTop: 18, + paddingHorizontal: 12, overflow: "scroll", flex: 1, + width: "100%" }, profileImage: { width: 60, @@ -198,5 +231,11 @@ const styles = StyleSheet.create({ }, lightPink: { color: theme.colors.primary, + }, + emptyBox: { + width: '100%', + flexDirection: 'column', + justifyContent: 'center', + alignItems: 'center', } })