2021-09-23 04:25:21 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-05-03 14:27:32 +00:00
|
|
|
import 'codemirror/theme/base16-light.css';
|
2021-09-21 13:48:28 +00:00
|
|
|
// import { 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-06-03 05:59:13 +00:00
|
|
|
import { CodeHinter } from '../CodeBuilder/CodeHinter';
|
2021-04-10 02:06:17 +00:00
|
|
|
|
2021-09-27 03:40:21 +00:00
|
|
|
export const Transformation = ({ changeOption, currentState, options, darkMode }) => {
|
2021-09-21 13:48:28 +00:00
|
|
|
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-06-03 05:59:13 +00:00
|
|
|
const [value, setValue] = useState(defaultValue);
|
2021-09-27 03:40:21 +00:00
|
|
|
const [enableTransformation, setEnableTransformation] = useState(() => options.enableTransformation);
|
2021-04-18 13:17:55 +00:00
|
|
|
|
2021-09-23 04:25:21 +00:00
|
|
|
// let suggestions = useMemo(() => getSuggestionKeys(currentState), [currentState.components, currentState.queries]);
|
|
|
|
|
function codeChanged(value) {
|
|
|
|
|
setValue(() => value);
|
2021-05-03 14:27:32 +00:00
|
|
|
changeOption('transformation', value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 03:40:21 +00:00
|
|
|
function toggleEnableTransformation() {
|
|
|
|
|
setEnableTransformation((prev) => !prev);
|
|
|
|
|
changeOption('enableTransformation', !enableTransformation);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
return (
|
2021-05-02 06:10:28 +00:00
|
|
|
<div className="field mb-2 transformation-editor">
|
2021-04-30 08:10:57 +00:00
|
|
|
<label className="form-check form-switch my-2">
|
2021-04-30 06:31:32 +00:00
|
|
|
<input
|
2021-04-30 08:10:57 +00:00
|
|
|
className="form-check-input"
|
2021-04-30 06:31:32 +00:00
|
|
|
type="checkbox"
|
2021-09-27 03:40:21 +00:00
|
|
|
onClick={toggleEnableTransformation}
|
|
|
|
|
checked={enableTransformation}
|
2021-04-30 06:31:32 +00:00
|
|
|
/>
|
2021-04-30 08:10:57 +00:00
|
|
|
<span className="form-check-label">Transformations</span>
|
2021-04-30 06:31:32 +00:00
|
|
|
</label>
|
2021-09-27 03:40:21 +00:00
|
|
|
{!enableTransformation && (
|
2021-06-03 07:26:35 +00:00
|
|
|
<div>
|
|
|
|
|
<div className="alert alert-success" role="alert">
|
2021-08-05 07:22:23 +00:00
|
|
|
Transformations can be used to transform the results of queries. All the app variables are accessible from
|
|
|
|
|
transformers and supports JS libraries such as Lodash & Moment.{' '}
|
2021-09-21 13:48:28 +00:00
|
|
|
<a href="https://docs.tooljet.io/docs/tutorial/transformations" target="_blank" rel="noreferrer">
|
2021-08-05 07:22:23 +00:00
|
|
|
Read documentation
|
|
|
|
|
</a>
|
|
|
|
|
.
|
2021-06-03 07:26:35 +00:00
|
|
|
</div>
|
2021-04-10 02:06:17 +00:00
|
|
|
</div>
|
2021-04-30 06:31:32 +00:00
|
|
|
)}
|
|
|
|
|
<br></br>
|
2021-09-27 03:40:21 +00:00
|
|
|
{enableTransformation && (
|
2021-11-15 06:18:09 +00:00
|
|
|
<div>
|
2021-06-03 05:59:13 +00:00
|
|
|
<CodeHinter
|
2021-09-21 13:48:28 +00:00
|
|
|
currentState={currentState}
|
|
|
|
|
initialValue={value}
|
|
|
|
|
mode="javascript"
|
|
|
|
|
theme={darkMode ? 'monokai' : 'base16-light'}
|
|
|
|
|
lineNumbers={true}
|
|
|
|
|
className="query-hinter"
|
|
|
|
|
ignoreBraces={true}
|
2021-09-23 04:25:21 +00:00
|
|
|
onChange={(value) => codeChanged(value)}
|
2021-05-02 06:10:28 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
2021-04-30 06:31:32 +00:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|