Skip to content

Commit 2c6b59c

Browse files
author
Ian Wensink
committed
fix(calculate-formula): make formulas available for hidden fields
1 parent ab67320 commit 2c6b59c

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/Converse/index.jsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Converse extends Component {
2525

2626
static getTokens(formStore, field) {
2727
if(field.visible) {
28-
const amount = field.componentClass.calculateCurrentAmount(formStore, field);
28+
const amount = Converse.calculateFormula(formStore.values, field.element['#payment_amount']) || 0;
2929
const formattedAmount = parseFloat(amount).toFixed(2);
3030
return {
3131
payment_amount: formattedAmount,
@@ -36,14 +36,34 @@ class Converse extends Component {
3636
return {};
3737
}
3838

39-
static calculateCurrentAmount(formStore, field) {
39+
/**
40+
* financial_amount_monthly_suggestion|0| + financial_amount_monthly|0| + financial_amount_yearly_suggestion|0| + financial_amount_yearly|0|
41+
* @param values
42+
* @param formula
43+
* @returns {*}
44+
*/
45+
static calculateFormula(values, formula) {
4046
const parser = new FormulaParser();
41-
Object.entries(formStore.values).forEach(([key, value]) => {
47+
const formattedFormula = formula.replace(/\|([0-9])\|/g, ''); // Remove all fallback values from formula (|1|)
48+
const formulaParts = formula.split('|'); // Split formula into parts, an array of fields & fallback values ('field|1| + field2|0|' => ['field', 1, 'field2', 0])
49+
parser.on('callVariable', (name, done) => { // Get's called on every variable during calculation
50+
const value = values[name]; // Get field value from store
51+
if(typeof value === 'undefined' || isNaN(parseFloat(value))) { // If value doesn't exist, or isn't a number after parsing
52+
const fallbackIndex = formulaParts.findIndex(item => item.endsWith(name)); // Get index of field in formula parts
53+
const fallbackValue = parseFloat(formulaParts[fallbackIndex + 1]); // Next to field is the fallback value, hence the + 1
54+
if(fallbackIndex !== false && !isNaN(fallbackValue)) { // If the fallback value exists, and is a number
55+
done(fallbackValue);
56+
}
57+
} else {
58+
done(value.replace(',', '.')); // The value exists, make sure it's in the correct format to be recognized as a number
59+
}
60+
});
61+
Object.entries(values).forEach(([key, value]) => {
4262
parser.setVariable(key, value);
4363
});
44-
const amount = parser.parse(field.element['#payment_amount']).result;
64+
const amount = parser.parse(formattedFormula).result;
4565
if(isNaN(amount) || amount === null) {
46-
return 0;
66+
return null;
4767
}
4868
return amount < 0 ? 0 : amount;
4969
}

src/Hidden/index.jsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { observer } from 'mobx-react';
4+
import Converse from '../Converse';
5+
import Form from '../Observables/Form';
6+
27
// styled
38
import StyledHidden from './styled/hidden';
49

510
const Hidden = props => (
611
<StyledHidden
712
{...props}
13+
value={Hidden.rewriteValue(props.value, props.formStore.values)}
814
type='hidden'
915
/>
1016
);
1117

12-
Hidden.meta = {
18+
/**
19+
* ~financial_amount_monthly_suggestion|0| + financial_amount_monthly|0| + financial_amount_yearly_suggestion|0| + financial_amount_yearly|0|~
20+
* @param value
21+
* @param values
22+
* @returns {*}
23+
*/
24+
Hidden.rewriteValue = (value, values) => {
25+
if(!value.startsWith('~') || !value.endsWith('~')) return value;
26+
return Converse.calculateFormula(values, value.slice(1, -1));
27+
};
28+
29+
Hidden.propTypes = {
30+
value: PropTypes.string.isRequired,
31+
formStore: PropTypes.instanceOf(Form).isRequired,
32+
};
33+
34+
const DecoratedHidden = observer(Hidden);
35+
36+
DecoratedHidden.meta = {
1337
wrapper: 'div',
1438
wrapperProps: { style: { display: 'none' } },
1539
};
1640

17-
export default Hidden;
41+
export default DecoratedHidden;

src/Observables/Form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Form {
8282
const values = {};
8383

8484
this.visibleFields.filter(field => !field.isEmpty).forEach((field) => {
85-
values[field.key] = typeof field.componentClass.rewriteValue === 'function' ? field.componentClass.rewriteValue(field.value) : field.value;
85+
values[field.key] = typeof field.componentClass.rewriteValue === 'function' ? field.componentClass.rewriteValue(field.value, values) : field.value;
8686
});
8787

8888
return values;

0 commit comments

Comments
 (0)