ToolJet/frontend/src/Editor/QueryManager/Transformation.jsx

67 lines
2.2 KiB
React
Raw Normal View History

2021-05-03 16:04:54 +00:00
import React, { useState, useEffect, useMemo } from 'react';
2021-04-10 02:06:17 +00:00
import CodeMirror from '@uiw/react-codemirror';
2021-05-03 14:27:32 +00:00
import 'codemirror/theme/base16-light.css';
2021-05-03 16:04:54 +00:00
import { handleChange, onBeforeChange, getSuggestionKeys } from '../CodeBuilder/utils';
2021-05-03 14:27:32 +00:00
import 'codemirror/mode/javascript/javascript';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/search/match-highlighter';
import 'codemirror/addon/hint/show-hint.css';
2021-04-10 02:06:17 +00:00
2021-05-03 14:27:32 +00:00
export const Transformation = ({ changeOption, options, currentState }) => {
const defaultValue = options.transformation
|| `// write your code here
2021-04-10 02:06:17 +00:00
// return value will be set as data and the original data will be available as rawData
return data.filter(row => row.amount > 1000);`;
2021-04-30 06:31:32 +00:00
const [value, setValue] = useState(defaultValue);
2021-04-30 06:31:32 +00:00
useEffect(() => {
setValue(defaultValue);
}, [defaultValue]);
2021-05-03 14:27:32 +00:00
function codeChanged(value) {
changeOption('transformation', value);
}
2021-05-03 16:04:54 +00:00
let suggestions = useMemo(() => getSuggestionKeys(currentState), [currentState.components, currentState.queries]);
2021-04-30 06:31:32 +00:00
return (
<div className="field mb-2 transformation-editor">
<label className="form-check form-switch my-2">
2021-04-30 06:31:32 +00:00
<input
className="form-check-input"
2021-04-30 06:31:32 +00:00
type="checkbox"
onClick={() => changeOption('enableTransformation', !options.enableTransformation)}
checked={options.enableTransformation}
/>
<span className="form-check-label">Transformations</span>
2021-04-30 06:31:32 +00:00
</label>
{!options.enableTransformation && (
2021-05-03 14:27:32 +00:00
<div className="alert alert-info" role="alert">
2021-04-30 06:31:32 +00:00
Transformations can be used to transform the results of queries.
2021-04-10 02:06:17 +00:00
</div>
2021-04-30 06:31:32 +00:00
)}
<br></br>
{options.enableTransformation && (
<div>
<CodeMirror
2021-05-03 14:27:32 +00:00
height="220px"
fontSize="1"
2021-05-03 16:04:54 +00:00
onChange={(editor) => handleChange(editor, codeChanged, suggestions)}
2021-05-03 14:27:32 +00:00
onBeforeChange={(editor, change) => onBeforeChange(editor, change)}
value={value}
options={{
2021-05-03 14:27:32 +00:00
theme: 'base16-light',
mode: 'javascript',
lineWrapping: true,
2021-05-03 14:27:32 +00:00
scrollbarStyle: null,
highlightSelectionMatches: true,
}}
/>
</div>
2021-04-30 06:31:32 +00:00
)}
</div>
);
};