import { Ionicons, MaterialCommunityIcons, MaterialIcons } from '@expo/vector-icons'; import { addDays, addWeeks, format, getMonth, getYear, isSameDay, startOfWeek, subWeeks } from 'date-fns'; import { useRouter } from 'expo-router'; import * as SecureStore from 'expo-secure-store'; import React, { useEffect, useState } from "react"; import { ScrollView, StyleSheet, Text, TouchableOpacity, View } from "react-native"; const PRIMARY = '#A24BFA'; const BG = '#0c0a0a'; export default function Schedule() { const [currentWeek, setCurrentWeek] = useState(new Date()); const [workdays, setWorkdays] = useState([]); const [loading, setLoading] = useState(false); const router = useRouter(); useEffect(() => { fetchWorkdays(); }, [currentWeek]); async function fetchWorkdays() { setLoading(true); try { const token = await SecureStore.getItemAsync('token'); if (!token) { throw new Error('Nincs elmentett token, kérlek jelentkezz be újra!'); } // Az aktuális hét első és utolsó napja const weekStart = startOfWeek(currentWeek, { weekStartsOn: 1 }); const weekEnd = addDays(weekStart, 6); const months = new Set([ getMonth(weekStart) + 1, getMonth(weekEnd) + 1 ]); const years = new Set([ getYear(weekStart), getYear(weekEnd) ]); let allWorkdays: any[] = []; for (const year of years) { for (const month of months) { const response = await fetch(`https://menuapi.devbeni.lol/api/@me/schedule?year=${year}&month=${month}`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json', }, }); const data = await response.json(); if (data && data.data && Array.isArray(data.data.Data)) { allWorkdays = allWorkdays.concat(data.data.Data); } } } setWorkdays(allWorkdays); } catch (e) { const err = e as any; if (err.response) { console.log('API error response:', { data: err.response.data, }); } else { console.log('Network error:', e); } setWorkdays([]); } finally { setLoading(false); } } function renderCalendar() { const weekStart = startOfWeek(currentWeek, { weekStartsOn: 1 }); const today = new Date(); // 2 soros grid: első sor 4 nap, második sor 3 nap const weekDays = Array.from({ length: 7 }, (_, i) => addDays(weekStart, i)); const firstRow = weekDays.slice(0, 4); const secondRow = weekDays.slice(4); function renderDayBox(day: string | number | Date) { const formattedDate = format(day, 'yyyy-MM-dd'); const wd = workdays.find(w => w.WorkDay?.slice(0, 10) === formattedDate); let bg = '#ede7f6'; let color = '#6a1b9a'; let borderWidth = 0; if (isSameDay(day, today)) { borderWidth = 2; color = PRIMARY; } if (wd) { if (wd.Type === 1) { bg = '#B388FF'; color = '#311b92'; } else if (wd.Type === 10) { bg = '#A24BFA'; color = '#fff'; } } return ( {format(day, 'd')} {wd && wd.Type === 1 && ( PN )} {wd && wd.Type === 10 && wd.text && ( {wd.text} )} ); } return ( setCurrentWeek(subWeeks(currentWeek, 1))}> {format(weekStart, 'yyyy. MMMM d.') + ' - ' + format(addDays(weekStart, 6), 'MMMM d.')} setCurrentWeek(addWeeks(currentWeek, 1))}> {["H", "K", "Sz", "Cs", "P", "Szo", "V"].map(d => ( {d} ))} {loading ? ( Betöltés… ) : ( {firstRow.map(renderDayBox)} {secondRow.map(renderDayBox)} )} ); } return ( Beosztás naptár {(!loading && workdays.length === 0) && ( Nincs beosztás vagy nem vagy bejelentkezve! )} {renderCalendar()} ); } function NavBar({ activeTab }: { activeTab: string }) { const router = useRouter(); return ( router.push('/profile')}> Profilom router.push('/schedule')}> Beosztás router.push('/requests')}> Kérelmek ); } const styles = StyleSheet.create({ card: { backgroundColor: 'rgba(24, 20, 28, 0.95)', borderRadius: 24, padding: 32, width: '90%', maxWidth: 400, shadowColor: '#000', shadowOpacity: 0.3, shadowRadius: 24, shadowOffset: { width: 0, height: 8 }, elevation: 8, marginBottom: 32, marginTop: 32, }, label: { color: '#bdbdbd', fontSize: 16, marginBottom: 16, fontWeight: 'bold', textAlign: 'center', }, navBar: { flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center', backgroundColor: 'rgba(24, 20, 28, 0.98)', borderTopWidth: 1, borderTopColor: '#222', position: 'absolute', bottom: 0, left: 0, right: 0, height: 64, paddingHorizontal: 16, }, navItem: { flex: 1, alignItems: 'center', justifyContent: 'center', }, navLabel: { fontSize: 13, marginTop: 2, fontWeight: 'bold', }, });