1
1
import React , { useEffect , useMemo , useState } from 'react' ;
2
- import TodoList from '@/components/todo/TodoList' ;
3
2
import Header from '@/components/Header' ;
4
3
import Calendar from '@/components/calendar/Calendar' ;
5
4
import ActivityTodo from '@/components/todo/ActivityTodo' ;
6
5
import { useLocalStorage } from '@/hooks/useLocalStorage' ;
7
- import { data } from 'framer-motion/client' ;
8
6
import useFetch from '@/hooks/useFetch' ;
9
7
import ActivityDesktopTodo from '@/components/todo/ActivityDesktopTodo' ;
10
8
import { motion } from 'framer-motion' ;
@@ -13,10 +11,22 @@ import AffirmationDisplay from '@/components/todo/WordsOfAffirmation';
13
11
import { useRouter } from 'next/router' ;
14
12
import Swal from 'sweetalert2' ;
15
13
14
+ // ok, for reference future me, this is to delay the funct call to avoid multiple crazy fetches and to save on network usage (i think? it works, so uh dont touch pls)
15
+ function debounce ( fn : Function , delay : number ) {
16
+ let timeoutId : NodeJS . Timeout ;
17
+ return ( ...args : any [ ] ) => {
18
+ if ( timeoutId ) {
19
+ clearTimeout ( timeoutId ) ;
20
+ }
21
+ timeoutId = setTimeout ( ( ) => fn ( ...args ) , delay ) ;
22
+ } ;
23
+ }
24
+
16
25
const IndexPage : React . FC = ( ) => {
17
26
const [ sessionToken , setSessionToken ] = useLocalStorage ( 'session_token' , '' ) ;
18
27
const [ selectedDate , setSelectedDate ] = useState < Date | null > ( null ) ;
19
28
const [ todoData , setTodoData ] = useState < any [ ] > ( [ ] ) ;
29
+ const [ filterStatus , setFilterStatus ] = useState < string > ( '' ) ;
20
30
const router = useRouter ( ) ;
21
31
const importId = router . query . import as string ;
22
32
const connectId = router . query . connectcode as string ;
@@ -25,6 +35,8 @@ const IndexPage: React.FC = () => {
25
35
'session-id' : sessionToken ,
26
36
} ) , [ sessionToken ] ) ;
27
37
38
+ const { data, loading, error, refetch } = useFetch ( `/api/todo?status=${ filterStatus } ` , headers ) ;
39
+
28
40
useEffect ( ( ) => {
29
41
if ( connectId ) {
30
42
const fetchData = async ( ) => {
@@ -71,20 +83,31 @@ const IndexPage: React.FC = () => {
71
83
}
72
84
} , [ connectId ] ) ;
73
85
74
- const { data, loading, error } = useFetch ( '/api/todo/' , headers ) ;
86
+ const debouncedSetFilterStatus = debounce ( ( status : string ) => {
87
+ refetch ( `/api/todo?status=${ status } ` , headers ) ;
88
+ } , 300 ) ;
89
+
90
+ const handleSetFilterStatus = ( status : string ) => {
91
+ setFilterStatus ( status ) ;
92
+ debouncedSetFilterStatus ( status ) ;
93
+ } ;
75
94
76
95
const handleCalendarDateClick = async ( date : Date ) => {
77
96
setSelectedDate ( date ) ;
78
97
79
98
let url = '/api/todo' ;
80
99
100
+ if ( filterStatus ) {
101
+ url += `?status=${ filterStatus } ` ;
102
+ }
103
+
81
104
if ( date . getTime ( ) === 0 ) {
82
105
setSelectedDate ( null ) ;
83
106
} else {
84
107
const nextDay = new Date ( date ) ;
85
108
nextDay . setDate ( date . getDate ( ) + 1 ) ;
86
109
nextDay . setHours ( 0 , 0 , 0 , 0 ) ;
87
- url += `?date=${ nextDay . toISOString ( ) } ` ;
110
+ filterStatus ? url += `&date= ${ nextDay . toISOString ( ) } ` : url += `?date=${ nextDay . toISOString ( ) } ` ;
88
111
}
89
112
90
113
try {
@@ -96,7 +119,6 @@ const IndexPage: React.FC = () => {
96
119
}
97
120
const todoItems = await response . json ( ) ;
98
121
setTodoData ( todoItems ) ;
99
- console . log ( 'To-do data for selected date:' , todoItems ) ;
100
122
} catch ( fetchError ) {
101
123
console . error ( 'Error fetching to-do data:' , fetchError ) ;
102
124
}
@@ -111,6 +133,8 @@ const IndexPage: React.FC = () => {
111
133
if ( data ) {
112
134
setTodoData ( data ) ;
113
135
console . log ( 'Session is valid' ) ;
136
+ } else {
137
+ setTodoData ( [ ] ) ;
114
138
}
115
139
} , [ error , loading , data ] ) ;
116
140
@@ -122,7 +146,7 @@ const IndexPage: React.FC = () => {
122
146
} ) ;
123
147
124
148
if ( response . ok ) {
125
- Swal . fire ( {
149
+ Swal . fire ( {
126
150
title : 'Import successful' ,
127
151
text : 'Your task have been imported successfully' ,
128
152
icon : 'success' ,
@@ -131,11 +155,11 @@ const IndexPage: React.FC = () => {
131
155
timer : 3000 ,
132
156
timerProgressBar : true ,
133
157
showConfirmButton : false ,
134
- } ) ;
158
+ } ) ;
135
159
136
- router . push ( '/' ) ;
160
+ router . push ( '/' ) ;
137
161
138
- setTodoData ( [ ...todoData , await response . json ( ) ] ) ;
162
+ setTodoData ( [ ...todoData , await response . json ( ) ] ) ;
139
163
} else {
140
164
Swal . fire ( {
141
165
title : 'Import failed' ,
@@ -156,46 +180,48 @@ const IndexPage: React.FC = () => {
156
180
157
181
return (
158
182
< >
159
- { loading && (
160
- < div className = 'dark:bg-[#121212] dark:text-white bg-white text-black' >
161
- < div className = 'w-full h-screen flex justify-center items-center' >
162
- < div className = 'text-2xl font-bold' > Loading...</ div >
163
- </ div >
164
- </ div >
165
- ) }
166
- { ! loading && (
167
- < div className = 'dark:bg-[#121212] dark:text-white bg-white text-black min-h-screen' >
168
- < Header />
169
- < div className = 'w-full lg:flex' >
170
- < div className = 'p-4 w-full lg:w-1/3 h-screen justify-center' >
171
- < div className = 'mb-4' >
172
- < motion . div
173
- initial = { { opacity : 0 , y : - 20 } }
174
- animate = { { opacity : 1 , y : 0 } }
175
- transition = { { duration : 0.5 } }
176
- >
177
- < Calendar todoData = { data } onDateClick = { handleCalendarDateClick } />
178
- </ motion . div >
179
- </ div >
180
- < ActivityTodo todoData = { todoData } selectedDate = { selectedDate } />
183
+ { loading && (
184
+ < div className = 'dark:bg-[#121212] dark:text-white bg-white text-black' >
185
+ < div className = 'w-full h-screen flex justify-center items-center' >
186
+ < div className = 'text-2xl font-bold' > Loading...</ div >
181
187
</ div >
182
- < div className = 'p-4 hidden w-full lg:w-1/3 lg:flex justify-center' >
183
- < div className = 'w-full h-full overflow-y-auto scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-gray-400 dark:scrollbar-thumb-gray-600' >
184
- < ActivityDesktopTodo todoData = { todoData } selectedDate = { selectedDate } />
185
- </ div >
186
- </ div >
187
- < div className = 'p-4 w-full lg:w-1/3 lg:flex justify-center' >
188
- < div className = 'w-full h-full overflow-y-auto overflow-x-hidden scrollbar-thin scrollbar-thumb-rounded scrollbar-thumb-gray-400 dark:scrollbar-thumb-gray-600' >
189
- < div className = 'hidden lg:flex' >
190
- < AddTodo />
188
+ </ div >
189
+ ) }
190
+ { ! loading && (
191
+ < div className = "dark:bg-[#121212] dark:text-white bg-white text-black min-h-screen flex flex-col" >
192
+ < Header setFilterStatus = { handleSetFilterStatus } filterStatus = { filterStatus } />
193
+ < div className = "flex-grow flex flex-col lg:flex-row overflow-hidden" >
194
+ < div className = "flex flex-col lg:flex-row flex-grow overflow-y-auto" >
195
+ < div className = "w-full lg:w-1/3 flex flex-col p-4" >
196
+ < motion . div
197
+ initial = { { opacity : 0 , y : - 20 } }
198
+ animate = { { opacity : 1 , y : 0 } }
199
+ transition = { { duration : 0.5 } }
200
+ className = "mb-4"
201
+ >
202
+ < Calendar todoData = { data } onDateClick = { handleCalendarDateClick } />
203
+ </ motion . div >
204
+ < div className = "flex-grow" >
205
+ < ActivityTodo todoData = { todoData } selectedDate = { selectedDate } />
206
+ </ div >
207
+ </ div >
208
+
209
+ < div className = "hidden lg:flex w-full lg:w-1/3 p-4" >
210
+ < ActivityDesktopTodo todoData = { todoData } selectedDate = { selectedDate } />
211
+ </ div >
212
+
213
+ < div className = "w-full lg:w-1/3 flex flex-col p-4" >
214
+ < div className = "hidden lg:block mb-4" >
215
+ < AddTodo />
216
+ </ div >
217
+ < div className = "flex-grow" >
218
+ < AffirmationDisplay />
219
+ </ div >
191
220
</ div >
192
- < AffirmationDisplay />
193
221
</ div >
194
222
</ div >
195
223
</ div >
196
- </ div >
197
- )
198
- }
224
+ ) }
199
225
</ >
200
226
) ;
201
227
} ;
0 commit comments