2023-04-10 15:02:45 +03:00
|
|
|
import { PlusOutlined } from '@ant-design/icons';
|
|
|
|
|
import { Button, Col, Row } from 'antd';
|
2023-04-19 07:25:18 +03:00
|
|
|
import { MAX_FORMULAS, MAX_QUERIES } from 'constants/queryBuilder';
|
2023-03-27 11:34:06 +03:00
|
|
|
// ** Hooks
|
2023-05-02 17:08:03 +03:00
|
|
|
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
|
2023-04-10 15:02:45 +03:00
|
|
|
// ** Constants
|
2023-05-19 13:14:32 +05:30
|
|
|
import { memo, useEffect, useMemo } from 'react';
|
2023-03-27 11:34:06 +03:00
|
|
|
|
2023-04-01 08:59:35 +03:00
|
|
|
// ** Components
|
2023-04-19 07:25:18 +03:00
|
|
|
import { Formula, Query } from './components';
|
2023-03-27 11:34:06 +03:00
|
|
|
// ** Types
|
|
|
|
|
import { QueryBuilderProps } from './QueryBuilder.interfaces';
|
|
|
|
|
|
2023-04-10 15:02:45 +03:00
|
|
|
export const QueryBuilder = memo(function QueryBuilder({
|
|
|
|
|
config,
|
2023-06-23 11:19:53 +03:00
|
|
|
panelType: newPanelType,
|
2023-06-16 13:38:39 +03:00
|
|
|
actions,
|
2023-07-06 14:22:44 +03:00
|
|
|
inactiveFilters = {},
|
2023-04-10 15:02:45 +03:00
|
|
|
}: QueryBuilderProps): JSX.Element {
|
|
|
|
|
const {
|
2023-05-23 16:47:52 +03:00
|
|
|
currentQuery,
|
|
|
|
|
addNewBuilderQuery,
|
2023-04-19 07:25:18 +03:00
|
|
|
addNewFormula,
|
2023-06-23 11:19:53 +03:00
|
|
|
handleSetConfig,
|
|
|
|
|
panelType,
|
|
|
|
|
initialDataSource,
|
2023-04-10 15:02:45 +03:00
|
|
|
} = useQueryBuilder();
|
2023-03-27 11:34:06 +03:00
|
|
|
|
2023-06-23 11:19:53 +03:00
|
|
|
const currentDataSource = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
(config && config.queryVariant === 'static' && config.initialDataSource) ||
|
|
|
|
|
null,
|
|
|
|
|
[config],
|
|
|
|
|
);
|
2023-04-10 15:02:45 +03:00
|
|
|
|
2023-05-10 19:40:27 +03:00
|
|
|
useEffect(() => {
|
2023-06-23 11:19:53 +03:00
|
|
|
if (currentDataSource !== initialDataSource || newPanelType !== panelType) {
|
|
|
|
|
handleSetConfig(newPanelType, currentDataSource);
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
handleSetConfig,
|
|
|
|
|
panelType,
|
|
|
|
|
initialDataSource,
|
|
|
|
|
currentDataSource,
|
|
|
|
|
newPanelType,
|
|
|
|
|
]);
|
2023-05-10 19:40:27 +03:00
|
|
|
|
2023-04-10 15:02:45 +03:00
|
|
|
const isDisabledQueryButton = useMemo(
|
2023-05-23 16:47:52 +03:00
|
|
|
() => currentQuery.builder.queryData.length >= MAX_QUERIES,
|
|
|
|
|
[currentQuery],
|
2023-04-10 15:02:45 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const isDisabledFormulaButton = useMemo(
|
2023-05-23 16:47:52 +03:00
|
|
|
() => currentQuery.builder.queryFormulas.length >= MAX_FORMULAS,
|
|
|
|
|
[currentQuery],
|
2023-04-10 15:02:45 +03:00
|
|
|
);
|
2023-03-27 11:34:06 +03:00
|
|
|
|
2023-05-10 19:40:27 +03:00
|
|
|
const isAvailableToDisableQuery = useMemo(
|
|
|
|
|
() =>
|
2023-07-05 09:34:20 +05:30
|
|
|
currentQuery.builder.queryData.length > 0 ||
|
2023-05-23 16:47:52 +03:00
|
|
|
currentQuery.builder.queryFormulas.length > 0,
|
|
|
|
|
[currentQuery],
|
2023-05-10 19:40:27 +03:00
|
|
|
);
|
|
|
|
|
|
2023-04-01 08:59:35 +03:00
|
|
|
return (
|
2023-06-19 15:57:58 +03:00
|
|
|
<Row style={{ width: '100%' }} gutter={[0, 20]} justify="start">
|
2023-04-10 15:02:45 +03:00
|
|
|
<Col span={24}>
|
|
|
|
|
<Row gutter={[0, 50]}>
|
2023-05-23 16:47:52 +03:00
|
|
|
{currentQuery.builder.queryData.map((query, index) => (
|
2023-04-10 15:02:45 +03:00
|
|
|
<Col key={query.queryName} span={24}>
|
|
|
|
|
<Query
|
|
|
|
|
index={index}
|
2023-05-10 19:40:27 +03:00
|
|
|
isAvailableToDisable={isAvailableToDisableQuery}
|
2023-04-10 15:02:45 +03:00
|
|
|
queryVariant={config?.queryVariant || 'dropdown'}
|
|
|
|
|
query={query}
|
2023-07-06 14:22:44 +03:00
|
|
|
inactiveFilters={inactiveFilters}
|
2023-04-10 15:02:45 +03:00
|
|
|
/>
|
|
|
|
|
</Col>
|
|
|
|
|
))}
|
2023-05-23 16:47:52 +03:00
|
|
|
{currentQuery.builder.queryFormulas.map((formula, index) => (
|
2023-05-02 17:08:03 +03:00
|
|
|
<Col key={formula.queryName} span={24}>
|
2023-04-19 07:25:18 +03:00
|
|
|
<Formula formula={formula} index={index} />
|
|
|
|
|
</Col>
|
|
|
|
|
))}
|
2023-04-10 15:02:45 +03:00
|
|
|
</Row>
|
|
|
|
|
</Col>
|
|
|
|
|
|
2023-07-06 17:00:20 +03:00
|
|
|
<Col span={24}>
|
2023-06-16 13:38:39 +03:00
|
|
|
<Row gutter={[20, 0]}>
|
|
|
|
|
<Col>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={isDisabledQueryButton}
|
|
|
|
|
type="primary"
|
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
|
onClick={addNewBuilderQuery}
|
|
|
|
|
>
|
|
|
|
|
Query
|
|
|
|
|
</Button>
|
|
|
|
|
</Col>
|
|
|
|
|
<Col>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={isDisabledFormulaButton}
|
|
|
|
|
onClick={addNewFormula}
|
|
|
|
|
type="primary"
|
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
|
>
|
|
|
|
|
Formula
|
|
|
|
|
</Button>
|
|
|
|
|
</Col>
|
|
|
|
|
{actions}
|
|
|
|
|
</Row>
|
2023-07-06 17:00:20 +03:00
|
|
|
</Col>
|
2023-04-10 15:02:45 +03:00
|
|
|
</Row>
|
2023-04-01 08:59:35 +03:00
|
|
|
);
|
2023-04-10 15:02:45 +03:00
|
|
|
});
|