|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { View, Text, StyleSheet, Image, ScrollView, Button } from 'react-native'; |
| 3 | +import { useQuery } from 'react-query'; |
| 4 | +import DateTimePicker, { DateTimePickerEvent } from '@react-native-community/datetimepicker'; |
| 5 | +import { format } from 'date-fns'; |
| 6 | + |
| 7 | +import { Colors, envConstants } from '@consts/consts'; |
| 8 | + |
| 9 | +import { useGlobalStore } from '@store/store'; |
| 10 | +import { ApodDataInterface } from 'src/types/types'; |
| 11 | + |
| 12 | +import { CustomSafeAreaView } from '@components/CustomSafeAreaView/CustomSafeAreaView'; |
| 13 | + |
| 14 | +const fetchAPOD = async (date: string): Promise<ApodDataInterface> => { |
| 15 | + const fetchUrl = `https://api.nasa.gov/planetary/apod?api_key=${envConstants.nasaApiKey}&date=${date}`; |
| 16 | + console.log('fetchUrl: ', fetchUrl); |
| 17 | + |
| 18 | + const response = await fetch(fetchUrl); |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error('Network response was not ok'); |
| 21 | + } |
| 22 | + return response.json(); |
| 23 | +}; |
| 24 | + |
| 25 | +export const ApodScreen = () => { |
| 26 | + const { setGlobalLoading } = useGlobalStore(); |
| 27 | + |
| 28 | + const [selectedDate, setSelectedDate] = useState(new Date()); |
| 29 | + const [dateString, setDateString] = useState(format(new Date(), 'yyyy-MM-dd')); |
| 30 | + const [showDatePicker, setShowDatePicker] = useState(false); |
| 31 | + const [isVideo, setIsVideo] = useState(false); |
| 32 | + |
| 33 | + const { data, error, isLoading } = useQuery<ApodDataInterface, Error>(['apod', dateString], () => |
| 34 | + fetchAPOD(dateString), |
| 35 | + ); |
| 36 | + |
| 37 | + React.useEffect(() => { |
| 38 | + if (data) { |
| 39 | + console.log({ data }); |
| 40 | + const verifyVideo = data.url.includes('youtube'); |
| 41 | + setIsVideo(verifyVideo); |
| 42 | + } |
| 43 | + }, [data]); |
| 44 | + |
| 45 | + React.useEffect(() => { |
| 46 | + console.log('isLoading: ', isLoading); |
| 47 | + |
| 48 | + setGlobalLoading(isLoading); |
| 49 | + }, [isLoading]); |
| 50 | + |
| 51 | + const handleDateChange = (event: DateTimePickerEvent, date: Date | undefined) => { |
| 52 | + setShowDatePicker(false); |
| 53 | + if (date) { |
| 54 | + setSelectedDate(date); |
| 55 | + setDateString(format(date, 'yyyy-MM-dd')); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const openDatePicker = () => { |
| 60 | + setShowDatePicker(true); |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <CustomSafeAreaView style={styles.screenContainer}> |
| 65 | + <ScrollView style={{ padding: 10 }}> |
| 66 | + <View> |
| 67 | + <View style={{ justifyContent: 'center', alignItems: 'center', marginBottom: 20 }}> |
| 68 | + <Button title="Select Date" onPress={openDatePicker} /> |
| 69 | + {showDatePicker && ( |
| 70 | + <DateTimePicker |
| 71 | + value={selectedDate} |
| 72 | + mode="date" |
| 73 | + display="default" |
| 74 | + onChange={handleDateChange} |
| 75 | + /> |
| 76 | + )} |
| 77 | + </View> |
| 78 | + |
| 79 | + <View style={{ gap: 10 }}> |
| 80 | + <Text style={styles.title}>{data?.title}</Text> |
| 81 | + <View> |
| 82 | + <Text style={{ fontWeight: 'bold', fontSize: 18 }}>Explanation: </Text> |
| 83 | + <Text style={{ fontSize: 16 }}>{data?.explanation}</Text> |
| 84 | + </View> |
| 85 | + <View style={{ flexDirection: 'row', alignItems: 'center' }}> |
| 86 | + <Text style={{ fontWeight: 'bold', fontSize: 18 }}>Date: </Text> |
| 87 | + <Text style={{ fontSize: 16 }}>{data?.date}</Text> |
| 88 | + </View> |
| 89 | + <View style={{ flexDirection: 'row', alignItems: 'center' }}> |
| 90 | + <Text style={{ fontWeight: 'bold', fontSize: 18 }}>Copyright: </Text> |
| 91 | + <Text style={{ fontSize: 16 }}> |
| 92 | + {data?.copyright ? data.copyright?.trim() : 'No Copyrights'} |
| 93 | + </Text> |
| 94 | + </View> |
| 95 | + <View style={{ flexDirection: 'row', alignItems: 'center' }}> |
| 96 | + <Text style={{ fontWeight: 'bold', fontSize: 18 }}>Media Type: </Text> |
| 97 | + <Text style={{ fontSize: 16 }}>{data?.media_type}</Text> |
| 98 | + </View> |
| 99 | + </View> |
| 100 | + |
| 101 | + {data?.media_type === 'video' ? ( |
| 102 | + <Text style={{ fontSize: 16, marginTop: 10 }}> |
| 103 | + Video format not yet supported. Please wait for new updates.{' '} |
| 104 | + </Text> |
| 105 | + ) : ( |
| 106 | + <View |
| 107 | + style={{ |
| 108 | + borderWidth: 3, |
| 109 | + borderColor: Colors.purple, |
| 110 | + borderRadius: 4, |
| 111 | + marginTop: 10, |
| 112 | + }} |
| 113 | + > |
| 114 | + <Image source={{ uri: data?.url }} style={styles.image} /> |
| 115 | + </View> |
| 116 | + )} |
| 117 | + </View> |
| 118 | + </ScrollView> |
| 119 | + </CustomSafeAreaView> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +const styles = StyleSheet.create({ |
| 124 | + screenContainer: { |
| 125 | + flex: 1, |
| 126 | + padding: 8, |
| 127 | + backgroundColor: Colors.lightGray, |
| 128 | + }, |
| 129 | + title: { |
| 130 | + fontSize: 24, |
| 131 | + fontWeight: 'bold', |
| 132 | + }, |
| 133 | + image: { |
| 134 | + width: '100%', |
| 135 | + height: 300, |
| 136 | + // marginVertical: 10, |
| 137 | + }, |
| 138 | + video: { |
| 139 | + alignSelf: 'center', |
| 140 | + width: '100%', |
| 141 | + height: 300, |
| 142 | + }, |
| 143 | +}); |
0 commit comments