Skip to content

Commit 31e8324

Browse files
committed
refactor: dynamically generate database options in search form from registry
1 parent 8d6e8b2 commit 31e8324

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

src/pages/SearchPage.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import { schema } from "./searchformSchema";
1+
import { generateSchemaWithDatabaseEnum } from "./searchformSchema";
2+
// schema
23
import { Typography, Container, Box } from "@mui/material";
34
import Form from "@rjsf/mui";
45
import validator from "@rjsf/validator-ajv8";
56
import { Colors } from "design/theme";
67
import { useAppDispatch } from "hooks/useAppDispatch";
78
import { useAppSelector } from "hooks/useAppSelector";
89
import React from "react";
9-
import { useState } from "react";
10+
import { useState, useEffect, useMemo } from "react";
11+
// useEffect useMemo
1012
import { Link } from "react-router-dom";
11-
import { fetchMetadataSearchResults } from "redux/neurojson/neurojson.action";
13+
import {
14+
fetchMetadataSearchResults,
15+
fetchRegistry,
16+
} from "redux/neurojson/neurojson.action";
1217
import { RootState } from "redux/store";
1318
import RoutesEnum from "types/routes.enum";
1419

@@ -18,6 +23,24 @@ const SearchPage: React.FC = () => {
1823
const searchResults = useAppSelector(
1924
(state: RootState) => state.neurojson.searchResults
2025
);
26+
const registry = useAppSelector(
27+
(state: RootState) => state.neurojson.registry
28+
);
29+
30+
console.log("registry:", registry);
31+
console.log("result:", searchResults);
32+
33+
useEffect(() => {
34+
dispatch(fetchRegistry());
35+
}, [dispatch]);
36+
37+
// dynamic add database enum to schema
38+
const schema = useMemo(() => {
39+
const dbList = registry?.length
40+
? [...registry.map((item: any) => item.id), "any"]
41+
: ["any"];
42+
return generateSchemaWithDatabaseEnum(dbList);
43+
}, [registry]);
2144

2245
const handleSubmit = ({ formData }: any) => {
2346
dispatch(fetchMetadataSearchResults(formData));
@@ -100,6 +123,7 @@ const SearchPage: React.FC = () => {
100123
textDecoration: "none",
101124
color: Colors.blue,
102125
}}
126+
target="_blank"
103127
>
104128
{label}
105129
</Link>

src/pages/searchformSchema.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { JSONSchema7 } from "json-schema";
22

3-
export const schema: JSONSchema7 = {
3+
export const baseSchema: JSONSchema7 = {
44
title: "Metadata Search",
55
type: "object",
66
properties: {
77
keyword: {
88
title: "Search keyword",
99
type: "string",
1010
},
11-
database: {
12-
title: "Search database",
13-
type: "string",
14-
default: "any",
15-
enum: [
16-
"openneuro",
17-
"abide",
18-
"abide2",
19-
"datalad-registry",
20-
"adhd200",
21-
"any",
22-
],
23-
},
11+
// database: {
12+
// title: "Search database",
13+
// type: "string",
14+
// default: "any",
15+
// enum: [
16+
// "openneuro",
17+
// "abide",
18+
// "abide2",
19+
// "datalad-registry",
20+
// "adhd200",
21+
// "any",
22+
// ],
23+
// },
2424
dataset: {
2525
title: "Search dataset",
2626
type: "string",
@@ -138,3 +138,21 @@ export const schema: JSONSchema7 = {
138138
// },
139139
},
140140
};
141+
142+
// Helper to inject dynamic "database" enum
143+
export const generateSchemaWithDatabaseEnum = (
144+
databaseEnum: string[]
145+
): JSONSchema7 => {
146+
return {
147+
...baseSchema,
148+
properties: {
149+
database: {
150+
title: "Search database",
151+
type: "string",
152+
default: "any",
153+
enum: databaseEnum,
154+
},
155+
...baseSchema.properties,
156+
},
157+
};
158+
};

src/services/neurojson.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const NeurojsonService = {
7373
};
7474

7575
const params = new URLSearchParams();
76+
params.append("_get", "dbname, dsname, json");
7677
Object.keys(formData).forEach((key) => {
7778
let val = formData[key];
7879
if (val === "" || val === "any" || val === undefined || val === null)

0 commit comments

Comments
 (0)