|
1 | | -import { Typography } from "@mui/material"; |
| 1 | +import { schema } from "./searchformSchema"; |
| 2 | +import { Typography, Container, Box } from "@mui/material"; |
| 3 | +import Form from "@rjsf/mui"; |
| 4 | +import validator from "@rjsf/validator-ajv8"; |
2 | 5 | import React from "react"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { Link } from "react-router-dom"; |
| 8 | +import RoutesEnum from "types/routes.enum"; |
| 9 | + |
| 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 | +}; |
3 | 59 |
|
4 | 60 | const SearchPage: React.FC = () => { |
5 | | - return <Typography variant="h1">Search Page</Typography>; |
| 61 | + const [result, setResult] = useState<any>(null); |
| 62 | + const [hasSearched, setHasSearched] = useState(false); |
| 63 | + |
| 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 | + } |
| 80 | + setHasSearched(true); |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <Container maxWidth="md" style={{ marginTop: "2rem" }}> |
| 85 | + <Box |
| 86 | + sx={{ |
| 87 | + backgroundColor: "white", |
| 88 | + p: 3, |
| 89 | + borderRadius: 2, |
| 90 | + boxShadow: 1, |
| 91 | + }} |
| 92 | + > |
| 93 | + <Form |
| 94 | + schema={schema} |
| 95 | + onSubmit={handleSubmit} |
| 96 | + validator={validator} |
| 97 | + liveValidate |
| 98 | + /> |
| 99 | + |
| 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 && ( |
| 109 | + <Box mt={4}> |
| 110 | + {Array.isArray(result) ? ( |
| 111 | + result.length > 0 ? ( |
| 112 | + <> |
| 113 | + <Typography variant="h6"> |
| 114 | + {`Found ${result.length} Datasets`} |
| 115 | + </Typography> |
| 116 | + <ul> |
| 117 | + {result.map((item, idx) => { |
| 118 | + const label = `${item.dbname}/${item.dsname}`; |
| 119 | + const link = `${RoutesEnum.DATABASES}/${item.dbname}/${item.dsname}`; |
| 120 | + |
| 121 | + return ( |
| 122 | + <Box key={idx} mb={1}> |
| 123 | + <Link |
| 124 | + to={link} |
| 125 | + style={{ textDecoration: "none", color: "#1976d2" }} |
| 126 | + > |
| 127 | + {label} |
| 128 | + </Link> |
| 129 | + </Box> |
| 130 | + ); |
| 131 | + })} |
| 132 | + </ul> |
| 133 | + </> |
| 134 | + ) : ( |
| 135 | + <Typography variant="h6"> |
| 136 | + No matching dataset was found |
| 137 | + </Typography> |
| 138 | + ) |
| 139 | + ) : ( |
| 140 | + <Typography color="error"> |
| 141 | + {result?.msg === "empty output" |
| 142 | + ? "No results found based on your criteria. Please adjust the filters and try again." |
| 143 | + : "Something went wrong. Please try again later."} |
| 144 | + </Typography> |
| 145 | + )} |
| 146 | + </Box> |
| 147 | + )} |
| 148 | + </Box> |
| 149 | + </Container> |
| 150 | + ); |
6 | 151 | }; |
7 | 152 |
|
8 | 153 | export default SearchPage; |
0 commit comments