@@ -2,81 +2,25 @@ import { schema } from "./searchformSchema";
22import { Typography , Container , Box } from "@mui/material" ;
33import Form from "@rjsf/mui" ;
44import validator from "@rjsf/validator-ajv8" ;
5+ import { Colors } from "design/theme" ;
6+ import { useAppDispatch } from "hooks/useAppDispatch" ;
7+ import { useAppSelector } from "hooks/useAppSelector" ;
58import React from "react" ;
69import { useState } from "react" ;
710import { Link } from "react-router-dom" ;
11+ import { fetchMetadataSearchResults } from "redux/neurojson/neurojson.action" ;
12+ import { RootState } from "redux/store" ;
813import RoutesEnum from "types/routes.enum" ;
914
10- // helper function to build query string
11- const buildQueryString = ( formData : any ) : string => {
12- const map : Record < string , string > = {
13- keyword : "keyword" ,
14- age_min : "agemin" ,
15- age_max : "agemax" ,
16- task_min : "taskmin" ,
17- task_max : "taskmax" ,
18- run_min : "runmin" ,
19- run_max : "runmax" ,
20- sess_min : "sessmin" ,
21- sess_max : "sessmax" ,
22- modality : "modality" ,
23- run_name : "run" ,
24- type_name : "type" ,
25- session_name : "session" ,
26- task_name : "task" ,
27- limit : "limit" ,
28- skip : "skip" ,
29- count : "count" ,
30- unique : "unique" ,
31- gender : "gender" ,
32- database : "dbname" ,
33- dataset : "dsname" ,
34- subject : "subname" ,
35- } ;
36-
37- const params = new URLSearchParams ( ) ;
38- Object . keys ( formData ) . forEach ( ( key ) => {
39- let val = formData [ key ] ;
40- if ( val === "" || val === "any" || val === undefined || val === null )
41- return ;
42-
43- const queryKey = map [ key ] ;
44- if ( ! queryKey ) return ;
45-
46- if ( key . startsWith ( "age" ) ) {
47- params . append ( queryKey , String ( Math . floor ( val * 100 ) ) . padStart ( 5 , "0" ) ) ;
48- } else if ( key === "gender" ) {
49- params . append ( queryKey , val [ 0 ] ) ;
50- } else if ( key === "modality" ) {
51- params . append ( queryKey , val . replace ( / .* \( / , "" ) . replace ( / \) .* / , "" ) ) ;
52- } else {
53- params . append ( queryKey , val . toString ( ) ) ;
54- }
55- } ) ;
56-
57- return `?${ params . toString ( ) } ` ;
58- } ;
59-
6015const SearchPage : React . FC = ( ) => {
61- const [ result , setResult ] = useState < any > ( null ) ;
16+ const dispatch = useAppDispatch ( ) ;
6217 const [ hasSearched , setHasSearched ] = useState ( false ) ;
18+ const searchResults = useAppSelector (
19+ ( state : RootState ) => state . neurojson . searchResults
20+ ) ;
6321
64- // const handleSubmit = ({ formData }: any) => {
65- // console.log("submitted search query:", formData);
66- // };
67- const handleSubmit = async ( { formData } : any ) => {
68- console . log ( "submitted search query:" , formData ) ;
69- const query = buildQueryString ( formData ) ;
70- const url = `https://cors.redoc.ly/https://neurojson.org/io/search.cgi${ query } ` ;
71- // console.log("url", url);
72- try {
73- const res = await fetch ( url ) ;
74- const data = await res . json ( ) ;
75- setResult ( data ) ;
76- console . log ( data ) ;
77- } catch ( err ) {
78- console . error ( "Failed to fetch data:" , err ) ;
79- }
22+ const handleSubmit = ( { formData } : any ) => {
23+ dispatch ( fetchMetadataSearchResults ( formData ) ) ;
8024 setHasSearched ( true ) ;
8125 } ;
8226
@@ -97,32 +41,27 @@ const SearchPage: React.FC = () => {
9741 liveValidate
9842 />
9943
100- { /* {result && (
101- <Box mt={4}>
102- <Typography variant="h6">Datasets Found</Typography>
103- <pre style={{ background: "#f5f5f5", padding: "1rem" }}>
104- {JSON.stringify(result, null, 2)}
105- </pre>
106- </Box>
107- )} */ }
108- { hasSearched && (
44+ { hasSearched && searchResults && (
10945 < Box mt = { 4 } >
110- { Array . isArray ( result ) ? (
111- result . length > 0 ? (
46+ { Array . isArray ( searchResults ) ? (
47+ searchResults . length > 0 ? (
11248 < >
11349 < Typography variant = "h6" >
114- { `Found ${ result . length } Datasets` }
50+ { `Found ${ searchResults . length } Datasets` }
11551 </ Typography >
11652 < ul >
117- { result . map ( ( item , idx ) => {
53+ { searchResults . map ( ( item , idx ) => {
11854 const label = `${ item . dbname } /${ item . dsname } ` ;
11955 const link = `${ RoutesEnum . DATABASES } /${ item . dbname } /${ item . dsname } ` ;
12056
12157 return (
12258 < Box key = { idx } mb = { 1 } >
12359 < Link
12460 to = { link }
125- style = { { textDecoration : "none" , color : "#1976d2" } }
61+ style = { {
62+ textDecoration : "none" ,
63+ color : Colors . blue ,
64+ } }
12665 >
12766 { label }
12867 </ Link >
@@ -138,7 +77,7 @@ const SearchPage: React.FC = () => {
13877 )
13978 ) : (
14079 < Typography color = "error" >
141- { result ?. msg === "empty output"
80+ { searchResults ?. msg === "empty output"
14281 ? "No results found based on your criteria. Please adjust the filters and try again."
14382 : "Something went wrong. Please try again later." }
14483 </ Typography >
0 commit comments