I already fetch the data but i don’t know how to turn the time button background from orange(the data isn’t on database) to grey(the data is on database), do i need to change my availableTimes or put another condition on my code ? Thank Your Very Muchh
collection booking
field :
1. createdAt
2. time (value: timestamp)
what this app needs now is:
on touchableopacity fetch day month day and year
on touchablewithoutfeedback fetch only hours
import React, {useState, useEffect} from 'react';
import {
Text,
View,
StyleSheet,
Dimensions,
ScrollView,
Alert,
TouchableOpacity,
TouchableWithoutFeedback,
StatusBar,
ImageBackground,
} from 'react-native';
import {
BORDERRADIUS,
COLORS,
FONTFAMILY,
FONTSIZE,
SPACING,
} from '../theme/theme';
import firestore from '@react-native-firebase/firestore';
import DateTimePicker from '@react-native-community/datetimepicker';
import moment from 'moment';
import CustomIcon from '../components/CustomIcon';
import LinearGradient from 'react-native-linear-gradient';
import AppHeader from '../components/AppHeader';
const windowHeight = Dimensions.get('window').height;
const windowWidth = Dimensions.get('window').width;
const ShowScheduleScreen = ({navigation, route}: any) => {
const [fieldData, setFieldData] = useState([]);
const [date, setDate] = useState(moment().tz('Asia/Singapore').toDate());
const [showDatePicker, setShowDatePicker] = useState(false);
const [bookingData, setBookingData] = useState([]);
const availableTimes = [
new Date(0, 0, 0, 10, 0, 0),
new Date(0, 0, 0, 11, 0, 0),
new Date(0, 0, 0, 12, 0, 0),
new Date(0, 0, 0, 13, 0, 0),
new Date(0, 0, 0, 14, 0, 0),
new Date(0, 0, 0, 15, 0, 0),
new Date(0, 0, 0, 16, 0, 0),
new Date(0, 0, 0, 17, 0, 0),
new Date(0, 0, 0, 18, 0, 0),
new Date(0, 0, 0, 19, 0, 0),
new Date(0, 0, 0, 20, 0, 0),
new Date(0, 0, 0, 21, 0, 0),
new Date(0, 0, 0, 22, 0, 0),
new Date(0, 0, 0, 23, 0, 0),
];
const selectedFieldId = route.params?.fieldid;
const selectedField = fieldData.find(
item => item.id_lapangan === selectedFieldId,
);
const fetchFieldData = async () => {
try {
const querySnapshot = await firestore()
.collection('lapangan')
.orderBy('created_at')
.get();
const data: any = querySnapshot.docs.map(doc => {
const document = doc.data();
document.id_lapangan = doc.id;
document.image = document.image ? document.image : '';
return document;
});
setFieldData(data);
} catch (error) {
console.log('Error fetching field data:', error);
}
};
const fetchBookingData = async () => {
try {
const querySnapshot = await firestore()
.collection('booking')
.where('id_lapangan', '==', selectedFieldId)
.get();
const data: any = querySnapshot.docs.map(doc => {
const document = doc.data();
return document;
});
setBookingData(data);
console.log(data);
} catch (error) {
console.log('Error fetching booking data:', error);
}
};
useEffect(() => {
fetchFieldData();
}, []);
useEffect(() => {
if (selectedFieldId) {
fetchBookingData();
}
}, [selectedFieldId, date]);
const handleDateChange = (event: any, selectedDate: any) => {
const currentDate = selectedDate || date;
setShowDatePicker(false);
const selectedDateTime = moment(currentDate).tz('Asia/Singapore');
const selectedDateOnly = selectedDateTime.format('YYYY-MM-DD');
const today = moment().tz('Asia/Singapore');
const normalizedCurrentDate = moment(selectedDateOnly).startOf('day');
const normalizedToday = moment(today).tz('Asia/Singapore').startOf('day');
if (normalizedCurrentDate.isBefore(normalizedToday)) {
Alert.alert('Tanggal tidak valid', 'Tolong pilih tanggal yang valid');
return;
}
setDate(selectedDateTime.toDate());
};
return (
<ScrollView style={styles.container}>
<StatusBar hidden />
<View>
<ImageBackground
source={{uri: selectedField?.image}}
style={[
styles.imageBG,
{aspectRatio: 2372 / 1727, height: windowWidth * (1727 / 2372)},
]}>
<LinearGradient
colors={[COLORS.BlackRGB10, COLORS.Black]}
style={styles.linearGradient}>
<View style={styles.appHeaderContainer}>
<AppHeader
leftIcon="close"
leftAction={() => navigation.goBack()}
/>
</View>
</LinearGradient>
</ImageBackground>
</View>
<View style={styles.wrapper}>
<View style={styles.containerX}>
<Text style={styles.dateInfoText}>Pilih Tanggal</Text>
<TouchableOpacity
style={styles.touchableInputX}
onPress={() => setShowDatePicker(true)}>
<Text style={styles.touchableInputTextX}>
{moment(date)
.tz('Asia/Singapore')
.locale('id')
.format('dddd, DD MMMM YYYY')}
</Text>
</TouchableOpacity>
{showDatePicker && (
<DateTimePicker
value={moment(date).tz('Asia/Singapore').toDate()}
mode="date"
locale="id-ID"
display="spinner"
onChange={handleDateChange}
/>
)}
</View>
<View style={styles.buttonContainer}>
{Array.from(Array(4), (_, row) => (
<View style={styles.row} key={`row_${row}`}>
{Array.from(Array(row === 0 || row === 3 ? 3 : 4), (_, col) => {
let index;
if (row === 0) {
index = col;
} else if (row === 1) {
index = 3 + col;
} else if (row === 2) {
index = 7 + col;
} else if (row === 3) {
index = 11 + col;
}
if (index < availableTimes.length) {
const time = availableTimes[index];
return (
<TouchableWithoutFeedback
key={`button_${index}`}
disabled={true}>
<View style={styles.button}>
<Text style={styles.buttonText}>
{time.getHours()}:00
</Text>
</View>
</TouchableWithoutFeedback>
);
} else {
return null;
}
})}
</View>
))}
</View>
<View style={styles.timeRadioContainer}>
<View style={styles.radioContainer}>
<CustomIcon
name="radio"
style={[styles.radioIcon, {color: COLORS.Orange}]}
/>
<Text style={styles.radioText}>Tersedia</Text>
</View>
<View style={styles.radioContainer}>
<CustomIcon
name="radio"
style={[styles.radioIcon, {color: COLORS.Grey}]}
/>
<Text style={styles.radioText}>Tidak Tersedia</Text>
</View>
</View>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
height: '100%',
backgroundColor: COLORS.Black,
},
containerX: {
flex: 1,
justifyContent: 'center',
position: 'relative',
marginVertical: SPACING.space_16,
},
appHeaderContainer: {
marginHorizontal: SPACING.space_36,
marginTop: SPACING.space_20 * 2,
},
imageBG: {
width: '100%',
aspectRatio: 3072 / 1727,
},
linearGradient: {
height: '100%',
},
buttonContainer: {
marginTop: SPACING.space_4,
flexDirection: 'column',
alignItems: 'center',
},
wrapper: {
marginVertical: SPACING.space_32,
},
touchableInputX: {
borderWidth: 2,
borderColor: COLORS.White,
backgroundColor: COLORS.Orange,
borderRadius: BORDERRADIUS.radius_10,
marginHorizontal: 40,
paddingVertical: SPACING.space_10,
fontSize: FONTSIZE.size_16,
justifyContent: 'center',
},
touchableInputTextX: {
fontFamily: FONTFAMILY.poppins_semibold,
fontSize: FONTSIZE.size_16,
color: COLORS.White,
textAlign: 'center',
},
dateInfoText: {
fontFamily: FONTFAMILY.poppins_regular,
fontSize: FONTSIZE.size_14,
color: COLORS.White,
marginBottom: SPACING.space_4,
marginLeft: SPACING.space_28,
},
containerGap: {
gap: SPACING.space_36,
},
row: {
flexDirection: 'row',
marginBottom: 10,
},
button: {
width: windowWidth * 0.18,
height: 30,
marginHorizontal: 5,
justifyContent: 'center',
alignItems: 'center',
borderRadius: BORDERRADIUS.radius_10,
borderColor: COLORS.White,
borderWidth: 2,
backgroundColor: COLORS.Orange,
},
buttonText: {
fontFamily: FONTFAMILY.poppins_semibold,
fontSize: FONTSIZE.size_14,
color: COLORS.White,
},
timeRadioContainer: {
flexDirection: 'row',
marginTop: SPACING.space_4,
marginBottom: SPACING.space_10,
alignItems: 'center',
justifyContent: 'space-evenly',
},
radioContainer: {
flexDirection: 'row',
gap: SPACING.space_10,
alignItems: 'center',
},
radioIcon: {
fontSize: FONTSIZE.size_20,
color: COLORS.White,
},
radioText: {
fontFamily: FONTFAMILY.poppins_medium,
fontSize: FONTSIZE.size_12,
color: COLORS.White,
},
});
export default ShowScheduleScreen;