Skip to content

Commit cf2a6c7

Browse files
maffbarmac
authored andcommitted
chore: refactor updateFormalExpression helper into a reusable utility
1 parent c4b12bc commit cf2a6c7

File tree

5 files changed

+138
-218
lines changed

5 files changed

+138
-218
lines changed

src/provider/bpmn/properties/AdHocCompletionProps.js

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
TextFieldEntry,
77
} from '@bpmn-io/properties-panel';
88

9-
import { createElement } from '../../../utils/ElementUtil';
10-
119
import { useService } from '../../../hooks';
10+
import { createOrUpdateFormalExpression } from '../../../utils/FormalExpressionUtil';
1211

1312
/**
1413
* @typedef { import('@bpmn-io/properties-panel').EntryDefinition } Entry
@@ -52,9 +51,13 @@ function CompletionCondition(props) {
5251
};
5352

5453
const setValue = (value) => {
55-
return commandStack.execute(
56-
'element.updateModdleProperties',
57-
updateFormalExpression(element, 'completionCondition', value, bpmnFactory)
54+
return createOrUpdateFormalExpression(
55+
element,
56+
getBusinessObject(element),
57+
'completionCondition',
58+
value,
59+
bpmnFactory,
60+
commandStack
5861
);
5962
};
6063

@@ -97,60 +100,4 @@ function CancelRemainingInstances(props) {
97100
getValue,
98101
setValue,
99102
});
100-
}
101-
102-
// helper ////////////////////////////
103-
104-
// formal expression /////////////////
105-
106-
/**
107-
* updateFormalExpression - updates a specific formal expression
108-
*
109-
* @param {djs.model.Base} element
110-
* @param {string} propertyName
111-
* @param {string} newValue
112-
* @param {BpmnFactory} bpmnFactory
113-
*/
114-
function updateFormalExpression(element, propertyName, newValue, bpmnFactory) {
115-
const businessObject = getBusinessObject(element);
116-
const expressionProps = {};
117-
118-
if (!newValue) {
119-
120-
// remove formal expression
121-
expressionProps[propertyName] = undefined;
122-
123-
return {
124-
element,
125-
moddleElement: businessObject,
126-
properties: expressionProps,
127-
};
128-
}
129-
130-
const existingExpression = businessObject.get(propertyName);
131-
if (!existingExpression) {
132-
133-
// add formal expression
134-
expressionProps[propertyName] = createElement(
135-
'bpmn:FormalExpression',
136-
{ body: newValue },
137-
businessObject,
138-
bpmnFactory
139-
);
140-
141-
return {
142-
element,
143-
moddleElement: businessObject,
144-
properties: expressionProps,
145-
};
146-
}
147-
148-
// edit existing formal expression
149-
return {
150-
element,
151-
moddleElement: existingExpression,
152-
properties: {
153-
body: newValue,
154-
},
155-
};
156-
}
103+
}

src/provider/bpmn/properties/MultiInstanceProps.js

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
} from '../../../hooks';
1111

1212
import {
13-
createElement
14-
} from '../../../utils/ElementUtil';
13+
createOrUpdateFormalExpression
14+
} from '../../../utils/FormalExpressionUtil';
1515

1616
/**
1717
* @typedef { import('@bpmn-io/properties-panel').EntryDefinition } Entry
@@ -58,9 +58,13 @@ function LoopCardinality(props) {
5858
};
5959

6060
const setValue = (value) => {
61-
return commandStack.execute(
62-
'element.updateModdleProperties',
63-
updateFormalExpression(element, 'loopCardinality', value, bpmnFactory)
61+
return createOrUpdateFormalExpression(
62+
element,
63+
getLoopCharacteristics(element),
64+
'loopCardinality',
65+
value,
66+
bpmnFactory,
67+
commandStack
6468
);
6569
};
6670

@@ -87,9 +91,13 @@ function CompletionCondition(props) {
8791
};
8892

8993
const setValue = (value) => {
90-
return commandStack.execute(
91-
'element.updateModdleProperties',
92-
updateFormalExpression(element, 'completionCondition', value, bpmnFactory)
94+
return createOrUpdateFormalExpression(
95+
element,
96+
getLoopCharacteristics(element),
97+
'completionCondition',
98+
value,
99+
bpmnFactory,
100+
commandStack
93101
);
94102
};
95103

@@ -153,68 +161,6 @@ function getLoopCharacteristics(element) {
153161
return bo.loopCharacteristics;
154162
}
155163

156-
/**
157-
* createFormalExpression - creates a 'bpmn:FormalExpression' element.
158-
*
159-
* @param {ModdleElement} parent
160-
* @param {string} body
161-
* @param {BpmnFactory} bpmnFactory
162-
*
163-
* @result {ModdleElement<bpmn:FormalExpression>} a formal expression
164-
*/
165-
function createFormalExpression(parent, body, bpmnFactory) {
166-
return createElement('bpmn:FormalExpression', { body: body }, parent, bpmnFactory);
167-
}
168-
169-
/**
170-
* updateFormalExpression - updates a specific formal expression of the loop characteristics.
171-
*
172-
* @param {djs.model.Base} element
173-
* @param {string} propertyName
174-
* @param {string} newValue
175-
* @param {BpmnFactory} bpmnFactory
176-
*/
177-
function updateFormalExpression(element, propertyName, newValue, bpmnFactory) {
178-
const loopCharacteristics = getLoopCharacteristics(element);
179-
180-
const expressionProps = {};
181-
182-
if (!newValue) {
183-
184-
// remove formal expression
185-
expressionProps[ propertyName ] = undefined;
186-
187-
return {
188-
element,
189-
moddleElement: loopCharacteristics,
190-
properties: expressionProps
191-
};
192-
}
193-
194-
const existingExpression = loopCharacteristics.get(propertyName);
195-
196-
if (!existingExpression) {
197-
198-
// add formal expression
199-
expressionProps[ propertyName ] = createFormalExpression(loopCharacteristics, newValue, bpmnFactory);
200-
201-
return {
202-
element,
203-
moddleElement: loopCharacteristics,
204-
properties: expressionProps
205-
};
206-
}
207-
208-
// edit existing formal expression
209-
return {
210-
element,
211-
moddleElement: existingExpression,
212-
properties: {
213-
body: newValue
214-
}
215-
};
216-
}
217-
218164
// loopCardinality
219165

220166
/**

src/provider/zeebe/properties/AdHocCompletionProps.js

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import {
44
isFeelEntryEdited,
55
} from '@bpmn-io/properties-panel';
66

7-
import { createElement } from '../../../utils/ElementUtil';
8-
97
import { useService } from '../../../hooks';
10-
118
import { FeelEntryWithVariableContext } from '../../../entries/FeelEntryWithContext';
9+
import { createOrUpdateFormalExpression } from '../../../utils/FormalExpressionUtil';
1210

1311
/**
1412
* @typedef { import('@bpmn-io/properties-panel').EntryDefinition } Entry
@@ -47,9 +45,13 @@ function CompletionCondition(props) {
4745
};
4846

4947
const setValue = (value) => {
50-
return commandStack.execute(
51-
'element.updateModdleProperties',
52-
updateFormalExpression(element, 'completionCondition', value, bpmnFactory)
48+
return createOrUpdateFormalExpression(
49+
element,
50+
getBusinessObject(element),
51+
'completionCondition',
52+
value,
53+
bpmnFactory,
54+
commandStack
5355
);
5456
};
5557

@@ -62,60 +64,4 @@ function CompletionCondition(props) {
6264
setValue,
6365
debounce,
6466
});
65-
}
66-
67-
// helper ////////////////////////////
68-
69-
// formal expression /////////////////
70-
71-
/**
72-
* updateFormalExpression - updates a specific formal expression
73-
*
74-
* @param {djs.model.Base} element
75-
* @param {string} propertyName
76-
* @param {string} newValue
77-
* @param {BpmnFactory} bpmnFactory
78-
*/
79-
function updateFormalExpression(element, propertyName, newValue, bpmnFactory) {
80-
const businessObject = getBusinessObject(element);
81-
const expressionProps = {};
82-
83-
if (!newValue) {
84-
85-
// remove formal expression
86-
expressionProps[propertyName] = undefined;
87-
88-
return {
89-
element,
90-
moddleElement: businessObject,
91-
properties: expressionProps,
92-
};
93-
}
94-
95-
const existingExpression = businessObject.get(propertyName);
96-
if (!existingExpression) {
97-
98-
// add formal expression
99-
expressionProps[propertyName] = createElement(
100-
'bpmn:FormalExpression',
101-
{ body: newValue },
102-
businessObject,
103-
bpmnFactory
104-
);
105-
106-
return {
107-
element,
108-
moddleElement: businessObject,
109-
properties: expressionProps,
110-
};
111-
}
112-
113-
// edit existing formal expression
114-
return {
115-
element,
116-
moddleElement: existingExpression,
117-
properties: {
118-
body: newValue,
119-
},
120-
};
121-
}
67+
}

src/provider/zeebe/properties/MultiInstanceProps.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import {
1414
import {
1515
createElement
1616
} from '../../../utils/ElementUtil';
17+
import {
18+
createOrUpdateFormalExpression
19+
} from '../../../utils/FormalExpressionUtil';
1720

1821
import { useService } from '../../../hooks';
1922

2023
import { FeelEntryWithVariableContext } from '../../../entries/FeelEntryWithContext';
2124

22-
2325
export function MultiInstanceProps(props) {
2426
const {
2527
element
@@ -188,18 +190,14 @@ function CompletionCondition(props) {
188190
};
189191

190192
const setValue = (value) => {
191-
if (value && value !== '') {
192-
const loopCharacteristics = getLoopCharacteristics(element);
193-
const completionCondition = createElement(
194-
'bpmn:FormalExpression',
195-
{ body: value },
196-
loopCharacteristics,
197-
bpmnFactory
198-
);
199-
setCompletionCondition(element, commandStack, completionCondition);
200-
} else {
201-
setCompletionCondition(element, commandStack, undefined);
202-
}
193+
return createOrUpdateFormalExpression(
194+
element,
195+
getLoopCharacteristics(element),
196+
'completionCondition',
197+
value,
198+
bpmnFactory,
199+
commandStack
200+
);
203201
};
204202

205203
return FeelEntryWithVariableContext({
@@ -234,16 +232,6 @@ function getCompletionCondition(element) {
234232
return getLoopCharacteristics(element).get('completionCondition');
235233
}
236234

237-
function setCompletionCondition(element, commandStack, completionCondition = undefined) {
238-
commandStack.execute('element.updateModdleProperties', {
239-
element,
240-
moddleElement: getLoopCharacteristics(element),
241-
properties: {
242-
completionCondition
243-
}
244-
});
245-
}
246-
247235
function getProperty(element, propertyName) {
248236
const loopCharacteristics = getLoopCharacteristics(element),
249237
zeebeLoopCharacteristics = getZeebeLoopCharacteristics(loopCharacteristics);

0 commit comments

Comments
 (0)