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

[RFR] Fix default filter values cannot be removed by user #2831

Merged
merged 3 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 29 additions & 6 deletions cypress/integration/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('List Page', () => {
cy.viewport(1280, 500);

cy.scrollTo(0, 200);
cy.get(ListPagePosts.elements.headroomUnpinned).should('not.be.visible');
cy.get(ListPagePosts.elements.headroomUnpinned).should(
'not.be.visible'
);

cy.scrollTo(0, -100);
cy.get(ListPagePosts.elements.headroomUnfixed).should('be.visible');
Expand Down Expand Up @@ -109,6 +111,15 @@ describe('List Page', () => {
cy.contains('1-1 of 1');
ListPagePosts.setFilterValue('q', '');
});

it('should allow to disable alwaysOn filters with default value', () => {
LoginPage.navigate();
LoginPage.login('admin', 'password');
ListPageUsers.navigate();
cy.contains('1-2 of 2');
cy.get('button[tooltip="Remove this filter"]').click();
cy.contains('1-3 of 3');
});
});

describe('Bulk Actions', () => {
Expand Down Expand Up @@ -223,13 +234,15 @@ describe('List Page', () => {
});
});

describe("Sorting", () => {
describe('Sorting', () => {
it('should display a sort arrow when clicking on a sortable column header', () => {
ListPagePosts.toggleColumnSort('id');
cy.get(ListPagePosts.elements.svg('id')).should('be.visible');

ListPagePosts.toggleColumnSort('tags.name');
cy.get(ListPagePosts.elements.svg('tags.name')).should('be.visible');
cy.get(ListPagePosts.elements.svg('tags.name')).should(
'be.visible'
);
});

it('should hide the sort arrow when clicking on another sortable column header', () => {
Expand All @@ -241,10 +254,20 @@ describe('List Page', () => {
it('should reverse the sort arrow when clicking on an already sorted column header', () => {
ListPagePosts.toggleColumnSort('published_at');
ListPagePosts.toggleColumnSort('tags.name');
cy.get(ListPagePosts.elements.svg('tags.name', '[class*=iconDirectionAsc]')).should('exist');
cy.get(
ListPagePosts.elements.svg(
'tags.name',
'[class*=iconDirectionAsc]'
)
).should('exist');

ListPagePosts.toggleColumnSort('tags.name');
cy.get(ListPagePosts.elements.svg('tags.name', '[class*=iconDirectionDesc]')).should('exist');
cy.get(
ListPagePosts.elements.svg(
'tags.name',
'[class*=iconDirectionDesc]'
)
).should('exist');
});
})
});
});
57 changes: 36 additions & 21 deletions packages/ra-core/src/controller/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ export class ListController extends Component {
return true;
}

/**
* Check if user has already set custom sort, page, or filters for this list
*
* User params come from the Redux store as the params props. By default,
* this object is:
*
* { filter: {}, order: null, page: 1, perPage: null, sort: null }
*
* To check if the user has custom params, we must compare the params
* to these initial values.
*
* @param {object} params
*/
hasCustomParams(params) {
return (
Object.keys(params.filter).length > 0 ||
params.order != null ||
params.page !== 1 ||
params.perPage != null ||
params.sort != null
);
}

/**
* Merge list params from 4 different sources:
* - the query string
Expand All @@ -148,10 +171,9 @@ export class ListController extends Component {
const query =
Object.keys(this.props.query).length > 0
? this.props.query
: { ...this.props.params };
const filterDefaultValues = this.props.filterDefaultValues || {};

query.filter = { ...filterDefaultValues, ...query.filter };
: this.hasCustomParams(this.props.params)
? { ...this.props.params }
: { filter: this.props.filterDefaultValues || {} };

if (!query.sort) {
query.sort = this.props.sort.field;
Expand All @@ -166,6 +188,11 @@ export class ListController extends Component {
return query;
}

getFilterValues() {
const query = this.getQuery();
return query.filter || {};
}

updateData(query) {
const params = query || this.getQuery();
const { sort, order, page = 1, perPage, filter } = params;
Expand All @@ -190,7 +217,7 @@ export class ListController extends Component {
this.changeParams({ type: SET_PER_PAGE, payload: perPage });

setFilters = debounce(filters => {
if (isEqual(filters, this.props.filterValues)) {
if (isEqual(filters, this.getFilterValues())) {
return;
}

Expand All @@ -203,15 +230,15 @@ export class ListController extends Component {
this.setState({ [filterName]: true });
if (typeof defaultValue !== 'undefined') {
this.setFilters({
...this.props.filterValues,
...this.getFilterValues(),
[filterName]: defaultValue,
});
}
};

hideFilter = filterName => {
this.setState({ [filterName]: false });
const newFilters = removeKey(this.props.filterValues, filterName);
const newFilters = removeKey(this.getFilterValues(), filterName);
this.setFilters(newFilters);
};

Expand Down Expand Up @@ -255,9 +282,6 @@ export class ListController extends Component {
selectedIds,
} = this.props;
const query = this.getQuery();

const queryFilterValues = query.filter || {};

const resourceName = translate(`resources.${resource}.name`, {
smart_count: 2,
_: inflection.humanize(inflection.pluralize(resource)),
Expand All @@ -275,7 +299,7 @@ export class ListController extends Component {
data,
defaultTitle,
displayedFilters: this.state,
filterValues: queryFilterValues,
filterValues: this.getFilterValues(),
hasCreate,
hideFilter: this.hideFilter,
ids,
Expand Down Expand Up @@ -320,7 +344,6 @@ ListController.propTypes = {
crudGetList: PropTypes.func.isRequired,
data: PropTypes.object, // eslint-disable-line react/forbid-prop-types
debounce: PropTypes.number,
filterValues: PropTypes.object, // eslint-disable-line react/forbid-prop-types
hasCreate: PropTypes.bool,
hasEdit: PropTypes.bool,
hasList: PropTypes.bool,
Expand All @@ -345,7 +368,6 @@ ListController.propTypes = {
ListController.defaultProps = {
debounce: 500,
filter: {},
filterValues: {},
perPage: 10,
sort: {
field: 'id',
Expand Down Expand Up @@ -435,19 +457,12 @@ function mapStateToProps(state, props) {
total: resourceState.list.total,
data: resourceState.data,
isLoading: state.admin.loading > 0,
filterValues: resourceState.list.params.filter,
version: state.admin.ui.viewVersion,
};
}



export default compose(
checkMinimumRequiredProps('List', [
'basePath',
'location',
'resource',
]),
checkMinimumRequiredProps('List', ['basePath', 'location', 'resource']),
connect(
mapStateToProps,
{
Expand Down