Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not filter out properties on inactive pages that also appear on active pages #155

Merged
merged 5 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions lib/js/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/js/helpers.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint": "npm run lint:js && npm run lint:sass",
"lint:js": "eslint --quiet --ext .js --ext .jsx .",
"lint:sass": "sass-lint -c test/config/sass-lint.yaml --verbose",
"test": "BABEL_ENV=test mocha --require test/config/unit-test-setup.js --require test/config/enzyme-setup.jsx --compilers js:babel-core/register ./test/**/*.spec.jsx",
"test": "BABEL_ENV=test mocha --require test/config/unit-test-setup.js --require test/config/enzyme-setup.jsx --compilers js:babel-core/register './test/**/*.spec.js?(x)' ",
"webpack": "webpack"
},
"repository": {
Expand Down
29 changes: 25 additions & 4 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { deepEquals } from '@department-of-veterans-affairs/react-jsonschema-for
import FormPage from './containers/FormPage';
import ReviewPage from './review/ReviewPage';

// An active page is one that will be shown to the user.
// Pages become inactive if they are conditionally shown based
// on answers to previous questions.
export function isActivePage(page, data) {
if (typeof page.depends === 'function') {
return page.depends(data, page.index);
Expand All @@ -21,6 +24,16 @@ export function getActivePages(pages, data) {
return pages.filter(page => isActivePage(page, data));
}

export function getActiveProperties(activePages) {
const allProperties = [];
activePages.forEach(page => {
if (page.schema) {
allProperties.push(...Object.keys(page.schema.properties));
}
});
return _.uniq(allProperties);
}

export function getInactivePages(pages, data) {
return pages.filter(page => !isActivePage(page, data));
}
Expand Down Expand Up @@ -212,11 +225,18 @@ export function filterViewFields(data) {
}, {});
}

export function filterInactivePages(pages, form) {
return pages.reduce((formData, page) => {
export function filterInactivePageData(inactivePages, activePages, form) {
const activeProperties = getActiveProperties(activePages);
let newData;

return inactivePages.reduce((formData, page) => {
return Object.keys(page.schema.properties)
.reduce((currentData, prop) => {
return _.unset(prop, currentData);
newData = currentData;
if (!activeProperties.includes(prop)) {
delete newData[prop];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on this, my brain wasn't working!

}
return newData;
}, formData);
}, form.data);
}
Expand Down Expand Up @@ -270,8 +290,9 @@ export function isInProgress(pathName) {
* Normal transform for schemaform data
*/
export function transformForSubmit(formConfig, form, replacer = stringifyFormReplacer) {
const activePages = getActivePages(createFormPageList(formConfig), form.data);
const inactivePages = getInactivePages(createFormPageList(formConfig), form.data);
const withoutInactivePages = filterInactivePages(inactivePages, form);
const withoutInactivePages = filterInactivePageData(inactivePages, activePages, form);
const withoutViewFields = filterViewFields(withoutInactivePages);

return JSON.stringify(withoutViewFields, replacer) || '{}';
Expand Down
61 changes: 60 additions & 1 deletion test/js/helpers.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ describe('Schemaform helpers:', () => {
},
chapter2: {
pages: {
page2: {}
page2: {
schema: {
type: 'object',
properties: {}
}
}
}
}
}
Expand All @@ -366,6 +371,60 @@ describe('Schemaform helpers:', () => {
field: 'testing'
});
});
it('should not remove properties that are on both active and inactive pages', () => {
const formConfig = {
chapters: {
chapter1: {
pages: {
page1: {
schema: {
type: 'object',
properties: {
otherField: {
type: 'string'
},
anotherField: {
type: 'string'
}
}
},
depends: {
field: 'something'
}
},
}
},
chapter2: {
pages: {
page2: {
schema: {
type: 'object',
properties: {
anotherField: {
type: 'string'
}
}
}
}
}
}
}
};
const formData = {
data: {
otherField: 'testing2',
anotherField: 'testing3',
field: 'testing'
}
};

const output = JSON.parse(transformForSubmit(formConfig, formData));

expect(output).to.eql({
field: 'testing',
anotherField: 'testing3'
});
});
it('should remove empty addresses', () => {
const formConfig = {
chapters: {
Expand Down