Skip to content

VULCAN-947/checkbox disable feature based on precondition #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/card/settings/CardSettingsFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const NeoCardSettingsFooter = ({
settingValue={reportSettings[actionsToCustomize]}
type={type}
fields={fields}
preConditionsSetting={reportSettings?.preConditions}
customReportActionsModalOpen={customReportActionsModalOpen}
setCustomReportActionsModalOpen={setCustomReportActionsModalOpen}
onReportSettingUpdate={onReportSettingUpdate}
Expand Down
50 changes: 49 additions & 1 deletion src/chart/table/TableActionsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,58 @@ export const getCheckboxes = (actionsRules, rows, getGlobalParameter) => {
return [...new Set(selection)];
};

export const hasPreCondition = (preConditions) => {
return preConditions.length > 0;
};

const groupConditionsByField = (conditions) => {
return conditions.reduce((acc, condition) => {
if (!acc[condition.field]) {
acc[condition.field] = [];
}
acc[condition.field].push(condition);
return acc;
}, {});
};

const evaluateGroupedConditions = (groupedConditions, row) => {
return Object.keys(groupedConditions).every((field) => {
// Logical OR between conditions for the same field
return groupedConditions[field].some((condition) => evaluateCondition(condition, row));
});
};

export const convertConditionsToExpression = (conditions, row) => {
const groupedConditions = groupConditionsByField(conditions);
return !evaluateGroupedConditions(groupedConditions, row);
};

const evaluateCondition = (condition, row) => {
let fieldValue = row[condition.field];

// Handle Neo4j integer format
if (fieldValue && typeof fieldValue === 'object' && 'low' in fieldValue && 'high' in fieldValue) {
// Assuming we only care about the 'low' value for comparisons
fieldValue = String(fieldValue.low);
}

switch (condition.condition) {
case '=':
return fieldValue === condition.value;
case '!=':
return fieldValue !== condition.value;
case 'contains':
return typeof fieldValue === 'string' && fieldValue.includes(condition.value);
case 'not_contains':
return typeof fieldValue === 'string' && !fieldValue.includes(condition.value);
default:
return false;
}
};

export const updateCheckBoxes = (actionsRules, rows, selection, setGlobalParameter) => {
if (hasCheckboxes(actionsRules)) {
const selectedRows = rows.filter((row) => selection.includes(row.id));
console.log(selectedRows);
let rules = actionsRules.filter((rule) => rule.condition && rule.condition == 'rowCheck');
rules.forEach((rule) => {
const parameterValues = selectedRows.map((row) => row[rule.value]).filter((v) => v !== undefined);
Expand Down
30 changes: 26 additions & 4 deletions src/chart/table/TableChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect } from 'react';
import { DataGrid, GridColumnVisibilityModel } from '@mui/x-data-grid';
import { DataGrid, GridColumnVisibilityModel, GridRowId } from '@mui/x-data-grid';
import { ChartProps } from '../Chart';
import {
evaluateRulesOnDict,
Expand All @@ -23,7 +23,13 @@ import { CloudArrowDownIconOutline, XMarkIconOutline } from '@neo4j-ndl/react/ic
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Button from '@mui/material/Button';
import { extensionEnabled } from '../../utils/ReportUtils';
import { getCheckboxes, hasCheckboxes, updateCheckBoxes } from './TableActionsHelper';
import {
convertConditionsToExpression,
getCheckboxes,
hasCheckboxes,
hasPreCondition,
updateCheckBoxes,
} from './TableActionsHelper';

const TABLE_ROW_HEIGHT = 52;
const HIDDEN_COLUMN_PREFIX = '__';
Expand Down Expand Up @@ -78,6 +84,12 @@ export const NeoTableChart = (props: ChartProps) => {
extensionEnabled(props.extensions, 'actions') && props.settings && props.settings.actionsRules
? props.settings.actionsRules
: [];

const preConditions =
extensionEnabled(props.extensions, 'actions') && props.settings && props.settings.preConditions
? props.settings.preConditions
: [];

const compact = props.settings && props.settings.compact !== undefined ? props.settings.compact : false;
const styleRules = useStyleRules(
extensionEnabled(props.extensions, 'styling'),
Expand Down Expand Up @@ -253,7 +265,9 @@ export const NeoTableChart = (props: ChartProps) => {
for (const [index, rule] of styleRules.entries()) {
if (rule.targetField) {
if (rule.targetField === params.field) {
trueRule = `rule${evaluateSingleRuleOnDict({ [rule.field]: params.row[rule.field] }, rule, index, [e])}`;
trueRule = `rule${evaluateSingleRuleOnDict({ [rule.field]: params.row[rule.field] }, rule, index, [
e,
])}`;
}
} else {
trueRule = `rule${evaluateSingleRuleOnDict({ [params.field]: params.value }, rule, index, [e])}`;
Expand All @@ -266,6 +280,13 @@ export const NeoTableChart = (props: ChartProps) => {
},
};

const isRowSelectable = (params: { id: GridRowId; row: any }) => {
if (hasPreCondition(preConditions)) {
return convertConditionsToExpression(preConditions, params.row);
}
return true;
};

return (
<ThemeProvider theme={theme}>
<div className={classes.root} style={{ height: '100%', width: '100%', position: 'relative' }}>
Expand Down Expand Up @@ -314,13 +335,14 @@ export const NeoTableChart = (props: ChartProps) => {
<DataGrid
{...commonGridProps}
getRowHeight={() => 'auto'}
isRowSelectable={isRowSelectable}
sx={{
...customStyles,
'&.MuiDataGrid-root .MuiDataGrid-cell': { wordBreak: 'break-word' },
}}
/>
) : (
<DataGrid {...commonGridProps} sx={customStyles} />
<DataGrid {...commonGridProps} sx={customStyles} isRowSelectable={isRowSelectable} />
)}
</div>
</ThemeProvider>
Expand Down
151 changes: 151 additions & 0 deletions src/extensions/actions/ActionsRuleCreationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ import { getPageNumbersAndNamesList } from '../advancedcharts/Utils';
import { IconButton, Button, Dialog, Dropdown, TextInput } from '@neo4j-ndl/react';
import { Autocomplete, TextField } from '@mui/material';

// Pre conditions

const PRE_CONDITIONS_RULES = [
{
value: '=',
label: '=',
},
{
value: '!=',
label: '!=',
},
{
value: 'contains',
label: 'contains',
},
{
value: 'not_contains',
label: 'not_contains',
},
];

const defaultPreCondition = {
condition: '=',
field: '',
value: '',
};

// The set of conditional checks that are included in the rule specification.
const RULE_CONDITIONS = {
table: [
Expand Down Expand Up @@ -166,12 +193,17 @@ export const NeoCustomReportActionsModal = ({
fields,
setCustomReportActionsModalOpen,
onReportSettingUpdate,
preConditionsSetting,
}) => {
// The rule set defined in this modal is updated whenever the setting value is externally changed.
const [rules, setRules] = React.useState([]);
const [preConditions, setPreConditions] = React.useState([defaultPreCondition]);
useEffect(() => {
if (settingValue) {
setRules(settingValue);
if (preConditionsSetting) {
setPreConditions(preConditionsSetting);
}
}
}, [settingValue]);

Expand All @@ -183,6 +215,12 @@ export const NeoCustomReportActionsModal = ({
} else {
onReportSettingUpdate(settingName, rules);
}

if (preConditions.length === 0) {
onReportSettingUpdate('preConditions', undefined);
} else {
onReportSettingUpdate('preConditions', preConditions);
}
setCustomReportActionsModalOpen(false);
};

Expand All @@ -193,6 +231,10 @@ export const NeoCustomReportActionsModal = ({
setRules(newRules);
};

const updatePreConditionFieldById = (j, field, value) => {
setPreConditions((prevItems) => prevItems.map((item, i) => (i === j ? { ...item, [field]: value } : item)));
};

/**
* Create the list of suggestions used in the autocomplete box of the rule specification window.
* This will be dynamic based on the type of report we are customizing.
Expand Down Expand Up @@ -535,6 +577,115 @@ export const NeoCustomReportActionsModal = ({
</td>
</tr>
</table>
{rules.some((rule) => rule?.condition === 'rowCheck') && (
<table>
<tr>
<td colSpan={7}>
<tr>
<th colSpan={7} className='n-text-center n-font-bold n-py-2'>
Report Pre Conditions
</th>
</tr>
</td>
</tr>
{preConditions.map((con, i) => {
return (
<tr>
<td width='2.5%' className='n-pr-1'>
<span className='n-pr-1'>{i + 1}.</span>
<span className='n-font-bold'>IF</span>
</td>
<td width='100%'>
<div style={{ border: '2px dashed grey' }} className='n-p-1'>
<Autocomplete
className='n-align-middle n-inline-block n-w-5/12 n-pr-1'
disableClearable={true}
id={`autocomplete-label-type${i}`}
size='small'
noOptionsText='*Specify an exact field name'
options={createFieldVariableSuggestions(null, null, null).filter((e) =>
e.toLowerCase().includes(con.field.toLowerCase())
)}
value={con.field ? con.field : ''}
inputValue={con.field ? con.field : ''}
popupIcon={<></>}
style={{ minWidth: 125 }}
onInputChange={(event, value) => {
updatePreConditionFieldById(i, 'field', value);
}}
onChange={(event, newValue) => {
updatePreConditionFieldById(i, 'field', newValue);
}}
renderInput={(params) => (
<TextField
{...params}
placeholder='Field name...'
InputLabelProps={{ shrink: true }}
style={{ padding: '6px 0 7px' }}
size={'small'}
/>
)}
/>
<Dropdown
type='select'
className='n-align-middle n-w-2/12 n-pr-1'
selectProps={{
onChange: (newValue) => updatePreConditionFieldById(i, 'condition', newValue?.value),
options: PRE_CONDITIONS_RULES.map((option) => ({
label: option.label,
value: option.value,
})),
value: { label: con.condition, value: con.condition },
}}
style={{ minWidth: 70, display: 'inline-block' }}
fluid
/>
<TextInput
className='n-align-middle n-inline-block n-w-5/12'
style={{ minWidth: 100 }}
placeholder='Value...'
value={con.value}
onChange={(e) => updatePreConditionFieldById(i, 'value', e.target.value)}
fluid
></TextInput>
</div>
</td>

<td width='5%'>
<IconButton
aria-label='remove rule'
size='medium'
style={{ marginLeft: 10 }}
floating
onClick={() => {
setPreConditions((prevItems) => prevItems.filter((_, j) => j !== i));
}}
>
<XMarkIconOutline />
</IconButton>
</td>
</tr>
);
})}

<tr>
<td colSpan={7}>
<div className='n-text-center n-mt-1'>
<IconButton
aria-label='add'
size='medium'
floating
onClick={() => {
setPreConditions([...preConditions, defaultPreCondition]);
}}
>
<PlusIconOutline />
</IconButton>
</div>
</td>
</tr>
</table>
)}
</div>
</Dialog.Content>
<Dialog.Actions>
Expand Down
Loading