fuse_expo/app/(tabs)/index.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-09-10 11:03:30 -05:00
import React, { useState, useEffect } from 'react';
2024-09-25 13:16:33 -05:00
import { StyleSheet, SafeAreaView } from 'react-native';
import { ThemedText } from '@/components/ThemedText';
import { ThemedView } from '@/components/ThemedView';
import { LinearGradient } from 'expo-linear-gradient';
2024-09-10 11:03:30 -05:00
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
2024-09-10 13:30:33 -05:00
const API_KEY = process.env.EXPO_PUBLIC_API_KEY;
const BASE_URL = process.env.EXPO_PUBLIC_BASE_URL;
export default function HomeScreen() {
2024-09-10 11:03:30 -05:00
const [message, setMessage] = useState('Loading message...');
useEffect(() => {
fetchMessage();
const intervalId = setInterval(fetchMessage, 10000); // Every 10 seconds
return () => clearInterval(intervalId);
}, []);
const fetchMessage = async () => {
try {
const storedUser = await AsyncStorage.getItem('@user');
if (!storedUser) {
setMessage('User not found. Please select a user.');
return;
}
const user = JSON.parse(storedUser);
const response = await axios.get(`${BASE_URL}/getMessage`, {
params: { apiKey: API_KEY, userId: user.id }
});
if (response.data && response.data[0] && response.data[0].receivedMessage) {
setMessage(response.data[0].receivedMessage);
} else {
setMessage('No message found.');
}
} catch (error) {
console.error('API call error:', error);
setMessage('Failed to fetch message. Please try again.');
}
};
return (
<LinearGradient
colors={['#F67C0A', '#F60AD3']}
style={styles.container}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
>
2024-09-25 13:16:33 -05:00
<SafeAreaView style={styles.container}>
<ThemedView style={styles.container}>
<ThemedText style={styles.title}>{message}</ThemedText>
</ThemedView>
</SafeAreaView>
</LinearGradient>
2024-09-10 11:03:30 -05:00
);
}
const styles = StyleSheet.create({
2024-09-09 22:57:42 -05:00
container: {
flex: 1,
alignItems: 'center',
2024-09-09 22:57:42 -05:00
justifyContent: 'center',
backgroundColor: 'transparent',
},
2024-09-09 22:57:42 -05:00
title: {
fontSize: 56,
lineHeight: 72,
2024-09-09 22:57:42 -05:00
fontWeight: 'bold',
marginBottom: 20,
textAlign: 'center',
paddingHorizontal: 20,
},
2024-09-10 11:03:30 -05:00
});