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';
|
2024-09-09 12:09:42 -05:00
|
|
|
import { ThemedText } from '@/components/ThemedText';
|
|
|
|
import { ThemedView } from '@/components/ThemedView';
|
2024-09-10 15:33:34 -05:00
|
|
|
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;
|
2024-09-09 12:09:42 -05:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-09 12:09:42 -05:00
|
|
|
return (
|
2024-09-10 15:33:34 -05:00
|
|
|
<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>
|
2024-09-10 15:33:34 -05:00
|
|
|
</LinearGradient>
|
2024-09-10 11:03:30 -05:00
|
|
|
);
|
2024-09-09 12:09:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-09-09 22:57:42 -05:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
2024-09-09 12:09:42 -05:00
|
|
|
alignItems: 'center',
|
2024-09-09 22:57:42 -05:00
|
|
|
justifyContent: 'center',
|
2024-09-10 15:33:34 -05:00
|
|
|
backgroundColor: 'transparent',
|
2024-09-09 12:09:42 -05:00
|
|
|
},
|
2024-09-09 22:57:42 -05:00
|
|
|
title: {
|
2024-09-10 15:33:34 -05:00
|
|
|
fontSize: 56,
|
|
|
|
lineHeight: 72,
|
2024-09-09 22:57:42 -05:00
|
|
|
fontWeight: 'bold',
|
|
|
|
marginBottom: 20,
|
|
|
|
textAlign: 'center',
|
|
|
|
paddingHorizontal: 20,
|
2024-09-09 12:09:42 -05:00
|
|
|
},
|
2024-09-10 11:03:30 -05:00
|
|
|
});
|