Skip to content

Commit

Permalink
Copy rule
Browse files Browse the repository at this point in the history
  • Loading branch information
thangqp committed Jul 3, 2024
1 parent d645bf0 commit 6ae2c88
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import React from 'react';
import { Tab, Tabs } from '@mui/material';
import PropTypes from 'prop-types';

const FilterBar = (props) => {
const { options, value, setFilter } = props;
const onChange = (_event, value) => setFilter(value);
const TabBar = (props) => {
const { options, value, setValue } = props;
const onChange = (_event, value) => setValue(value);

return (
<Tabs value={value || false} onChange={onChange} centered>
Expand All @@ -27,10 +27,10 @@ const FilterBar = (props) => {
);
};

FilterBar.propTypes = {
TabBar.propTypes = {
options: PropTypes.array.isRequired,
value: PropTypes.string.isRequired,
setFilter: PropTypes.func.isRequired,
setValue: PropTypes.func.isRequired,
};

export default FilterBar;
export default TabBar;
12 changes: 7 additions & 5 deletions src/components/3-organisms/Rule.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,18 @@ const Rule = (props) => {
// ? addFilterLabel
// : addFilterGroupLabel
}
disabled={hasFilter}
/>
<DeleteButton
onClick={deleteFilter}
tooltip={deleteFilterLabel}
disabled={!hasFilter}
/>

<CopyButton
onClick={copyFilter}
tooltip={copyFilterLabel}
/>
{/*<CopyButton*/}
{/* onClick={copyFilter}*/}
{/* tooltip={copyFilterLabel}*/}
{/* disabled={!hasFilter}*/}
{/*/>*/}
</Grid>
</Grid>
{children}
Expand Down
7 changes: 2 additions & 5 deletions src/components/3-organisms/automaton/AutomatonProperties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import { styles } from './AutomatonPropertiesStyle';
import { getPossibleOptionsForProperty } from '../../../utils/automata';
import * as _ from 'lodash';
import { Automaton } from '../../../redux/types/mapping.type';
import {
AutomationDefinition,
PropertyMappingDefinition,
} from '../../../redux/types/model.type';
import { AutomationDefinition } from '../../../redux/types/model.type';
import { EquipmentValues } from '../../../redux/types/network.type';

export interface AutomatonPropertiesProps {
Expand Down Expand Up @@ -70,7 +67,7 @@ const AutomatonProperties = ({
propertyDefinition?.values ??
(propertyDefinition?.mapping &&
getPossibleOptionsForProperty(
propertyDefinition?.mapping as PropertyMappingDefinition,
propertyDefinition?.mapping,
networkPropertyValues
)) ??
[];
Expand Down
29 changes: 21 additions & 8 deletions src/containers/MappingContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
import RuleContainer from './RuleContainer';
import Header from '../components/2-molecules/Header';
import AttachDialog from '../components/2-molecules/AttachDialog';
import FilterBar from '../components/2-molecules/FilterBar';
import TabBar from '../components/2-molecules/TabBar';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import { AddIconButton } from '../components/1-atoms/buttons';
import AutomatonContainer from './AutomatonContainer';
Expand All @@ -60,6 +60,9 @@ const CONTROLLED_PARAMETERS_LABEL = 'Manage model parameters';

const MappingContainer = () => {
// TODO Add path parameter here
const totalRulesNumber = useSelector(
(state) => state.mappings.rules.length
);
const rulesNumber = useSelector(getRulesNumber);
const activeMapping = useSelector((state) => state.mappings.activeMapping);
const isModified = useSelector(isModifiedSelector);
Expand All @@ -75,6 +78,10 @@ const MappingContainer = () => {
const filteredFamily = useSelector(
(state) => state.mappings.filteredAutomatonFamily
);

const totalAutomataNumber = useSelector(
(state) => state.mappings.automata.length
);
const automataNumber = useSelector(getAutomataNumber);
const sortedAutomataNumber = useSelector(getSortedAutomataNumber);
const controlledParameters = useSelector(
Expand Down Expand Up @@ -103,7 +110,7 @@ const MappingContainer = () => {
value: family,
// TODO: intl
label: `${family} (${sortedAutomataNumber[family]})`,
disabled: sortedAutomataNumber[family] === 0,
// disabled: sortedAutomataNumber[family] === 0, always enable automaton tab for each automaton type
})
);

Expand Down Expand Up @@ -202,7 +209,9 @@ const MappingContainer = () => {
</Grid>
<Accordion>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography>{MODELS_TITLE}</Typography>
<Typography>{`${MODELS_TITLE} ${
totalRulesNumber ? '(' + totalRulesNumber + ')' : ''
}`}</Typography>
</AccordionSummary>
<Divider />
<AccordionDetails style={{ display: 'inherit' }}>
Expand All @@ -213,10 +222,10 @@ const MappingContainer = () => {
xs
justifyContent={'flex-start'}
>
<FilterBar
<TabBar
value={filteredType}
options={filterRulesOptions}
setFilter={setFilteredType}
setValue={setFilteredType}
/>
</Grid>
<Grid item xs="auto">
Expand All @@ -231,7 +240,11 @@ const MappingContainer = () => {
</Accordion>
<Accordion>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography>{AUTOMATA_TITLE}</Typography>
<Typography>{`${AUTOMATA_TITLE} ${
totalAutomataNumber
? '(' + totalAutomataNumber + ')'
: ''
}`}</Typography>
</AccordionSummary>
<Divider />
<AccordionDetails style={{ display: 'inherit' }}>
Expand All @@ -242,10 +255,10 @@ const MappingContainer = () => {
xs
justifyContent={'flex-start'}
>
<FilterBar
<TabBar
value={filteredFamily}
options={filterAutomataOptions}
setFilter={setFilteredFamily}
setValue={setFilteredFamily}
/>
</Grid>
<Grid item xs="auto">
Expand Down
4 changes: 2 additions & 2 deletions src/containers/RuleContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ const RuleContainer = ({ index, editParameters }) => {
const deleteFilter = () =>
dispatch(
MappingSlice.actions.deleteFilter({
index,
ruleIndex: index,
// filterIndex,
})
);

const copyFilter = () =>
dispatch(
MappingSlice.actions.copyFilter({
index,
ruleIndex: index,
// filterIndex,
})
);
Expand Down
70 changes: 41 additions & 29 deletions src/redux/slices/Mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const transformMapping = (receivedMapping) => {
// RQB need an id for each rule/group to avoid re-create a new component
// even the same input => lost focus while typing
// This solution can be removed when the back-end returns id persisted for each rule in the db (round-trip)
enrichIdQuery(rule.filter.rules)
enrichIdQuery(rule.filter.rules, false)
);
}
return rule;
Expand Down Expand Up @@ -759,6 +759,15 @@ const reducers = {
const ruleToCopy = _.cloneDeep(
filterRulesByType(state.rules, state.filteredRuleType)[index]
);

// unset id for rule
ruleToCopy.id = undefined;
// if filter exists must unset filter id and provide all new ids for rule/group inside the filter
if (ruleToCopy.filter?.id) {
ruleToCopy.filter.id = undefined;
// force set new ids for the whole query
enrichIdQuery(ruleToCopy.filter.rules, true);
}
state.rules.push(ruleToCopy);
},
// Filter
Expand Down Expand Up @@ -843,39 +852,42 @@ const reducers = {
modifiedFilter.rules = value;
},
deleteFilter: (state, action) => {
const { ruleIndex, filterIndex } = action.payload;
const { ruleIndex /*, filterIndex */ } = action.payload;
const ruleToModify = filterRulesByType(
state.rules,
state.filteredRuleType
)[ruleIndex];
const filterIdToDelete = ruleToModify.filters[filterIndex].id;
const newFilters = ruleToModify.filters.filter(
(value, index) => index !== filterIndex
);
ruleToModify.filters = newFilters;
const modifiedComposition = ruleToModify.composition
.replaceAll(filterIdToDelete, 'true')
.replaceAll('(true)', 'true')
.replaceAll(/ (?:&&|\|\|) true/g, '')
.replaceAll(/true (?:&&|\|\|) /g, '');
ruleToModify.composition = modifiedComposition;
},
copyFilter: (state, action) => {
const { ruleIndex, filterIndex } = action.payload;
const selectedRule = filterRulesByType(
state.rules,
state.filteredRuleType
)[ruleIndex];
let filters = selectedRule.filters;
const filterToCopy = _.cloneDeep(filters[filterIndex]);
const newId = `filter${selectedRule.filterCounter++}`;
filterToCopy.id = newId;
selectedRule.filters.push(filterToCopy);
selectedRule.composition =
selectedRule.filters.length === 1
? newId
: `${selectedRule.composition} && ${newId}`;
ruleToModify.filter = undefined;
// const filterIdToDelete = ruleToModify.filters[filterIndex].id;
// const newFilters = ruleToModify.filters.filter(
// (value, index) => index !== filterIndex
// );
// ruleToModify.filters = newFilters;
// const modifiedComposition = ruleToModify.composition
// .replaceAll(filterIdToDelete, 'true')
// .replaceAll('(true)', 'true')
// .replaceAll(/ (?:&&|\|\|) true/g, '')
// .replaceAll(/true (?:&&|\|\|) /g, '');
// ruleToModify.composition = modifiedComposition;
},
// copyFilter: (state, action) => {
// const { ruleIndex /*, filterIndex */ } = action.payload;
// const selectedRule = filterRulesByType(
// state.rules,
// state.filteredRuleType
// )[ruleIndex];
// const filter = selectedRule.filter;
// const filterToCopy = _.cloneDeep(filter);
// // const newId = `filter${selectedRule.filterCounter++}`;
// filterToCopy.id = uuid4();
// enrichIdQuery(filter.rules, true);
// console.log('filterToCopy', { filterToCopy });
// // selectedRule.filters.push(filterToCopy);
// // selectedRule.composition =
// // selectedRule.filters.length === 1
// // ? newId
// // : `${selectedRule.composition} && ${newId}`;
// },
// Automaton
addAutomaton: (state) => {
const newAutomaton = _.cloneDeep(DEFAULT_AUTOMATON);
Expand Down
4 changes: 3 additions & 1 deletion src/utils/automata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const getPossibleOptionsForProperty = (
...arr,
...(networkPropertyValues?.find(
(equipmentValues) => equipmentValues.type === possibleType
)?.values[propertyMappingDefinition.equipmentProperty] ?? []),
)?.values[
propertyMappingDefinition.equipmentProperty.toUpperCase()
] ?? []),
],
[] as string[]
);
Expand Down
15 changes: 8 additions & 7 deletions src/utils/rqb-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ const visitQuery = (
return query;
};

const idUpdater = (ruleOrGroup: RuleTypeExport | RuleGroupTypeExport) => {
if (ruleOrGroup && !ruleOrGroup.id) {
ruleOrGroup.id = uuid4() as UUID;
}
};
const idUpdaterMaker =
(force: boolean) => (ruleOrGroup: RuleTypeExport | RuleGroupTypeExport) => {
if (force || (ruleOrGroup && !ruleOrGroup.id)) {
ruleOrGroup.id = uuid4() as UUID;
}
};

export const enrichIdQuery = (query: RuleGroupTypeExport) => {
return visitQuery(query, idUpdater);
export const enrichIdQuery = (query: RuleGroupTypeExport, force: boolean) => {
return visitQuery(query, idUpdaterMaker(force));
};

0 comments on commit 6ae2c88

Please sign in to comment.