Skip to content

Commit

Permalink
Add helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalboni committed Sep 14, 2018
1 parent 56c8743 commit 31fd754
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 33 deletions.
23 changes: 19 additions & 4 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.

3 changes: 1 addition & 2 deletions lib/js/review/ReviewChapters.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/review/ReviewChapters.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/js/review/SubmitController.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/review/SubmitController.js.map

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions lib/js/routing.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/routing.js.map

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

23 changes: 19 additions & 4 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ export function expandArrayPages(pageList, data) {
return result.currentList;
}

/**
* Gets active and expanded pages, in the correct order
*
* Any `showPagePerItem` pages are expanded to create items for each array item.
* We update the `path` for each of those pages to replace `:index` with the current item index.
*
* @param pages {Array<Object>} List of page configs
* @param data {Object} Current form data
* @returns {Array<Object>} A list of pages, includeing individual array
* pages that are active
*/
export function getActiveExpandedPages(pages, data) {
const expandedPages = expandArrayPages(pages, data);
return getActivePages(expandedPages, data);
}

/**
* getPageKeys returns a list of keys for the currently active pages
*
Expand All @@ -532,8 +548,7 @@ export function expandArrayPages(pageList, data) {
* and the index if it’s a pagePerItem page
*/
export function getPageKeys(pages, formData) {
const eligiblePageList = getActivePages(pages, formData);
const expandedPageList = expandArrayPages(eligiblePageList, formData);
const expandedPageList = getActiveExpandedPages(pages, formData);

return expandedPageList.map(page => {
let pageKey = page.pageKey;
Expand All @@ -554,8 +569,7 @@ export function getPageKeys(pages, formData) {
export function getActiveChapters(formConfig, formData) {
const formPages = createFormPageList(formConfig);
const pageList = createPageList(formConfig, formPages);
const eligiblePageList = getActivePages(pageList, formData);
const expandedPageList = expandArrayPages(eligiblePageList, formData);
const expandedPageList = getActiveExpandedPages(pageList, formData);

return _.uniq(expandedPageList.map(p => p.chapterKey).filter(key => !!key && key !== 'review'));
}
Expand All @@ -578,3 +592,4 @@ export function omitRequired(schema) {

return newSchema;
}

6 changes: 2 additions & 4 deletions src/js/review/ReviewChapters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import Scroll from 'react-scroll';
import ReviewCollapsibleChapter from './ReviewCollapsibleChapter';
import {
createPageListByChapter,
expandArrayPages,
getActiveExpandedPages,
getActiveChapters,
getActivePages,
getPageKeys
} from '../helpers';
import {
Expand Down Expand Up @@ -124,8 +123,7 @@ export function mapStateToProps(state, ownProps) {
const chapters = chapterNames.map(chapterName => {
const pages = pagesByChapter[chapterName];

let expandedPages = expandArrayPages(pages, formData);
expandedPages = getActivePages(expandedPages, formData);
const expandedPages = getActiveExpandedPages(pages, formData);
const chapterFormConfig = formConfig.chapters[chapterName];
const open = openChapters.includes(chapterName);
const pageKeys = getPageKeys(pages, formData);
Expand Down
6 changes: 2 additions & 4 deletions src/js/review/SubmitController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { PreSubmitSection } from '../components/PreSubmitSection';
import { isValidForm } from '../validation';
import {
createPageListByChapter,
expandArrayPages,
getActivePages
getActiveExpandedPages
} from '../helpers';
import {
setPreSubmit,
Expand Down Expand Up @@ -45,8 +44,7 @@ class SubmitController extends React.Component {
router
} = this.props;

const eligiblePageList = getActivePages(pageList, form.data);
const expandedPageList = expandArrayPages(eligiblePageList, this.props.form.data);
const expandedPageList = getActiveExpandedPages(pageList, form.data);

// TODO: Fix this bug that assumes there is a confirmation page.
// Actually, it assumes the app also doesn't add routes at the end!
Expand Down
7 changes: 2 additions & 5 deletions src/js/routing.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import _ from 'lodash/fp';
import { expandArrayPages, getActivePages } from './helpers';
import { getActiveExpandedPages } from './helpers';

/*
* Returns the page list without conditional pages that have not satisfied
* their dependencies and therefore should be skipped.
*/
function getEligiblePages(pageList, data, pathname) {
// Any `showPagePerItem` pages are expanded to create items for each array item.
// We update the `path` for each of those pages to replace `:index` with the current item index.
const expandedPageList = expandArrayPages(pageList, data);
const eligiblePageList = getActivePages(expandedPageList, data);
const eligiblePageList = getActiveExpandedPages(pageList, data);
const pageIndex = _.findIndex(item => item.path === pathname, eligiblePageList);
return { pages: eligiblePageList, pageIndex };
}
Expand Down

0 comments on commit 31fd754

Please sign in to comment.