|
1 | 1 | import {useState} from 'react'; |
2 | 2 | import ListResultTable from "./ListResultTable.jsx"; |
3 | 3 | import {SelectInput, SimpleForm, useResourceContext, required} from "react-admin"; |
4 | | -import {useLocation} from 'react-router-dom'; |
| 4 | +import {useLocation, useNavigate} from 'react-router-dom'; |
| 5 | +import {Component} from "react"; |
5 | 6 |
|
6 | 7 | import config from "../../config"; |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * A custom form to set/choose values for variables for a templated query before that query is executed |
10 | | - * @param props |
| 11 | + * @param {object} props - the props passed down to the component |
11 | 12 | * @returns {Component} the templated query form component |
12 | 13 | */ |
13 | 14 | const TemplatedQueryForm = (props) => { |
14 | 15 | const resource = useResourceContext(); |
| 16 | + const location = useLocation(); |
| 17 | + const navigate = useNavigate(); |
| 18 | + const [variables, setVariables] = useState({}); |
15 | 19 |
|
16 | 20 | const query = config.queries.filter( |
17 | 21 | (query) => query.id === resource |
18 | 22 | )[0]; |
| 23 | + const isTemplatedQuery = query.variables !== undefined; |
| 24 | + let tableEnabled = !isTemplatedQuery; |
19 | 25 |
|
20 | | - if (!query.variables) { |
21 | | - return <ListResultTable {...props}/>; |
22 | | - } |
23 | | - |
24 | | - const location = useLocation(); |
25 | | - const queryParams = new URLSearchParams(location.search); |
26 | | - |
27 | | - const urlVariables = {} |
28 | | - for (const variableName of Object.keys(query.variables)) { |
29 | | - if (queryParams.has(variableName)) { |
30 | | - urlVariables[variableName] = queryParams.get(variableName) |
| 26 | + if (isTemplatedQuery) { |
| 27 | + // Update variables from query parameters |
| 28 | + const queryParams = new URLSearchParams(location.search); |
| 29 | + const queryVariables = {}; |
| 30 | + for (const variableName of Object.keys(query.variables)) { |
| 31 | + if (queryParams.has(variableName)) { |
| 32 | + queryVariables[variableName] = queryParams.get(variableName); |
| 33 | + } |
| 34 | + } |
| 35 | + if (!equalSimpleObjects(variables, queryVariables)) { |
| 36 | + setVariables(queryVariables); |
| 37 | + } else { |
| 38 | + tableEnabled = (Object.keys(variables).length === Object.keys(query.variables).length); |
31 | 39 | } |
32 | | - } |
33 | | - const [variables, setVariables] = useState(urlVariables); |
34 | | - |
35 | | - const [redirectToList, setRedirectToList] = useState(Object.keys(urlVariables).length === Object.keys(query.variables).length) |
36 | | - if (redirectToList) { |
37 | | - return <ListResultTable {...props} variables={variables}/>; |
38 | 40 | } |
39 | 41 |
|
40 | | - const onSubmit = (data) => { |
41 | | - setVariables(data) |
42 | | - setRedirectToList(true); |
| 42 | + const onSubmit = (formVariables) => { |
| 43 | + // Update query parameters from the form fields |
| 44 | + const queryParams = new URLSearchParams(location.search); |
| 45 | + for (const [variableName, variableValue] of Object.entries(formVariables)) { |
| 46 | + if (variableValue) { |
| 47 | + queryParams.set(variableName, variableValue); |
| 48 | + } |
| 49 | + } |
| 50 | + const queryString= queryParams.toString(); |
| 51 | + if (queryString.length > 0) { |
| 52 | + navigate(`?${queryString}`); |
| 53 | + } |
43 | 54 | } |
44 | 55 |
|
45 | 56 | return ( |
46 | | - <SimpleForm onSubmit={onSubmit}> |
47 | | - {Object.entries(query.variables).map(([name, options]) => ( |
48 | | - <SelectInput key={name} source={name} name={name} label={name} validate={required()} choices={ |
49 | | - options.map((option) => ({ |
50 | | - id: option, |
51 | | - name: option |
52 | | - })) |
53 | | - }/> |
54 | | - ))} |
55 | | - </SimpleForm> |
| 57 | + <> |
| 58 | + {isTemplatedQuery && !tableEnabled && ( |
| 59 | + <SimpleForm onSubmit={onSubmit}> |
| 60 | + {Object.entries(query.variables).map(([name, options]) => ( |
| 61 | + <SelectInput key={name} source={name} name={name} label={name} validate={required()} choices={ |
| 62 | + options.map((option) => ({ |
| 63 | + id: option, |
| 64 | + name: option |
| 65 | + })) |
| 66 | + }/> |
| 67 | + ))} |
| 68 | + </SimpleForm> |
| 69 | + )} |
| 70 | + {tableEnabled && ( |
| 71 | + <ListResultTable {...props} variables={variables}/> |
| 72 | + )} |
| 73 | + </> |
56 | 74 | ) |
57 | 75 | } |
58 | 76 |
|
| 77 | +/** |
| 78 | + * Check if two objects have the same property values (of type a primitive value or a string) |
| 79 | + * @param {object} obj1 - the first object |
| 80 | + * @param {object} obj2 - the second object |
| 81 | + * @returns {boolean} true means equal |
| 82 | + */ |
| 83 | +function equalSimpleObjects(obj1, obj2) { |
| 84 | + if (Object.keys(obj1).length !== Object.keys(obj2).length) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + for (const key1 in obj1) { |
| 88 | + if (obj1[key1] !== obj2[key1]) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + return true; |
| 93 | +} |
59 | 94 | export default TemplatedQueryForm; |
0 commit comments