@@ -23,16 +23,39 @@ import { createLoader } from "../../../utils/createLoader";
2323export const loader = createLoader ( { queryHook : dttQuery } ) ;
2424
2525export default function Home ( ) {
26- const { data : initialData , isLoading } = useDtt ( {
26+ const { data : initialData , isLoading : initialIsLoading } = useDtt ( {
2727 initialData : ( useLoaderData ( ) as [ ApiDtt ] ) [ 0 ] ,
2828 variables : { } ,
2929 } ) ;
30- const [ date , setDate ] = useState < string | undefined > ( ) ;
31- const { data } = useDtt ( {
32- variables : { date } ,
33- } ) ;
30+ const initialDate = initialData ?. date ?? DateTime . now ( ) . toISODate ( ) ;
31+ const [ date , setDate ] = useState < string > ( "initial" ) ;
32+ const resetDate = ( ) => setDate ( "initial" ) ;
33+ const nextDay = ( ) => {
34+ setDate (
35+ DateTime . fromISO ( date === "initial" ? initialDate : date )
36+ . plus ( { days : 1 } )
37+ . toISODate ( )
38+ ) ;
39+ if ( date === initialDate ) resetDate ( ) ;
40+ } ;
41+
42+ const previousDay = ( ) => {
43+ setDate (
44+ DateTime . fromISO ( date === "initial" ? initialDate : date )
45+ . minus ( { days : 1 } )
46+ . toISODate ( )
47+ ) ;
48+ if ( date === initialDate ) resetDate ( ) ;
49+ } ;
3450
35- const initialDate = initialData ?. date ;
51+ const { data, isLoading } = useDtt (
52+ {
53+ variables : { } ,
54+ } ,
55+ date === "initial" ? undefined : date
56+ ) ;
57+
58+ console . log ( data ) ;
3659
3760 const periods : TimetablPeriod [ ] =
3861 data ?. periods ??
@@ -48,8 +71,6 @@ export default function Home() {
4871 room : 605 ,
4972 } ) ;
5073
51- const isLoaded = ! isLoading ;
52-
5374 const [ countdown , setCountdown ] = useState ( "" ) ;
5475
5576 const activeIndex =
@@ -63,39 +84,37 @@ export default function Home() {
6384 return (
6485 < LayoutGroup >
6586 < Flex direction = { "column" } align = "center" gap = { 1.5 } >
66- { initialDate === DateTime . now ( ) . toISODate ( ) && (
87+ { initialDate === DateTime . now ( ) . toISODate ( ) && ( // Initial date === today
6788 < NextPeriod
6889 { ...{
6990 periods : initialPeriods ,
7091 date : initialDate ,
7192 countdown,
7293 setCountdown,
73- isLoaded,
94+ isLoaded : ! initialIsLoading ,
7495 } }
7596 />
7697 ) }
7798 < Flex w = "full" gap = { 3 } as = { motion . div } layout >
7899 < IconButton
79100 icon = { < ArrowLeft /> }
80101 variant = "outline"
81- onClick = { ( ) =>
82- setDate ( DateTime . fromISO ( date ) . minus ( { days : 1 } ) . toISODate ( ) )
83- }
102+ onClick = { previousDay }
84103 aria-label = "Previous day"
85104 />
86105 < Input
87106 type = "date"
88- value = { date ?? initialDate ?? "" }
107+ value = { date === "initial" ? initialDate : date }
89108 onChange = { ( { target : { value } } ) => setDate ( value ) }
90109 focusBorderColor = "primary.200"
91110 as = { motion . input }
92111 layout
93112 />
94113 < AnimatePresence >
95- { date !== initialDate && (
114+ { date !== "initial" && (
96115 < Button
97116 variant = "outline"
98- onClick = { ( ) => setDate ( undefined ) }
117+ onClick = { resetDate }
99118 as = { motion . button }
100119 layout
101120 >
@@ -106,9 +125,7 @@ export default function Home() {
106125 < IconButton
107126 icon = { < ArrowRight /> }
108127 variant = "outline"
109- onClick = { ( ) =>
110- setDate ( DateTime . fromISO ( date ) . plus ( { days : 1 } ) . toISODate ( ) )
111- }
128+ onClick = { nextDay }
112129 aria-label = "Next day"
113130 />
114131 </ Flex >
@@ -129,7 +146,7 @@ export default function Home() {
129146 active = { activeIndex === index }
130147 period = { period }
131148 key = { period . key ?? index + 100 }
132- isLoaded = { isLoaded }
149+ isLoaded = { ! isLoading }
133150 />
134151 ) )
135152 ) : (
0 commit comments