Skip to content

Commit

Permalink
Add hidden columns selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlanciaux committed Mar 25, 2016
1 parent d4cd138 commit e2abecb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions js/initialStates/local-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default Immutable.fromJS({
pageSize: 10,
currentPage: 1,
},
metadataColumns: [],
filter: '',
sortColumns: [],
sortDirections: []
Expand Down
7 changes: 6 additions & 1 deletion js/reducers/__tests__/localReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import LocalReducer, {

import extend from 'lodash.assign';

const initialState = {renderProperties: {columnProperties: null}};
const initialState = {
renderProperties: {columnProperties: null},
sortDirections: [],
sortColumns: []
};

//TODO: Import the testHelpers instead of using this directly
const getMethod = (options) => {
if(!options.method) {
Expand Down
32 changes: 30 additions & 2 deletions js/selectors/__tests__/localSelectorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function getBasicState() {
pageSize: 1,
currentPage: 0,
maxPage: 2
}
},
sortColumns: [],
sortDirections: []
});
}

Expand All @@ -44,7 +46,9 @@ export function get3ColState() {
pageSize: 1,
currentPage: 0,
maxPage: 2
}
},
sortColumns: [],
sortDirections: []
});
}

Expand Down Expand Up @@ -137,6 +141,30 @@ describe('localSelectors', () => {
})
})

describe ('with metadata columns', () => {
it('gets metadata columns', () => {
const state = get3ColState().set('metadataColumns', Immutable.List(['two']));
const metaDataColumns = selectors.metaDataColumnsSelector(state);
expect(metaDataColumns.toJSON()).toEqual(['two'])
})
})

describe ('hidden columns', () => {
it('gets hidden columns without visible / metadata columns', () => {
const state = withRenderProperties(get3ColState()
.set('metadataColumns', Immutable.List(['two'])));

const hidden = selectors.hiddenColumnsSelector(state)
expect(hidden).toEqual(['three'])
});

it('returns empty when none', () => {
const state = withRenderProperties(getBasicState())
const hidden = selectors.hiddenColumnsSelector(state)
expect(hidden).toEqual([]);
});
})

describe('visible columns', () => {
it('gets the renderProperties', () => {
const state = withRenderProperties(getBasicState());
Expand Down
32 changes: 29 additions & 3 deletions js/selectors/localSelectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Immutable from 'immutable';
import MAX_SAFE_INTEGER from 'max-safe-integer';
import { createSelector } from 'reselect';
import { getVisibleDataColumns, getMetaData } from '../utils/dataUtils'
import { getVisibleDataColumns, getDataForColumns } from '../utils/dataUtils'

//oy - not a fan -- refactor asap because this is no good
let localUtils = null;
Expand Down Expand Up @@ -53,6 +53,9 @@ export const allColumnsSelector = createSelector(
(data) => (data.size === 0 ? [] : data.get(0).keySeq().toJSON())
)

//gets the metadata columns or nothing
export const metaDataColumnsSelector = state => (state.get('metadataColumns') || [])

//gets the column property objects ordered by order
export const sortedColumnPropertiesSelector = createSelector(
renderPropertiesSelector,
Expand Down Expand Up @@ -129,11 +132,34 @@ export const visibleDataSelector = createSelector(
(currentPageData, visibleColumns) => getVisibleDataColumns(currentPageData, visibleColumns)
)

export const hiddenColumnsSelector = createSelector(
visibleColumnsSelector,
allColumnsSelector,
metaDataColumnsSelector,
(visibleColumns, allColumns, metaDataColumns) => {
const removeColumns = [...visibleColumns, ...metaDataColumns];

return allColumns.filter(c => removeColumns.indexOf(c) === -1);
}
)

//TODO: this needs some tests
export const metaDataSelector = createSelector(
export const hiddenDataSelector = createSelector(
currentPageDataSelector,
visibleColumnsSelector,
(currentPageData, visibleColumns) => getMetaData(currentPageData, visibleColumns)
allColumnsSelector,
metaDataColumnsSelector,
(currentPageData, visibleColumns, allColumns, metaDataColumns) => {
debugger;
return getDataForColumns(currentPageData, keys)
}
)

//TODO: this needs some tests
export const metaDataSelector = createSelector(
currentPageDataSelector,
metaDataColumnsSelector,
(currentPageData, metaDataColumns) => { return getDataForColumns(currentPageData, metaDataColumns) }
)

//TODO: This NEEDS tests
Expand Down
7 changes: 3 additions & 4 deletions js/utils/dataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ export function addKeyToRows(data) {
return data.map(row => row.set('griddleKey', getKey()));
}

export function getMetaData(data, nonMetaColumns) {
export function getDataForColumns(data, columns) {
if (data.size < 1) {
return data;
}

const dataColumns = data.get(0).keySeq().toArray();

const columns = dataColumns.filter(item => nonMetaColumns.indexOf(item) < 0);
return data.map(d => d.filter(keyInArray(columns)));
const resultColumns = dataColumns.filter(item => columns.indexOf(item) >= 0);
return data.map(d => d.filter(keyInArray(resultColumns)));
}

//, {__metadata: d.filter(keyInArray(metadataColumns)).set('index', i)}
Expand Down

0 comments on commit e2abecb

Please sign in to comment.