Skip to content

Commit

Permalink
feat: loop for same form submission, ie. concatenate repeated questions
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-mccombs committed Nov 10, 2021
1 parent d4286b2 commit e642e1a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
14 changes: 11 additions & 3 deletions components/FormikFields/PaperInputPicker/Looper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import I18n from '../../../../modules/i18n';
const Looper = ({
data, config, additionalQuestions,
translatedLabel, setAdditionalQuestions,
loopsAdded, setLoopsAdded
loopsAdded, setLoopsAdded, sameForm
}) => {
const {
formikKey, numberQuestionsToRepeat
Expand Down Expand Up @@ -62,17 +62,25 @@ const Looper = ({
let updatedQuestions = [];
questionsToRepeat.forEach((question) => {
const updatedQuestion = _.cloneDeep(question);
updatedQuestion.formikKey = `${updatedQuestion.formikKey}__loop${individualLoopsAdded}`;
if (sameForm !== true) {
updatedQuestion.formikKey = `${updatedQuestion.formikKey}__loop${individualLoopsAdded}`;
} else {
updatedQuestion.formikKey = `${updatedQuestion.formikKey}__sameForm${individualLoopsAdded}`;
}
updatedQuestions = updatedQuestions.concat(updatedQuestion);
});

setAdditionalQuestions(additionalQuestions.concat(updatedQuestions));
if (sameForm !== true) {
setLoopsAdded(loopsAdded + 1);
}
setIndividualLoopsAdded(individualLoopsAdded + 1);
};

const removeLoop = () => {
setLoopsAdded(loopsAdded - 1);
if (sameForm !== true) {
setLoopsAdded(loopsAdded - 1);
}
setIndividualLoopsAdded(individualLoopsAdded - 1);
setAdditionalQuestions(additionalQuestions.slice(0,
additionalQuestions.length - numberQuestionsToRepeat));
Expand Down
26 changes: 26 additions & 0 deletions components/FormikFields/PaperInputPicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,32 @@ const PaperInputPicker = ({
/>
</View>
)}
{/* relies on function to clean the values prior to submission */}
{fieldType === 'loopSameForm' && (
<View key={formikKey}>
{additionalQuestions !== undefined && additionalQuestions.length !== 0
&& additionalQuestions.map((question) => (
<PaperInputPicker
data={question}
formikProps={formikProps}
customForm={customForm}
config={config}
surveyingOrganization={surveyingOrganization}
scrollViewScroll={scrollViewScroll}
setScrollViewScroll={setScrollViewScroll}
/>
))}
<Looper
data={data}
config={config}
additionalQuestions={additionalQuestions}
setAdditionalQuestions={setAdditionalQuestions}
translatedLabel={translatedLabel}
sameForm={true}
/>
</View>
)}

</>
);
};
Expand Down
6 changes: 5 additions & 1 deletion domains/DataCollection/Assets/NewAssets/AssetCore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getData } from '../../../../../modules/async-storage';
import { postAssetForm } from '../../../../../modules/cached-resources';
import I18n from '../../../../../modules/i18n';
import { isEmpty } from '../../../../../modules/utils';
import { cleanLoopSubmissions } from '../../../Forms/SupplementaryForm/utils';
import surveyingUserFailsafe from '../../../Forms/utils';
import configArray from './config/config';
import styles from './index.styles';
Expand Down Expand Up @@ -52,12 +53,15 @@ const AssetCore = ({
formObject.longitude = values.location?.longitude || 0;
formObject.altitude = values.location?.altitude || 0;

// add any looped values to formObject
const formObjectUpdated = cleanLoopSubmissions(values, formObject);

const postParams = {
parseClass: 'Assets',
parseUser: user.objectId,
signature: 'Asset Signature',
photoFile: 'photo',
localObject: formObject
localObject: formObjectUpdated
};

postAssetForm(postParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { postSupplementaryAssetForm } from '../../../../../modules/cached-resour
import I18n from '../../../../../modules/i18n';
import { theme } from '../../../../../modules/theme';
import { isEmpty } from '../../../../../modules/utils';
import { addSelectTextInputs } from '../../../Forms/SupplementaryForm/utils';
import { addSelectTextInputs, cleanLoopSubmissions } from '../../../Forms/SupplementaryForm/utils';
import surveyingUserFailsafe from '../../../Forms/utils';
import SelectedAsset from '../../ViewAssets/SelectedAsset';
import AssetFormSelect from './AssetFormSelect';
Expand Down Expand Up @@ -47,8 +47,9 @@ const AssetSupplementary = ({
const surveyingUserFailSafe = await surveyingUserFailsafe(user, surveyingUser, isEmpty);
const appVersion = await getData('appVersion') || '';

const formObjectUpdated = addSelectTextInputs(values, formObject);

let formObjectUpdated = addSelectTextInputs(values, formObject);
formObjectUpdated = cleanLoopSubmissions(values, formObjectUpdated);

const postParams = {
parseParentClassID: selectedAsset.objectId,
parseParentClass: 'Assets',
Expand Down
23 changes: 14 additions & 9 deletions domains/DataCollection/Forms/SupplementaryForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import surveyingUserFailsafe from '../utils';
import envConfig from './configs/envhealth.config';
import medConfig from './configs/medical-evaluation.config';
import vitalsConfig from './configs/vitals.config';
import { addSelectTextInputs, vitalsBloodPressue } from './utils';
import { addSelectTextInputs, vitalsBloodPressue, cleanLoopSubmissions } from './utils';

const SupplementaryForm = ({
navigation, selectedForm, setSelectedForm, surveyee, surveyingUser, surveyingOrganization,
Expand Down Expand Up @@ -75,6 +75,10 @@ const SupplementaryForm = ({
if (selectedForm === 'vitals') {
formObjectUpdated = vitalsBloodPressue(values, formObjectUpdated);
}

// clean looped form questions
formObjectUpdated = cleanLoopSubmissions(values, formObjectUpdated);

const postParams = {
parseParentClassID: surveyee.objectId,
parseParentClass: 'SurveyData',
Expand Down Expand Up @@ -110,14 +114,15 @@ const SupplementaryForm = ({
}, 1000);
};

postSupplementaryForm(postParams).then(() => {
submitAction();
}, (error) => {
console.log(error); // eslint-disable-line
// perhaps an alert to let the user know there was an error
setSubmitting(false);
setSubmissionError(true);
});
setSubmitting(false);
// postSupplementaryForm(postParams).then(() => {
// submitAction();
// }, (error) => {
// console.log(error); // eslint-disable-line
// // perhaps an alert to let the user know there was an error
// setSubmitting(false);
// setSubmissionError(true);
// });
}}
validationSchema={validationSchema}
// only validate on submit, errors persist after fixing
Expand Down
33 changes: 32 additions & 1 deletion domains/DataCollection/Forms/SupplementaryForm/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,35 @@ function vitalsBloodPressue(values, formObject) {
return newFormObject;
}

export { addSelectTextInputs, vitalsBloodPressue };
// function to concatenate all loopSameForm keys to orginal key in use
function cleanLoopSubmissions(values, formObject) {
const newFormObject = formObject;
const repeatedQuestions = {}
const valuesToPrune = []
Object.entries(values).forEach(([key, val]) => {
if (key.includes('__sameForm')) {
valuesToPrune.push(key)
const actualKey = key.split('__sameForm')[0];
if (Object.prototype.hasOwnProperty.call(repeatedQuestions, actualKey)){
repeatedQuestions[actualKey].push(val)
}
else {
repeatedQuestions[actualKey] = [val];
}
}
})

valuesToPrune.forEach((value) => {
delete newFormObject[value];
})
for (const [key, values] of Object.entries(repeatedQuestions)) {
values.forEach((value) => {
newFormObject[key] = newFormObject[key] + '|' + value;
})
}

return newFormObject;
}


export { addSelectTextInputs, vitalsBloodPressue, cleanLoopSubmissions };

0 comments on commit e642e1a

Please sign in to comment.