Skip to content

Commit f2713ed

Browse files
authored
[Backport 1.9] COMPASS 1763 Geo query circle does not show at first after query is executed (#1218) (#1220)
* 🎨 Rename usage of <UniqueMinichart> to <UniqueMiniChart> * 🎨 Make QueryStore listener reusable * 🐛 Populate query initially * 🎨 Rename query to queryValue within the <UniqueMinichart> hierarchy Note: I'm skipping over the <D3Component> hierarchy as it's not currently affected. * 🎨 Extract <ValueBubble> into its own component file * 🔨 Rename unique.jsx to unique-minichart.jsx
1 parent 4fb0207 commit f2713ed

File tree

4 files changed

+147
-138
lines changed

4 files changed

+147
-138
lines changed

src/internal-packages/schema/lib/component/minichart.jsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const app = require('hadron-app');
22
const React = require('react');
33
const PropTypes = require('prop-types');
4-
const UniqueMinichart = require('./unique');
4+
const UniqueMiniChart = require('./unique-minichart');
55
const _ = require('lodash');
66
const DocumentMinichart = require('./document');
77
const ArrayMinichart = require('./array');
@@ -33,14 +33,18 @@ class MiniChart extends React.Component {
3333
window.addEventListener('resize', this.resizeListener);
3434

3535
const QueryStore = app.appRegistry.getStore('Query.Store');
36-
this.unsubscribeQueryStore = QueryStore.listen((store) => {
36+
const onQueryChanged = (store) => {
3737
this.setState({
3838
filter: store.filter,
3939
valid: store.valid,
4040
userTyping: store.userTyping
4141
});
42-
});
42+
};
43+
44+
// Also populate initial values
45+
onQueryChanged(QueryStore.state);
4346

47+
this.unsubscribeQueryStore = QueryStore.listen(onQueryChanged);
4448
this.unsubscribeMiniChartResize = Actions.resizeMiniCharts.listen(this.resizeListener);
4549
}
4650

@@ -73,17 +77,17 @@ class MiniChart extends React.Component {
7377
this.props.type.name) ? NUMBER : this.props.type.name;
7478

7579
const fieldName = this.props.fieldName;
76-
const queryClause = this.state.filter[fieldName];
80+
const queryValue = this.state.filter[fieldName];
7781
const hasDuplicates = this.props.type.has_duplicates;
7882
const fn = vizFns[typeName.toLowerCase()];
7983
const width = this.state.containerWidth;
8084

8185
if (_.includes([ STRING, NUMBER ], typeName) && !hasDuplicates) {
8286
return (
83-
<UniqueMinichart
87+
<UniqueMiniChart
8488
key={typeName}
8589
fieldName={fieldName}
86-
query={queryClause}
90+
queryValue={queryValue}
8791
type={this.props.type}
8892
width={width}
8993
/>
@@ -96,7 +100,7 @@ class MiniChart extends React.Component {
96100
fieldName={fieldName}
97101
type={this.props.type}
98102
renderMode="div"
99-
query={queryClause}
103+
query={queryValue}
100104
width={width}
101105
height={height}
102106
fn={vizFns.coordinates}
@@ -129,7 +133,7 @@ class MiniChart extends React.Component {
129133
fieldName={this.props.fieldName}
130134
type={this.props.type}
131135
renderMode="svg"
132-
query={queryClause}
136+
query={queryValue}
133137
width={width}
134138
height={100}
135139
fn={fn}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const React = require('react');
2+
const PropTypes = require('prop-types');
3+
const ValueBubble = require('./value-bubble');
4+
const _ = require('lodash');
5+
6+
// const debug = require('debug')('mongodb-compass:minichart:unique');
7+
8+
class UniqueMiniChart extends React.Component {
9+
constructor(props) {
10+
super(props);
11+
this.state = { sample: _.sample(this.props.type.values, 20) };
12+
}
13+
14+
onRefresh(e) {
15+
e.stopPropagation();
16+
e.preventDefault();
17+
this.setState({
18+
sample: _.sample(this.props.type.values, 20)
19+
});
20+
}
21+
22+
/**
23+
* Render a single field;
24+
*
25+
* @returns {React.Component} A react component for a single field
26+
*/
27+
render() {
28+
if (!this.props.type.values) {
29+
return <div></div>;
30+
}
31+
const sample = this.state.sample || [];
32+
const fieldName = this.props.fieldName.toLowerCase();
33+
const typeName = this.props.type.name.toLowerCase();
34+
const randomValueList = sample.map((value, i) => {
35+
return (
36+
<ValueBubble
37+
key={`${fieldName}-${typeName}-${i}`}
38+
value={value}
39+
queryValue={this.props.queryValue}
40+
fieldName={this.props.fieldName}
41+
/>
42+
);
43+
});
44+
const style = {
45+
width: this.props.width
46+
};
47+
48+
return (
49+
<div className="minichart unique" style={style}>
50+
<dl className="dl-horizontal">
51+
<dt>
52+
<i onClick={this.onRefresh.bind(this)} className="mms-icon-continuous" />
53+
</dt>
54+
<dd>
55+
<ul className="list-inline">
56+
{randomValueList}
57+
</ul>
58+
</dd>
59+
</dl>
60+
</div>
61+
);
62+
}
63+
}
64+
65+
UniqueMiniChart.propTypes = {
66+
fieldName: PropTypes.string.isRequired,
67+
queryValue: PropTypes.string,
68+
type: PropTypes.object.isRequired,
69+
width: PropTypes.number
70+
};
71+
72+
module.exports = UniqueMiniChart;

src/internal-packages/schema/lib/component/unique.jsx

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const app = require('hadron-app');
2+
const React = require('react');
3+
const PropTypes = require('prop-types');
4+
const _ = require('lodash');
5+
const hasDistinctValue = require('../../../query/lib/util').hasDistinctValue;
6+
7+
const { DECIMAL_128, DOUBLE, LONG, INT_32 } = require('../helpers');
8+
9+
class ValueBubble extends React.Component {
10+
onBubbleClicked(e) {
11+
const QueryAction = app.appRegistry.getAction('Query.Actions');
12+
const action = e.shiftKey ?
13+
QueryAction.toggleDistinctValue : QueryAction.setValue;
14+
action({
15+
field: this.props.fieldName,
16+
value: this.props.value,
17+
unsetIfSet: true
18+
});
19+
}
20+
21+
/**
22+
* converts the passed in value into a string, supports the 4 numeric
23+
* BSON types as well.
24+
*
25+
* @param {Any} value value to be converted to a string
26+
* @return {String} converted value
27+
*/
28+
_extractStringValue(value) {
29+
if (_.has(value, '_bsontype')) {
30+
if (_.includes([ DECIMAL_128, LONG ], value._bsontype)) {
31+
return value.toString();
32+
}
33+
if (_.includes([ DOUBLE, INT_32 ], value._bsontype)) {
34+
return String(value.value);
35+
}
36+
}
37+
if (_.isString(value)) {
38+
return value;
39+
}
40+
return String(value);
41+
}
42+
43+
render() {
44+
const value = this._extractStringValue(this.props.value);
45+
const selectedClass = hasDistinctValue(this.props.queryValue, this.props.value) ?
46+
'selected' : 'unselected';
47+
return (
48+
<li className="bubble">
49+
<code className={`selectable ${selectedClass}`} onClick={this.onBubbleClicked.bind(this)}>
50+
{value}
51+
</code>
52+
</li>
53+
);
54+
}
55+
}
56+
57+
ValueBubble.propTypes = {
58+
fieldName: PropTypes.string.isRequired,
59+
queryValue: PropTypes.string,
60+
value: PropTypes.any.isRequired
61+
};
62+
63+
module.exports = ValueBubble;

0 commit comments

Comments
 (0)