|
1 | | -import {useState} from 'react'; |
2 | | -import ListResultTable from "./ListResultTable.jsx"; |
3 | | -import {SelectInput, SimpleForm, useResourceContext, required} from "react-admin"; |
4 | | -import {useLocation, useNavigate} from 'react-router-dom'; |
| 1 | +import {SelectInput, SimpleForm, required} from "react-admin"; |
5 | 2 | import {Component} from "react"; |
6 | | - |
7 | | -import config from "../../config"; |
| 3 | +import PropTypes from "prop-types"; |
8 | 4 |
|
9 | 5 | /** |
10 | 6 | * A custom form to set/choose values for variables for a templated query before that query is executed |
11 | 7 | * @param {object} props - the props passed down to the component |
12 | 8 | * @returns {Component} the templated query form component |
13 | 9 | */ |
14 | 10 | const TemplatedQueryForm = (props) => { |
15 | | - const resource = useResourceContext(); |
16 | | - const location = useLocation(); |
17 | | - const navigate = useNavigate(); |
18 | | - const [variables, setVariables] = useState({}); |
19 | | - |
20 | | - const query = config.queries.filter( |
21 | | - (query) => query.id === resource |
22 | | - )[0]; |
23 | | - const isTemplatedQuery = query.variables !== undefined; |
24 | | - let tableEnabled = !isTemplatedQuery; |
25 | | - |
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); |
39 | | - } |
40 | | - } |
41 | | - |
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 | | - } |
54 | | - } |
55 | | - |
| 11 | + const { |
| 12 | + variableOptions, |
| 13 | + onSubmit |
| 14 | + } = props; |
56 | 15 | return ( |
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 | | - </> |
74 | | - ) |
| 16 | + <SimpleForm onSubmit={onSubmit}> |
| 17 | + {Object.entries(variableOptions).map(([name, options]) => ( |
| 18 | + <SelectInput key={name} source={name} name={name} label={name} validate={required()} choices={ |
| 19 | + options.map((option) => ({ |
| 20 | + id: option, |
| 21 | + name: option |
| 22 | + })) |
| 23 | + }/> |
| 24 | + ))} |
| 25 | + </SimpleForm> |
| 26 | + ); |
75 | 27 | } |
76 | 28 |
|
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 | | -} |
| 29 | +TemplatedQueryForm.propTypes = { |
| 30 | + variableOptions: PropTypes.object, |
| 31 | + onSubmit: PropTypes.func, |
| 32 | +}; |
| 33 | + |
94 | 34 | export default TemplatedQueryForm; |
0 commit comments