@@ -3,7 +3,6 @@ import SearchBar from "@/components/SearchBar";
3
3
import "@/css/Root.css" ;
4
4
import DataTable from "@/components/DataTable" ;
5
5
import PreviewCard from "@/components/PreviewCard" ;
6
- import { location } from "@/data/dataFilters" ;
7
6
8
7
export default function Root ( ) {
9
8
const [ filteredData , setFilteredData ] = useState < MCAPFileInformation [ ] > ( ) ;
@@ -14,9 +13,9 @@ export default function Root() {
14
13
eventType : "" ,
15
14
beforeDate : "" ,
16
15
afterDate : "" ,
16
+ searchText : "" ,
17
17
} ) ;
18
18
const [ search , setSearch ] = useState < boolean > ( false ) ;
19
-
20
19
const formatDate = ( dateStr : string ) => {
21
20
const [ year , month , day ] = dateStr . split ( "-" ) ;
22
21
if ( month . length !== 2 || day . length !== 2 || year . length !== 4 ) {
@@ -26,109 +25,60 @@ export default function Root() {
26
25
} ;
27
26
28
27
const fetchData = async ( filters : SearchFilter ) => {
29
- const { location, date, notes , eventType, filename } = filters ;
28
+ const { location, date, eventType, searchText } = filters ;
30
29
let { afterDate, beforeDate } = filters ;
31
30
32
31
beforeDate = beforeDate ? formatDate ( beforeDate ) : undefined ;
33
32
afterDate = afterDate ? formatDate ( afterDate ) : undefined ;
34
-
35
- const buildParams = ( additionalParams : Record < string , string > = { } ) => {
36
- return {
37
- ...( location ? { location } : { } ) ,
38
- ...( eventType ? { eventType } : { } ) ,
39
- ...( date ? { date } : { } ) ,
40
- ...( afterDate ? { after_date : afterDate } : { } ) ,
41
- ...( beforeDate ? { before_date : beforeDate } : { } ) ,
42
- ...additionalParams ,
43
- } ;
33
+ const params = {
34
+ ...( location ? { location } : { } ) ,
35
+ ...( eventType ? { eventType } : { } ) ,
36
+ ...( date ? { date } : { } ) ,
37
+ ...( afterDate ? { after_date : afterDate } : { } ) ,
38
+ ...( beforeDate ? { before_date : beforeDate } : { } ) ,
39
+ ...( searchText ? { search_text : searchText } : { } ) ,
44
40
} ;
45
41
46
- if ( ! notes && ! filename ) {
47
- const params = buildParams ( ) ;
48
- const queryString = new URLSearchParams ( params ) . toString ( ) ;
49
-
50
- const res = await fetch (
51
- `${ import . meta. env . VITE_API_URL } /api/v2/mcaps?${ queryString } ` ,
52
- ) ;
53
- const data = await res . json ( ) ;
54
- return data . data ;
55
- }
56
-
57
- const promises : Promise < Response > [ ] = [ ] ;
58
-
59
- if ( notes ) {
60
- const paramsNotes = buildParams ( { notes } ) ;
61
- const queryStringNotes = new URLSearchParams ( paramsNotes ) . toString ( ) ;
62
- promises . push (
63
- fetch (
64
- `${ import . meta. env . VITE_API_URL } /api/v2/mcaps?${ queryStringNotes } ` ,
65
- ) ,
66
- ) ;
67
- }
68
-
69
- if ( filename ) {
70
- const paramsFilename = buildParams ( { filename } ) ;
71
- const queryStringFilename = new URLSearchParams (
72
- paramsFilename ,
73
- ) . toString ( ) ;
74
- promises . push (
75
- fetch (
76
- `${ import . meta. env . VITE_API_URL } /api/v2/mcaps?${ queryStringFilename } ` ,
77
- ) ,
78
- ) ;
79
- }
80
-
81
- const results = await Promise . all ( promises ) ;
82
- const data = await Promise . all ( results . map ( ( res ) => res . json ( ) ) ) ;
83
-
84
- const combinedData = data . flatMap ( ( entry ) => entry . data || [ ] ) ;
42
+ const queryString = new URLSearchParams ( params ) . toString ( ) ;
85
43
86
- const uniqueData = Array . from (
87
- new Set ( combinedData . map ( ( item ) => item . id ) ) ,
88
- ) . map ( ( id ) => combinedData . find ( ( item ) => item . id === id ) ) ;
89
-
90
- return uniqueData ;
44
+ const res = await fetch (
45
+ `${ import . meta. env . VITE_API_URL } /api/v2/mcaps?${ queryString } ` ,
46
+ ) ;
47
+
48
+ const data = await res . json ( ) ;
49
+ return data . data ;
91
50
} ;
92
51
93
52
const assignData = async ( ) => {
94
- const apiData = await Promise . all (
95
- location . map ( async ( loc : string ) => await fetchData ( { location : loc } ) ) ,
96
- ) ;
97
- const validData = apiData . filter ( ( data ) => data !== null ) ;
98
-
99
- const flattenedData = validData . flat ( ) ;
100
-
101
- const sortedData = flattenedData . sort ( ( a , b ) => {
102
- const [ monthA , dayA , yearA ] = a . date . split ( "-" ) ;
103
- const [ monthB , dayB , yearB ] = b . date . split ( "-" ) ;
104
- const dateA = new Date ( `${ yearA } -${ monthA } -${ dayA } ` ) ;
105
- const dateB = new Date ( `${ yearB } -${ monthB } -${ dayB } ` ) ;
106
-
53
+ const data = await fetchData ( searchFilters ) ;
54
+ console . log ( data ) ;
55
+ const sortedData = data . sort ( ( a : MCAPFileInformation , b : MCAPFileInformation ) => {
56
+ const dateA = new Date ( a . date ) ;
57
+ const dateB = new Date ( b . date ) ;
107
58
return dateB . getTime ( ) - dateA . getTime ( ) ;
108
59
} ) ;
109
-
110
60
setFilteredData ( sortedData ) ;
111
61
} ;
112
62
113
63
useEffect ( ( ) => {
114
64
assignData ( ) ;
115
65
} , [ ] ) ;
116
66
67
+ // Two useEffects bc of the way we are handling the Search Button D:
117
68
useEffect ( ( ) => {
118
69
const getData = async ( ) => {
119
- const data = await fetchData ( searchFilters ) ;
120
- // console.log(data);
121
- const sortedData = data . sort (
122
- ( a : MCAPFileInformation , b : MCAPFileInformation ) => {
123
- const [ monthA , dayA , yearA ] = a . date . split ( "-" ) ;
124
- const [ monthB , dayB , yearB ] = b . date . split ( "-" ) ;
125
- const dateA = new Date ( `${ yearA } -${ monthA } -${ dayA } ` ) ;
126
- const dateB = new Date ( `${ yearB } -${ monthB } -${ dayB } ` ) ;
127
-
70
+ if ( search ) { // Only fetch data when search is true
71
+ const data = await fetchData ( searchFilters ) ;
72
+ console . log ( data ) ;
73
+ const sortedData = data . sort ( ( a : MCAPFileInformation , b : MCAPFileInformation ) => {
74
+ const dateA = new Date ( a . date ) ;
75
+ const dateB = new Date ( b . date ) ;
128
76
return dateB . getTime ( ) - dateA . getTime ( ) ;
129
- } ,
130
- ) ;
131
- setFilteredData ( sortedData ) ;
77
+ } ) ;
78
+ setFilteredData ( sortedData ) ;
79
+
80
+ setSearch ( false ) ;
81
+ }
132
82
} ;
133
83
getData ( ) ;
134
84
} , [ search ] ) ;
0 commit comments