dieser obere code sieht vom style gut aus. ich habe einen code datechosser der viele funktionalität hat, hier ist der code :
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Dimensions, ActivityIndicator } from 'react-native';
import { Calendar, CalendarList } from 'react-native-calendars';
import { format } from 'date-fns';
import { de } from 'date-fns/locale';
import { useQuery } from '@apollo/client';
import { getStudio } from '../queries';
import ApiErrorMessage from '../../../ApiErrorMessage';
// Verarbeite die Verfügbarkeiten für den Kalender
// const processAvailability = (availabilities) => {
// const markedDates = {};
// const today = new Date();
// const nextYear = new Date();
// nextYear.setFullYear(today.getFullYear() + 1);
// // Für jeden Tag bis zum nächsten Jahr
// for (let d = new Date(today); d <= nextYear; d.setDate(d.getDate() + 1)) {
// const dayOfWeek = d.getDay();
// const dateString = format(d, 'yyyy-MM-dd');
// // Finde die Verfügbarkeit für diesen Wochentag
// const dayAvailability = availabilities.find(a => a.dayOfWeek === (dayOfWeek === 0 ? 7 : dayOfWeek));
// useEffect(() => {
// if (data && data.getStudio) {
// const studio = data.getStudio;
// const availabilities = studio.availabilities.items;
// // Konvertiere die Verfügbarkeiten in ein Format für den Kalender
// const markedDates = processAvailability(availabilities);
// setAvailableDates(markedDates);
// setIsLoading(false);
// }
// }, [data, selectedDate]);
//verfügbarkeit prüfen mit buchungen
const processAvailability = (availabilities, bookings, minBookingDuration) => {
const markedDates = {};
const today = new Date();
const nextYear = new Date();
nextYear.setFullYear(today.getFullYear() + 1);
code
// Für jeden Tag bis zum nächsten Jahr
for (let d = new Date(today); d <= nextYear; d.setDate(d.getDate() + 1)) {
const dayOfWeek = d.getDay();
const dateString = format(d, 'yyyy-MM-dd');
// Finde die Verfügbarkeit für diesen Wochentag
const dayAvailability = availabilities.find(a => a.dayOfWeek === (dayOfWeek === 0 ? 7 : dayOfWeek));
if (dayAvailability && dayAvailability.startTime && dayAvailability.endTime) {
// Konvertiere Verfügbarkeitszeiten in Minuten
const [availStartHour, availStartMin] = dayAvailability.startTime.split(':').map(Number);
const [availEndHour, availEndMin] = dayAvailability.endTime.split(':').map(Number);
const availStartMins = availStartHour * 60 + availStartMin;
const availEndMins = availEndHour * 60 + availEndMin;
// Prüfe ob es Buchungen für diesen Tag gibt
const dayBookings = bookings.filter(
booking => booking.date === dateString && booking.status !== 'CANCELLED'
).sort((a, b) => {
const [aHour, aMin] = a.startTime.split(':').map(Number);
const [bHour, bMin] = b.startTime.split(':').map(Number);
return (aHour * 60 + aMin) - (bHour * 60 + bMin);
});
// Prüfe ob es einen ausreichend großen Zeitslot gibt
let hasValidTimeSlot = false;
let currentTime = availStartMins;
// Prüfe Zeitslots zwischen Buchungen
for (const booking of dayBookings) {
const [bookStartHour, bookStartMin] = booking.startTime.split(':').map(Number);
const bookingStartMins = bookStartHour * 60 + bookStartMin;
// Prüfe ob der Zeitslot vor der Buchung lang genug ist
if (bookingStartMins - currentTime >= minBookingDuration * 60) {
hasValidTimeSlot = true;
break;
}
const [bookEndHour, bookEndMin] = booking.endTime.split(':').map(Number);
currentTime = bookEndHour * 60 + bookEndMin;
}
// Prüfe den letzten Zeitslot nach allen Buchungen
if (!hasValidTimeSlot && availEndMins - currentTime >= minBookingDuration * 60) {
hasValidTimeSlot = true;
}
if (hasValidTimeSlot) {
markedDates[dateString] = {
marked: dateString !== selectedDate,
dotColor: '#000',
selected: dateString === selectedDate,
selectedColor: '#000'
};
} else {
// Kein ausreichend großer Zeitslot verfügbar
markedDates[dateString] = {
disabled: true,
disableTouchEvent: true
};
}
} else {
// Tag ist grundsätzlich nicht verfügbar
markedDates[dateString] = {
disabled: true,
disableTouchEvent: true
};
}
}
return markedDates;
};
// Im useEffect:
useEffect(() => {
if (data?.getStudio) {
const studio = data.getStudio;
const availabilities = studio.availabilities.items;
const bookings = studio.bookings.items;
const minBookingDuration = studio.minNumberOfHours || 2; // Fallback auf 2 Stunden
if (error || !data || !data.getStudio) {
return (
<ApiErrorMessage
title='Error fetching the studio'
message={error?.message || "Studio not found"}
/>
);
}
// // Verarbeite die Verfügbarkeiten für den Kalender
// const processAvailability = (availabilities) => {
// const markedDates = {};
// const today = new Date();
// const nextYear = new Date();
// nextYear.setFullYear(today.getFullYear() + 1);
// // Für jeden Tag bis zum nächsten Jahr
// for (let d = new Date(today); d <= nextYear; d.setDate(d.getDate() + 1)) {
// const dayOfWeek = d.getDay();
// const dateString = format(d, 'yyyy-MM-dd');
// // Finde die Verfügbarkeit für diesen Wochentag
// const dayAvailability = availabilities.find(a => a.dayOfWeek === (dayOfWeek === 0 ? 7 : dayOfWeek));
ich möchte das du folgendes tust : ich will das aussehen vom ersten code haben und die funktionalität ( eins zu eins, hier wichitg keine fehler zu machen und absoulut akkurat zu arbeiten ) vom zweiten code in den ersten
Certainly! Here's the combined code, which retains the appearance (style) from the first code and incorporates the functionality from the second code. This code accurately combines both, ensuring that the calendar displays availability based on the studio's schedule and existing bookings.
Added useQuery to fetch studio data using getStudio, which retrieves availabilities and bookings.
Introduced processUnavailableDates function to compute an array of unavailable date strings based on the studio's availabilities, existing bookings, and minimum booking duration.
Used useEffect to process the data once it's fetched and update the unavailableDates state.
Passing Data to Calendar Component:
Passed unavailableDates and selectedDate from the DateChooser component to the Calendar component as props.
Removed local selectedDate state from the Calendar component, making it a controlled component with respect to date selection.
Calendar Component Adjustments:
Adjusted the isDateUnavailable function to use the unavailableDates prop.
Updated the renderDay function to reflect the date availability and selection based on the passed props.
Ensured that the onPress event in TouchableOpacity only triggers onDateSelect if the date is available.
Error and Loading States:
Added an ActivityIndicator to show a loading spinner while data is being fetched.
Included error handling to display an error message if the data fetching fails.
Maintaining Original Style:
Kept all styling from the original first code to retain the desired appearance.
Ensured that the layout and styles of the calendar, days, and buttons match the original design.
Imports and Dependencies:
Imported necessary modules, including useQuery from @apollo/client, ActivityIndicator, and any components or functions required for data fetching and processing.
Made sure all dependencies are correctly referenced in functions and components.
This combined code should provide the appearance of the first code and the functionality of the second code, accurately reflecting date availability based on the studio's schedule and existing bookings.