Skip to content

Commit

Permalink
Select filter with default value (#42)
Browse files Browse the repository at this point in the history
* Add defaultValue to movie api

* Add default value to filter
Set default value on reset and adapt hasActiveFilter logic to check for default value
Send defaultValue also as value with api call to set initial value
    #32

* Set default filter value on loadTrees (#32)

* Hide clear button if default (#32)
  • Loading branch information
nstrelow committed Feb 4, 2018
1 parent f2be469 commit a835954
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions frontend/app/api/concepts.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"label": "Picture Format",
"type": "SELECT",
"description": "The original picture format",
"defaultValue": "hdtv",
"options": [
{
"value": "sdtv",
Expand Down
8 changes: 8 additions & 0 deletions frontend/lib/js/category-trees/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export const loadTrees = (datasetId: DatasetIdType) => {
r => {
dispatch(loadTreesSuccess(r));

// Assign default select filter values
Object.values(r.concepts).forEach(concept => concept.tables.forEach(table =>
table.filters.forEach(filter => {
if (filter.defaultValue)
filter.value = filter.defaultValue;
})
));

// In the future: Data could be cached, version could be checked and
// further data only loaded when necessary
if (r.version > -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const findActiveFilters = (concept) => (
concept.tables.some(table =>
table.exclude || (table.filters && table.filters.some(filter =>
// multi select filters create an array that is not nulled when empty
filter.value && Object.keys(filter.value).length > 0
filter.value && filter.value !== filter.defaultValue && Object.keys(filter.value).length > 0
))
)
);
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/js/form-components/InputSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const InputSelect = (props: PropsType) => {
? props.input.onChange(field.value)
: props.input.onChange(null)
}
clearable={props.input.clearable}
disabled={!!props.disabled}
placeholder={T.translate('reactSelect.placeholder')}
backspaceToRemoveMessage={T.translate('reactSelect.backspaceToRemove')}
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/js/query-node-modal/ParameterTableFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ParameterTableFilters = (props: PropsType) => (
return (
<InputSelect
input={{
clearable: filter.value !== filter.defaultValue,
value: filter.value,
onChange: (value) => props.onSetFilterValue(filterIdx, value),
}}
Expand Down
15 changes: 9 additions & 6 deletions frontend/lib/js/standard-query-editor/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const setElementProperties = (node, andIdx, orIdx, properties) => {
};

return setGroupProperties(node, andIdx, groupProperties);
}
};

const setAllElementsProperties = (node, properties) => {
return node.map(group => ({
Expand All @@ -120,11 +120,11 @@ const setAllElementsProperties = (node, properties) => {
...properties
}))
}));
}
};

const nodeHasActiveFilters = (node, tables = node.tables) => {
return node.excludeTimestamps || nodeHasActiveTableFilters(tables);
}
};

const dropAndNode = (state, action) => {
const group = state[state.length - 1];
Expand Down Expand Up @@ -220,7 +220,7 @@ const nodeHasActiveTableFilters = (tables) => {
const hasTableValue = tables
.some(table =>
table.filters &&
table.filters.some(filter => !isEmpty(filter.value))
table.filters.some(filter => !isEmpty(filter.value) && filter.value !== filter.defaultValue)
);

const hasExcludedTable = tables.some(table => table.exclude);
Expand All @@ -245,7 +245,7 @@ const updateNodeTables = (state, andIdx, orIdx, tables) => {
const properties = {
tables,
hasActiveFilters: nodeHasActiveFilters(node, tables)
}
};

return setElementProperties(state, andIdx, orIdx, properties);
};
Expand Down Expand Up @@ -294,6 +294,9 @@ const setNodeFilterProperties = (state, action, obj) => {
);
}

if (properties.value === undefined && filter.defaultValue)
properties.value = filter.defaultValue;

const newTable = {
...table,
filters: [
Expand Down Expand Up @@ -342,7 +345,7 @@ const resetNodeAllFilters = (state, action) => {
// $FlowFixMe
? table.filters.map((filter) => ({
...filter,
value: null
value: filter.defaultValue
}))
: null;

Expand Down

0 comments on commit a835954

Please sign in to comment.