fuse_expo/app/(tabs)/countdown.tsx

196 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-09-09 22:57:42 -05:00
import React, { useState, useEffect } from 'react';
2024-09-25 13:16:33 -05:00
import { StyleSheet, ActivityIndicator, TouchableOpacity, View } from 'react-native';
2024-09-09 22:57:42 -05:00
import { ThemedText } from '@/components/ThemedText';
import { ThemedView } from '@/components/ThemedView';
2024-09-25 13:16:33 -05:00
import { SafeAreaView } from 'react-native-safe-area-context'; // Import for safe area handling
import { LinearGradient } from 'expo-linear-gradient';
2024-09-10 00:59:55 -05:00
import axios from 'axios';
2024-09-25 13:16:33 -05:00
import ChangeDateDrawer from '@/components/ChangeDateDrawer'; // Date drawer component
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 22:57:42 -05:00
2024-09-10 00:59:55 -05:00
// Separate API call function
const fetchCountdownDate = async () => {
try {
2024-09-10 11:03:30 -05:00
const response = await axios.get(`${BASE_URL}/getCountdown`, {
params: { apiKey: API_KEY }
});
2024-09-10 00:59:55 -05:00
if (response.data && response.data[0] && response.data[0].countdown) {
return new Date(response.data[0].countdown);
} else {
console.error('Unexpected API response format:', response.data);
return null;
}
} catch (error) {
console.error('API call error:', error);
return null;
}
};
2024-09-09 22:57:42 -05:00
export default function TabTwoScreen() {
const [countdown, setCountdown] = useState({
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
});
2024-09-10 00:59:55 -05:00
const [targetDate, setTargetDate] = useState<Date | null>(null);
const [isLoading, setIsLoading] = useState(true);
2024-09-10 13:30:33 -05:00
const [isDateDrawerVisible, setIsDateDrawerVisible] = useState(false);
2024-09-10 00:59:55 -05:00
useEffect(() => {
const loadCountdownDate = async () => {
setIsLoading(true);
const date = await fetchCountdownDate();
if (date) {
setTargetDate(date);
}
setIsLoading(false);
};
2024-09-09 22:57:42 -05:00
2024-09-10 00:59:55 -05:00
loadCountdownDate();
}, []);
2024-09-09 22:57:42 -05:00
useEffect(() => {
2024-09-10 00:59:55 -05:00
if (targetDate === null) return;
2024-09-09 22:57:42 -05:00
const interval = setInterval(() => {
2024-09-10 00:59:55 -05:00
const now = new Date();
const distance = targetDate.getTime() - now.getTime();
2024-09-09 22:57:42 -05:00
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
setCountdown({ days, hours, minutes, seconds });
if (distance < 0) {
clearInterval(interval);
setCountdown({ days: 0, hours: 0, minutes: 0, seconds: 0 });
}
}, 1000);
return () => clearInterval(interval);
}, [targetDate]);
2024-09-10 13:30:33 -05:00
const handleDateChange = (newDate: Date) => {
setTargetDate(newDate);
};
2024-09-10 00:59:55 -05:00
if (isLoading) {
return (
<ThemedView style={styles.container}>
<ActivityIndicator size="large" color="#0000ff" />
</ThemedView>
);
}
2024-09-11 09:35:14 -05:00
let days = countdown.days === 1 ? 'Day' : 'Days';
let hours = countdown.hours === 1 ? 'Hour' : 'Hours';
let minutes = countdown.minutes === 1 ? 'Minute' : 'Minutes';
let seconds = countdown.seconds === 1 ? 'Second' : 'Seconds';
2024-09-10 00:59:55 -05:00
2024-09-09 22:57:42 -05:00
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.safeContainer}>
<ThemedView style={styles.innerContainer}>
<ThemedText style={styles.title}>Countdown to Next Visit</ThemedText>
<ThemedView style={styles.countdownContainer}>
{/* Countdown items displayed horizontally with flexbox */}
<CountdownItem value={countdown.days} label={days} />
<CountdownItem value={countdown.hours} label={hours} />
<CountdownItem value={countdown.minutes} label={minutes} />
<CountdownItem value={countdown.seconds} label={seconds} />
</ThemedView>
<TouchableOpacity
style={styles.changeButton}
onPress={() => setIsDateDrawerVisible(true)}
>
<ThemedText style={styles.changeButtonText}>Change Date</ThemedText>
</TouchableOpacity>
{targetDate && (
<ChangeDateDrawer
isVisible={isDateDrawerVisible}
onClose={() => setIsDateDrawerVisible(false)}
onDateChange={handleDateChange}
currentDate={targetDate}
/>
)}
</ThemedView>
2024-09-25 13:16:33 -05:00
</SafeAreaView>
</LinearGradient>
2024-09-09 22:57:42 -05:00
);
}
2024-09-10 00:59:55 -05:00
function CountdownItem({ value, label }: { value: number; label: string }) {
2024-09-09 22:57:42 -05:00
return (
<ThemedView style={styles.countdownItem}>
<ThemedText style={styles.countdownValue}>{value}</ThemedText>
<ThemedText style={styles.countdownLabel}>{label}</ThemedText>
</ThemedView>
);
}
const styles = StyleSheet.create({
2024-09-25 13:16:33 -05:00
safeContainer: {
flex: 1,
backgroundColor: 'transparent', // Background color for safety area view
},
2024-09-09 22:57:42 -05:00
container: {
flex: 1,
2024-09-25 13:16:33 -05:00
backgroundColor: 'transparent',
},
innerContainer: {
flex: 1,
justifyContent: 'center',
2024-09-09 22:57:42 -05:00
alignItems: 'center',
2024-09-25 13:16:33 -05:00
paddingHorizontal: 20, // Padding around the edges
backgroundColor: 'transparent',
2024-09-09 22:57:42 -05:00
},
title: {
2024-09-25 13:16:33 -05:00
fontSize: 40, // Shrinking the title for smaller screens
lineHeight: 50,
fontWeight: '600',
2024-09-09 22:57:42 -05:00
textAlign: 'center',
2024-09-25 13:16:33 -05:00
paddingHorizontal: 10,
2024-09-09 22:57:42 -05:00
},
countdownContainer: {
flexDirection: 'row',
2024-09-25 13:16:33 -05:00
justifyContent: 'space-around', // Spread countdown items more evenly
2024-09-09 22:57:42 -05:00
alignItems: 'center',
2024-09-25 13:16:33 -05:00
width: '100%',
backgroundColor: 'transparent',
2024-09-25 13:16:33 -05:00
marginVertical: 40, // Added some space between countdown and title/button
2024-09-09 22:57:42 -05:00
},
countdownItem: {
alignItems: 'center',
2024-09-25 13:16:33 -05:00
marginHorizontal: 10, // Each item has horizontal spacing
backgroundColor: 'transparent',
2024-09-09 22:57:42 -05:00
},
countdownValue: {
fontSize: 36,
lineHeight: 42,
2024-09-09 22:57:42 -05:00
fontWeight: 'bold',
},
countdownLabel: {
2024-09-25 13:16:33 -05:00
fontSize: 18, // Reducing size of the label for better fit
lineHeight: 24,
2024-09-09 22:57:42 -05:00
},
2024-09-10 13:30:33 -05:00
changeButton: {
backgroundColor: '#730FF8',
2024-09-25 13:16:33 -05:00
padding: 12,
borderRadius: 8,
marginTop: 20,
2024-09-10 13:30:33 -05:00
},
changeButtonText: {
color: 'white',
2024-09-25 13:16:33 -05:00
fontSize: 18,
2024-09-10 13:30:33 -05:00
fontWeight: 'bold',
},
2024-09-25 13:16:33 -05:00
});