- added form fields calculation and new table calculationl tested in preview;
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import { klona } from 'klona';
|
||||
import { head, isNil , isEmpty } from 'ramda';
|
||||
import { head, isNil, isEmpty, pathOr } from 'ramda';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import 'quill/dist/quill.core.css';
|
||||
import { evaluate } from 'mathjs';
|
||||
@@ -84,23 +84,6 @@ const BandoFormsPreview = () => {
|
||||
if (data.status === 'SUCCESS') {
|
||||
setFormName(data.data.label);
|
||||
const elements = klona(data.data.content);
|
||||
let allvars = {};
|
||||
let allformulas = {};
|
||||
|
||||
// eslint-disable-next-line array-callback-return
|
||||
elements.map((o) => {
|
||||
const variable = head(o.settings.filter(o => o.name === 'variable'));
|
||||
if (variable && !isEmpty(variable.value)) {
|
||||
allvars[o.id] = variable.value[0];
|
||||
}
|
||||
const formula = head(o.settings.filter(o => o.name === 'formula'));
|
||||
if (formula && !isEmpty(formula.value)) {
|
||||
allformulas[o.id] = formula.value;
|
||||
}
|
||||
});
|
||||
|
||||
setFieldsWithVars(allvars);
|
||||
setFieldsWithFormula(allformulas);
|
||||
setFormData(elements);
|
||||
}
|
||||
storeSet.main.unsetAsyncRequest();
|
||||
@@ -112,37 +95,39 @@ const BandoFormsPreview = () => {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!isEmpty(fieldsWithVars) && !isEmpty(fieldsWithFormula)) {
|
||||
const updatedFormValues = klona(formValues);
|
||||
let context = {};
|
||||
let updatedFormValues = klona(formValues);
|
||||
let context = {};
|
||||
|
||||
// eslint-disable-next-line array-callback-return
|
||||
Object.keys(updatedFormValues).map(fieldId => {
|
||||
if (!isNil(fieldsWithFormula[fieldId])) {
|
||||
const formula = fieldsWithFormula[fieldId];
|
||||
context = getTokens(formula)
|
||||
.filter(v => !['false', 'null', 'true'].includes(v))
|
||||
.reduce((acc, cur) => {
|
||||
acc[cur] = isNil(context[cur]) ? 0 : context[cur];
|
||||
return acc;
|
||||
}, {});
|
||||
const mathFormula = renderWithDataVars(formula, context);
|
||||
try {
|
||||
updatedFormValues[fieldId] = evaluate(mathFormula);
|
||||
} catch (e) {
|
||||
console.log('Error in math formula: "', mathFormula, '"', e.message);
|
||||
updatedFormValues[fieldId] = 0;
|
||||
}
|
||||
}
|
||||
if (!isNil(fieldsWithVars[fieldId])) {
|
||||
context[fieldsWithVars[fieldId]] = updatedFormValues[fieldId]
|
||||
}
|
||||
});
|
||||
// eslint-disable-next-line array-callback-return
|
||||
formData.map((o) => {
|
||||
const variable = head(o.settings.filter(o => o.name === 'variable'));
|
||||
const formula = head(o.settings.filter(o => o.name === 'formula'));
|
||||
|
||||
if (!isEmpty(updatedFormValues) && !equal(updatedFormValues, formValues)) {
|
||||
reset(updatedFormValues);
|
||||
if (formula && !isEmpty(formula.value)) {
|
||||
context = getTokens(formula.value)
|
||||
.filter(v => !['false', 'null', 'true'].includes(v))
|
||||
.reduce((acc, cur) => {
|
||||
acc[cur] = isNil(context[cur]) ? 0 : context[cur];
|
||||
return acc;
|
||||
}, context);
|
||||
const mathFormula = renderWithDataVars(formula.value, context);
|
||||
try {
|
||||
updatedFormValues[o.id] = evaluate(mathFormula);
|
||||
} catch (e) {
|
||||
console.log('Error in math formula: "', mathFormula, '"', e.message);
|
||||
updatedFormValues[o.id] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (variable && !isEmpty(variable.value)) {
|
||||
context[variable.value[0]] = 'criteria_table' === o.name
|
||||
? pathOr(0, [o.id, 'total'], updatedFormValues)
|
||||
: pathOr(0, [o.id], updatedFormValues);
|
||||
}
|
||||
});
|
||||
|
||||
if (!isEmpty(updatedFormValues) && !equal(updatedFormValues, formValues)) {
|
||||
reset(updatedFormValues);
|
||||
}
|
||||
}, [formValues]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user