Commit 41240111 authored by Matija Obreza's avatar Matija Obreza
Browse files

QS not updated with filter code when received, call filterCodeToUrl action instead

parent 755acf52
......@@ -9,14 +9,30 @@ const addFilter = (code: string, filterJson: string) => ({
});
export const addFilterCode = (code: string, receivedFilter: object) => (dispatch, getState) => {
// console.log('Received filter code', code, receivedFilter);
if (!code) {
return;
code = '';
}
const receivedFilterJson = cleanFilters(receivedFilter, ['published']);
const receivedFilterJson = cleanFilters(receivedFilter, []);
// console.log('Adding filter code', code, receivedFilterJson);
dispatch(addFilter(code, receivedFilterJson));
};
export const filterCodeToUrl = (code: string) => (dispatch) => {
const qs = window && QS.parse(window.location.search) || {};
qs.filter = code;
dispatch(navigateTo('', qs));
// console.log(`Should we update filter code ${code}`, qs);
code = !code ? '' : code;
if ((! qs.filter && ! code) || qs.filter === code) {
// noop
// console.log(`Not updating URL code`, qs);
} else {
if (! code) {
delete qs.filter;
} else {
qs.filter = code;
}
// console.log(`Updating URL code ${code}`, qs);
dispatch(navigateTo('', qs));
}
};
import * as update from 'immutability-helper';
import {ADD_FILTER_CODE} from 'constants/filterCode';
import * as _ from 'lodash';
const INITIAL_STATE: {
filters: Map<string, object>;
filters: object,
} = {
filters: new Map<string, object>(),
filters: {},
};
export default function filterCode(state = INITIAL_STATE, action: { type: string, payload?: any } = { type: '' }) {
......@@ -14,10 +13,8 @@ export default function filterCode(state = INITIAL_STATE, action: { type: string
// Add filter
case ADD_FILTER_CODE: {
const map = _.isEmpty(state.filters) ? new Map<string, object>() : new Map<string, object>(state.filters);
map.set(action.payload.code, action.payload.filterJson);
return update(state, {
filters: { $set: map },
filters: { [action.payload.code]: {$set: action.payload.filterJson} },
});
}
......
......@@ -6,6 +6,7 @@ import { withStyles } from 'material-ui/styles';
import {log} from 'utilities/debug';
import { parse } from 'query-string';
import { filterCodeToUrl } from 'actions/filterCode';
import { listCrops } from 'actions/crop';
import { listDatasetsRequest, promiselistDatasets, listDatasetsByCodeRequest } from 'actions/dataset';
......@@ -31,6 +32,7 @@ interface IDatasetsProps extends React.ClassAttributes<any> {
pagination: Pagination<IDatasetFilter>;
paged: Page<Dataset>;
filterCodeToUrl: any;
listDatasetsRequest: any;
listDatasetsByCodeRequest: any;
promiselistDatasets: any;
......@@ -60,16 +62,11 @@ class BrowsePage extends React.Component<IDatasetsProps, any> {
}
public componentWillReceiveProps(nextProps) {
const {listDatasetsRequest, pagination: oldPagination, listDatasetsByCodeRequest} = this.props;
const {pagination} = nextProps;
if (! oldPagination.equals(pagination)) {
log('Paginations differ!', pagination);
if (pagination.filterCode) {
listDatasetsByCodeRequest(pagination.page, pagination.size, pagination.sort, pagination.filterCode, pagination.dir);
} else {
listDatasetsRequest(pagination.page, pagination.size, pagination.sort, pagination.filter, pagination.dir);
}
const {filterCodeToUrl} = this.props;
const {paged} = nextProps;
if (paged) {
filterCodeToUrl(paged.filterCode);
}
}
......@@ -174,7 +171,7 @@ const mapStateToProps = (state, ownProps) => ({
size: +parse(ownProps.location.search).l || 50, // page size
sort: parse(ownProps.location.search).s, // page sorts
dir: parse(ownProps.location.search).d, // page sort directions
filter: state.datasets.pagedQuery && parse(ownProps.location.search).filter && state.datasets.pagedQuery.filter || null,
filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter] || null,
filterCode: parse(ownProps.location.search).filter,
}),
paged: state.datasets.paged,
......@@ -185,6 +182,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
promiselistDatasets,
listDatasetsByCodeRequest,
listCrops,
filterCodeToUrl,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(BrowsePage));
......@@ -6,6 +6,7 @@ import { withStyles } from 'material-ui/styles';
import {log} from 'utilities/debug';
import { parse } from 'query-string';
import { filterCodeToUrl } from 'actions/filterCode';
import { loadDescriptors, promiseLoadDescriptors, loadDescriptorsByCode } from 'actions/descriptors';
import { listCrops } from 'actions/crop';
import { Descriptor, IDescriptorFilter } from 'model/descriptor.model';
......@@ -27,6 +28,7 @@ interface IDescriptorListsPageProps extends React.ClassAttributes<any> {
loadDescriptors: (page?: number, results?: number, sortBy?: string, filter?: IDescriptorFilter) => void;
loadDescriptorsByCode: (page?: number, results?: number, sortBy?: string, filterCode?: string) => void;
listCrops: () => any;
filterCodeToUrl: any;
loading: any;
}
......@@ -60,17 +62,12 @@ class BrowsePage extends React.Component<IDescriptorListsPageProps & any, any> {
}
public componentWillReceiveProps(nextProps) {
const {loadDescriptors, pagination: oldPagination, loadDescriptorsByCode} = this.props;
const {pagination} = nextProps;
const {filterCodeToUrl} = this.props;
const {paged} = nextProps;
if (! oldPagination.equals(pagination)) {
log('Paginations differ!', oldPagination, pagination);
if (pagination.filterCode) {
loadDescriptorsByCode(pagination.page, pagination.size, pagination.sort, pagination.filterCode, pagination.dir);
} else {
loadDescriptors(pagination.page, pagination.size, pagination.sort, {}, pagination.dir);
}
}
if (paged) {
filterCodeToUrl(paged.filterCode);
}
}
protected onPaginationChange = (page, results, sortBy, dir) => {
......@@ -172,7 +169,7 @@ const mapStateToProps = (state, ownProps) => ({
size: +parse(ownProps.location.search).l || 50, // page size
sort: parse(ownProps.location.search).s, // page sorts
dir: parse(ownProps.location.search).d, // page sort directions
filter: state.descriptors.pagedQuery && parse(ownProps.location.search).filter && state.descriptors.pagedQuery.filter || null,
filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter] || null,
filterCode: parse(ownProps.location.search).filter,
}),
paged: state.descriptors.paged,
......@@ -184,6 +181,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
promiseLoadDescriptors,
loadDescriptorsByCode,
listCrops,
filterCodeToUrl,
}, dispatch);
export default connect(
......
......@@ -5,6 +5,7 @@ import { bindActionCreators } from 'redux';
import {log} from 'utilities/debug';
import { parse } from 'query-string';
import { filterCodeToUrl } from 'actions/filterCode';
import { listDescriptorLists, promiseListDescriptorLists, listDescriptorListsByCode } from 'actions/descriptorList';
import { listCrops } from 'actions/crop';
......@@ -34,6 +35,7 @@ interface IBrowsePageProps extends React.ClassAttributes<any> {
listDescriptorListsByCode: any;
promiseListDescriptorLists: any;
listCrops: () => any;
filterCodeToUrl: any;
crop: any;
history: any;
location: any;
......@@ -63,16 +65,11 @@ class BrowsePage extends React.Component<IBrowsePageProps, any> {
}
public componentWillReceiveProps(nextProps) {
const {pagination: oldPagination, listDescriptorListsByCode, listDescriptorLists} = this.props;
const {pagination} = nextProps;
if (! oldPagination.equals(pagination)) {
log('Paginations differ!', oldPagination, pagination);
if (pagination.filterCode) {
listDescriptorListsByCode(pagination.page, pagination.size, pagination.sort, pagination.filterCode, pagination.dir);
} else {
listDescriptorLists(pagination.page, pagination.size, pagination.sort, {}, pagination.dir);
}
const {filterCodeToUrl} = this.props;
const {paged} = nextProps;
if (paged) {
filterCodeToUrl(paged.filterCode);
}
}
......@@ -178,7 +175,7 @@ const mapStateToProps = (state, ownProps) => ({
size: +parse(ownProps.location.search).l || 50, // page size
sort: parse(ownProps.location.search).s, // page sorts
dir: parse(ownProps.location.search).d, // page sort directions
filter: state.descriptorList.pagedQuery && parse(ownProps.location.search).filter && state.descriptorList.pagedQuery.filter || null,
filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter] || null,
filterCode: parse(ownProps.location.search).filter,
}),
paged: state.descriptorList.paged,
......@@ -190,6 +187,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
listCrops,
promiseListDescriptorLists,
listDescriptorListsByCode,
filterCodeToUrl,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(BrowsePage));
......@@ -6,6 +6,7 @@ import { withStyles } from 'material-ui/styles';
import {log} from 'utilities/debug';
import { parse } from 'query-string';
import { filterCodeToUrl } from 'actions/filterCode';
import { Page, Pagination } from 'model/common.model';
import { Partner } from 'model/partner.model';
import { IPartnerFilter } from 'model/filter.model';
......@@ -33,6 +34,7 @@ interface IBrowsePageProps extends React.ClassAttributes<any> {
loadPartners: any;
promiseLoadPartners: any;
loadPartnersByCode: any;
filterCodeToUrl: any;
history: any;
location: any;
}
......@@ -62,16 +64,11 @@ class PartnerListPage extends React.Component<IBrowsePageProps, any> {
}
public componentWillReceiveProps(nextProps) {
const {loadPartners, pagination: oldPagination, loadPartnersByCode} = this.props;
const {pagination} = nextProps;
const {filterCodeToUrl} = this.props;
const {paged} = nextProps;
if (! oldPagination.equals(pagination)) {
log('Paginations differ!', pagination);
if (pagination.filterCode) {
loadPartnersByCode(pagination.page, pagination.size, pagination.sort, pagination.filterCode, pagination.dir);
} else {
loadPartners(pagination.page, pagination.size, pagination.sort, {}, pagination.dir);
}
if (paged) {
filterCodeToUrl(paged.filterCode);
}
}
......@@ -183,7 +180,7 @@ const mapStateToProps = (state, ownProps) => ({
size: +parse(ownProps.location.search).l || 50, // page size
sort: parse(ownProps.location.search).s, // page sorts
dir: parse(ownProps.location.search).d, // page sort directions
filter: state.partner.pagedQuery && parse(ownProps.location.search).filter && state.partner.pagedQuery.filter || null,
filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter] || null,
filterCode: parse(ownProps.location.search).filter,
}),
paged: state.partner.paged,
......@@ -194,6 +191,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
loadPartners,
promiseLoadPartners,
loadPartnersByCode,
filterCodeToUrl,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(PartnerListPage));
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment