Skip to content

Commit

Permalink
Implement URLParam in all the sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan687 committed May 10, 2017
1 parent d628003 commit fecce0f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 76 deletions.
17 changes: 11 additions & 6 deletions app/sensors/CategorySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class CategorySearch extends Component {
this.type = "match_phrase";
this.channelId = null;
this.channelListener = null;
this.urlParams = helper.URLParams.get(this.props.componentId);
this.fieldType = typeof props.appbaseField;
this.handleSearch = this.handleSearch.bind(this);
this.optionRenderer = this.optionRenderer.bind(this);
Expand Down Expand Up @@ -268,8 +269,9 @@ export default class CategorySearch extends Component {
}

checkDefault() {
if (this.props.defaultSelected && this.defaultSelected !== this.props.defaultSelected) {
this.defaultSelected = this.props.defaultSelected;
const defaultValue = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
if (defaultValue && this.defaultSelected !== defaultValue) {
this.defaultSelected = defaultValue;
setTimeout(this.setValue.bind(this, this.defaultSelected), 100);
this.handleSearch({
value: this.defaultSelected
Expand Down Expand Up @@ -297,7 +299,7 @@ export default class CategorySearch extends Component {
if(this.props.onValueChange) {
this.props.onValueChange(obj.value);
}

helper.URLParams.update(this.props.componentId, finalVal.value, this.props.URLParam);
helper.selectedSensor.set(obj, true);
this.setState({
currentValue: value
Expand Down Expand Up @@ -363,14 +365,16 @@ CategorySearch.propTypes = {
React.PropTypes.string,
React.PropTypes.arrayOf(React.PropTypes.string)
]),
componentStyle: React.PropTypes.object
componentStyle: React.PropTypes.object,
URLParam: React.PropTypes.bool
};

// Default props value
CategorySearch.defaultProps = {
placeholder: "Search",
highlight: false,
componentStyle: {}
componentStyle: {},
URLParam: false
};

// context type
Expand All @@ -388,5 +392,6 @@ CategorySearch.types = {
placeholder: TYPES.STRING,
defaultSelected: TYPES.STRING,
customQuery: TYPES.FUNCTION,
highlight: TYPES.BOOLEAN
highlight: TYPES.BOOLEAN,
URLParam: TYPES.BOOLEAN
};
41 changes: 29 additions & 12 deletions app/sensors/DynamicRangeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class DynamicRangeSlider extends Component {
this.type = "range";
this.channelId = null;
this.channelListener = null;
this.urlParams = helper.URLParams.get(this.props.componentId, false, true);
this.handleValuesChange = this.handleValuesChange.bind(this);
this.handleResults = this.handleResults.bind(this);
this.customQuery = this.customQuery.bind(this);
Expand All @@ -47,7 +48,8 @@ export default class DynamicRangeSlider extends Component {
}

componentWillReceiveProps(nextProps) {
this.updateValues(nextProps.defaultSelected);
const defaultValue = this.urlParams !== null ? this.urlParams : nextProps.defaultSelected;
this.updateValues(defaultValue);
}

// stop streaming request and remove listener when component will unmount
Expand Down Expand Up @@ -247,21 +249,23 @@ export default class DynamicRangeSlider extends Component {
this.handleResults(null, { min, max });
});
}
this.updateValues(this.props.defaultSelected);
const defaultValue = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
this.updateValues(defaultValue);
}

updateValues(defaultSelected) {
if (defaultSelected) {
const { min, max } = this.state.range;
const { start, end } = defaultSelected(min, max);
const { start, end } = this.urlParams !== null ? this.urlParams : defaultSelected(min, max);

if (start >= min && end <= max) {
const values = {
min: start,
max: end
};
this.setState({
values: {
min: start,
max: end
}
});
values
}, this.handleResults.bind(this, null, values));
} else {
console.error(`defaultSelected values must lie between ${min} and ${max}`);
}
Expand Down Expand Up @@ -297,14 +301,24 @@ export default class DynamicRangeSlider extends Component {
if(this.props.onValueChange) {
this.props.onValueChange(obj.value);
}

helper.URLParams.update(this.props.componentId, this.setURLParam(obj.value), this.props.URLParam);
helper.selectedSensor.set(obj, true);

this.setState({
values
});
}

setURLParam(value) {
if("from" in value && "to" in value) {
value = {
start: value.from,
end: value.to
};
}
return JSON.stringify(value);
}

render() {
let title = null,
histogram = null,
Expand Down Expand Up @@ -379,14 +393,16 @@ DynamicRangeSlider.propTypes = {
react: React.PropTypes.object,
onValueChange: React.PropTypes.func,
interval: React.PropTypes.number,
componentStyle: React.PropTypes.object
componentStyle: React.PropTypes.object,
URLParam: React.PropTypes.bool
};

DynamicRangeSlider.defaultProps = {
title: null,
stepValue: 1,
showHistogram: true,
componentStyle: {}
componentStyle: {},
URLParam: false
};

// context type
Expand All @@ -405,5 +421,6 @@ DynamicRangeSlider.types = {
stepValue: TYPES.NUMBER,
showHistogram: TYPES.BOOLEAN,
customQuery: TYPES.FUNCTION,
initialLoader: TYPES.OBJECT
initialLoader: TYPES.OBJECT,
URLParam: TYPES.BOOLEAN
};
25 changes: 15 additions & 10 deletions app/sensors/NestedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default class NestedList extends Component {
};
this.channelId = null;
this.channelListener = null;
this.defaultSelected = this.props.defaultSelected;
this.urlParams = helper.URLParams.get(this.props.componentId, true);
this.defaultSelected = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
this.filterBySearch = this.filterBySearch.bind(this);
this.onItemSelect = this.onItemSelect.bind(this);
this.customQuery = this.customQuery.bind(this);
Expand All @@ -50,24 +51,24 @@ export default class NestedList extends Component {
}

componentDidMount() {
if (this.props.defaultSelected) {
this.defaultSelected = this.props.defaultSelected;
if (this.defaultSelected) {
setTimeout(this.handleSelect.bind(this), 100);
}
}

handleSelect() {
if (this.props.defaultSelected) {
this.props.defaultSelected.forEach((value, index) => {
if (this.defaultSelected) {
this.defaultSelected.forEach((value, index) => {
this.onItemSelect(value, index);
});
}
}

componentWillUpdate() {
setTimeout(() => {
if (!_.isEqual(this.defaultSelected, this.props.defaultSelected)) {
this.defaultSelected = this.props.defaultSelected;
const defaultValue = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
if (!_.isEqual(this.defaultSelected, defaultValue)) {
this.defaultSelected = defaultValue;
let items = this.state.items;
items = items.map((item) => {
item.key = item.key.toString();
Expand Down Expand Up @@ -312,6 +313,7 @@ export default class NestedList extends Component {
if(this.props.onValueChange) {
this.props.onValueChange(obj.value);
}
helper.URLParams.update(this.props.componentId, value, this.props.URLParam);
helper.selectedSensor.set(obj, isExecuteQuery);
}

Expand Down Expand Up @@ -481,7 +483,8 @@ NestedList.propTypes = {
]),
react: React.PropTypes.object,
onValueChange: React.PropTypes.func,
componentStyle: React.PropTypes.object
componentStyle: React.PropTypes.object,
URLParam: React.PropTypes.bool
};

// Default props value
Expand All @@ -492,7 +495,8 @@ NestedList.defaultProps = {
showSearch: false,
title: null,
placeholder: "Search",
componentStyle: {}
componentStyle: {},
URLParam: false
};

// context type
Expand All @@ -513,5 +517,6 @@ NestedList.types = {
showSearch: TYPES.BOOLEAN,
defaultSelected: TYPES.ARRAY,
customQuery: TYPES.FUNCTION,
initialLoader: TYPES.OBJECT
initialLoader: TYPES.OBJECT,
URLParam: TYPES.BOOLEAN
};
18 changes: 12 additions & 6 deletions app/sensors/RatingsFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default class RatingsFilter extends Component {
selected: null
};
this.type = "range";
this.defaultSelected = props.defaultSelected;
this.urlParams = helper.URLParams.get(this.props.componentId, false, true);
this.defaultSelected = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
this.handleChange = this.handleChange.bind(this);
this.customQuery = this.customQuery.bind(this);
}
Expand All @@ -32,8 +33,9 @@ export default class RatingsFilter extends Component {

componentWillUpdate() {
setTimeout(() => {
if (this.defaultSelected && this.defaultSelected.start !== this.props.defaultSelected.start) {
this.defaultSelected = this.props.defaultSelected;
const defaultValue = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
if (this.defaultSelected && this.defaultSelected.start !== defaultValue.start) {
this.defaultSelected = defaultValue;
const records = this.props.data.filter(record => (record.start === this.defaultSelected.start &&
record.end === this.defaultSelected.end));
if (records && records.length) {
Expand Down Expand Up @@ -86,6 +88,7 @@ export default class RatingsFilter extends Component {
}
// pass the selected sensor value with componentId as key,
const isExecuteQuery = true;
helper.URLParams.update(this.props.componentId, JSON.stringify(record), this.props.URLParam);
helper.selectedSensor.set(obj, isExecuteQuery);
}

Expand Down Expand Up @@ -156,13 +159,15 @@ RatingsFilter.propTypes = {
defaultSelected: React.PropTypes.object,
customQuery: React.PropTypes.func,
onValueChange: React.PropTypes.func,
componentStyle: React.PropTypes.object
componentStyle: React.PropTypes.object,
URLParam: React.PropTypes.bool
};

// Default props value
RatingsFilter.defaultProps = {
title: null,
componentStyle: {}
componentStyle: {},
URLParam: false
};

// context type
Expand All @@ -177,5 +182,6 @@ RatingsFilter.types = {
title: TYPES.STRING,
data: TYPES.OBJECT,
defaultSelected: TYPES.OBJECT,
customQuery: TYPES.FUNCTION
customQuery: TYPES.FUNCTION,
URLParam: TYPES.BOOLEAN
};
24 changes: 16 additions & 8 deletions app/sensors/TagCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default class TagCloud extends Component {
this.previousSelectedSensor = {};
this.channelId = null;
this.channelListener = null;
this.defaultSelected = props.defaultSelected;
this.urlParams = helper.URLParams.get(this.props.componentId, this.props.multiSelect);
this.defaultSelected = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
this.type = this.props.multiSelect ? "Terms" : "Term";
this.customQuery = this.customQuery.bind(this);
this.defaultCustomQuery = this.defaultCustomQuery.bind(this);
Expand All @@ -45,8 +46,9 @@ export default class TagCloud extends Component {

componentWillUpdate() {
setTimeout(() => {
if (this.props.multiSelect && !_.isEqual(this.defaultSelected, this.props.defaultSelected)) {
this.defaultSelected = this.props.defaultSelected;
const defaultValue = this.urlParams !== null ? this.urlParams : this.props.defaultSelected;
if (this.props.multiSelect && !_.isEqual(this.defaultSelected, defaultValue)) {
this.defaultSelected = defaultValue;
const items = this.state.items.map((item) => {
item.status = ((this.defaultSelected && this.defaultSelected.indexOf(item.key) > -1) || (this.selectedValue && this.selectedValue.indexOf(item.key) > -1));
return item;
Expand All @@ -64,9 +66,10 @@ export default class TagCloud extends Component {
key: this.props.componentId,
value: this.selectedValue
};
helper.URLParams.update(this.props.componentId, obj.value, this.props.URLParam);
helper.selectedSensor.set(obj, true);
} else if (!this.props.multiSelect && this.defaultSelected !== this.props.defaultSelected) {
this.defaultSelected = this.props.defaultSelected;
} else if (!this.props.multiSelect && this.defaultSelected !== defaultValue) {
this.defaultSelected = defaultValue;
const items = this.state.items.map((item) => {
if (this.defaultSelected && this.defaultSelected === item.key) {
item.status = !item.status;
Expand All @@ -88,6 +91,7 @@ export default class TagCloud extends Component {
key: this.props.componentId,
value: this.selectedValue
};
helper.URLParams.update(this.props.componentId, obj.value, this.props.URLParam);
helper.selectedSensor.set(obj, true);
}
}, 300);
Expand Down Expand Up @@ -266,6 +270,7 @@ export default class TagCloud extends Component {
key: this.props.componentId,
value: this.selectedValue
};
helper.URLParams.update(this.props.componentId, obj.value, this.props.URLParam);
helper.selectedSensor.set(obj, true);
}

Expand Down Expand Up @@ -342,15 +347,17 @@ TagCloud.propTypes = {
]),
react: React.PropTypes.object,
onValueChange: React.PropTypes.func,
componentStyle: React.PropTypes.object
componentStyle: React.PropTypes.object,
URLParam: React.PropTypes.bool
};

TagCloud.defaultProps = {
showCount: true,
multiSelect: false,
size: 100,
title: null,
componentStyle: {}
componentStyle: {},
URLParam: false
};

TagCloud.contextTypes = {
Expand All @@ -368,5 +375,6 @@ TagCloud.types = {
customQuery: TYPES.FUNCTION,
initialLoader: TYPES.STRING,
defaultSelected: TYPES.STRING,
react: TYPES.OBJECT
react: TYPES.OBJECT,
URLParam: TYPES.BOOLEAN
};

0 comments on commit fecce0f

Please sign in to comment.