import React, {useState, useEffect} from 'react';
import {
Text,
View,
StyleSheet,
Dimensions,
Image,
ScrollView,
FlatList,
Alert,
TouchableOpacity,
TouchableWithoutFeedback,
} from 'react-native';
import {
BORDERRADIUS,
COLORS,
FONTFAMILY,
FONTSIZE,
SPACING,
} from '../theme/theme';
import firestore from '@react-native-firebase/firestore';
import ScheduleCard from '../components/ScheduleCard';
import DateTimePicker from '@react-native-community/datetimepicker';
import moment from 'moment';
import CustomIcon from '../components/CustomIcon';
const windowHeight = Dimensions.get('window').height;
const windowWidth = Dimensions.get('window').width;
const ScheduleScreen = () => {
const [fieldData, setFieldData] = useState([]);
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);
}
};
useEffect(() => {
fetchFieldData();
}, []);
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 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());
};
const [selectedTime, setSelectedTime] = useState(null);
const [selectedFieldId, setSelectedFieldId] = useState(null);
const handleImageClick = async (selectedFieldId: any) => {
try {
const querySnapshot = await firestore()
.collection('booking')
.where('id_lapangan', '==', selectedFieldId?.id_lapangan)
.get();
const bookings: any = querySnapshot.docs.map(doc => doc.data());
setBookingData(bookings);
setSelectedTime(null);
console.log('Data booking :', bookings);
} catch (error) {
console.log('Error fetching booking data:', error);
}
};
return (
<ScrollView style={styles.container}>
<View style={styles.wrapper}>
<FlatList
data={fieldData}
showsHorizontalScrollIndicator={false}
horizontal
contentContainerStyle={styles.containerGap}
renderItem={({item, index}) => (
<ScheduleCard
shouldMarginatedAtEnd={true}
onPress={() => handleImageClick(item.id_lapangan)}
cardWidth={windowWidth / 2}
isFirst={index == 0 ? true : false}
isLast={index === fieldData.length - 1}
title={item.nama_lapangan}
floor={item.lantai_lapangan}
imagePath={item.image}
/>
)}
/>
<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];
const isTimeBooked = bookingData.find(
booking =>
moment(booking.waktu).isSame(date) &&
moment(booking.waktu).hours() === time.getHours(),
);
return (
<TouchableWithoutFeedback
key={`button_${index}`}
onPress={() => setSelectedTime(time)}>
<View
style={[
styles.button,
isTimeBooked ? styles.buttonUnavailable : null, // Menambahkan kondisi untuk tampilan tombol yang sudah dipesan
]}>
<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,
},
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',
},
textHeader: {
fontFamily: FONTFAMILY.poppins_semibold,
fontSize: FONTSIZE.size_14,
color: COLORS.White,
paddingHorizontal: SPACING.space_36,
paddingVertical: SPACING.space_32,
},
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,
},
buttonUnavailable: {
backgroundColor: COLORS.Grey,
},
buttonSelected: {
backgroundColor: COLORS.White,
},
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 ScheduleScreen;
i have 2 collection lapangan and booking
so after i clicked an image it get data id_lapangan from booking while id_lapangan data is lapangan document id.
my point is after i fetch data frm booking collection, choosing a date and the time on my app it didn’t change the touchablewithoutfeedback bgcolor from orange to grey.
how to make it right thank you.