Skip to content

Commit 30ee112

Browse files
committed
fix: Filter values can't be changed in Playground -- revert back defaultHeuristic implementation
1 parent e67f7a6 commit 30ee112

File tree

1 file changed

+165
-21
lines changed

1 file changed

+165
-21
lines changed

packages/cubejs-client-react/src/QueryBuilder.jsx

Lines changed: 165 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import {
33
prop, uniqBy, indexBy, fromPairs
44
} from 'ramda';
5-
import { ResultSet, defaultHeuristics, moveItemInArray } from '@cubejs-client/core';
5+
import { ResultSet, moveItemInArray } from '@cubejs-client/core';
66
import QueryRenderer from './QueryRenderer.jsx';
77
import CubeContext from './CubeContext';
88

@@ -25,7 +25,7 @@ export default class QueryBuilder extends React.Component {
2525
if (Array.isArray(props.query)) {
2626
throw new Error('Array of queries is not supported.');
2727
}
28-
28+
2929
return {
3030
...nextState,
3131
query: {
@@ -34,12 +34,12 @@ export default class QueryBuilder extends React.Component {
3434
},
3535
};
3636
}
37-
37+
3838
static resolveMember(type, { meta, query }) {
3939
if (!meta) {
4040
return [];
4141
}
42-
42+
4343
if (Array.isArray(query)) {
4444
return query.reduce((memo, currentQuery) => memo.concat(QueryBuilder.resolveMember(type, {
4545
meta,
@@ -63,10 +63,10 @@ export default class QueryBuilder extends React.Component {
6363
...meta.resolveMember(m, type)
6464
}));
6565
}
66-
66+
6767
static getOrderMembers(state) {
6868
const { query, meta } = state;
69-
69+
7070
if (!meta) {
7171
return [];
7272
}
@@ -88,7 +88,7 @@ export default class QueryBuilder extends React.Component {
8888
}))
8989
);
9090
}
91-
91+
9292
constructor(props) {
9393
super(props);
9494

@@ -100,7 +100,7 @@ export default class QueryBuilder extends React.Component {
100100
validatedQuery: props.query,
101101
...props.vizState
102102
};
103-
103+
104104
this.mutexObj = {};
105105
}
106106

@@ -117,7 +117,7 @@ export default class QueryBuilder extends React.Component {
117117
} else {
118118
meta = await this.cubejsApi().meta();
119119
}
120-
120+
121121
this.setState({
122122
meta,
123123
orderMembers: QueryBuilder.getOrderMembers({ meta, query }),
@@ -173,7 +173,7 @@ export default class QueryBuilder extends React.Component {
173173
});
174174
}
175175
});
176-
176+
177177
const {
178178
meta,
179179
query,
@@ -231,7 +231,7 @@ export default class QueryBuilder extends React.Component {
231231
if (sourceIndex == null || destinationIndex == null) {
232232
return;
233233
}
234-
234+
235235
this.updateVizState({
236236
orderMembers: moveItemInArray(orderMembers, sourceIndex, destinationIndex)
237237
});
@@ -252,23 +252,23 @@ export default class QueryBuilder extends React.Component {
252252
};
253253
const id = pivotConfig[sourceAxis][sourceIndex];
254254
const lastIndex = nextPivotConfig[destinationAxis].length - 1;
255-
255+
256256
if (id === 'measures') {
257257
destinationIndex = lastIndex + 1;
258258
} else if (destinationIndex >= lastIndex && nextPivotConfig[destinationAxis][lastIndex] === 'measures') {
259259
destinationIndex = lastIndex - 1;
260260
}
261-
261+
262262
nextPivotConfig[sourceAxis].splice(sourceIndex, 1);
263263
nextPivotConfig[destinationAxis].splice(destinationIndex, 0, id);
264-
264+
265265
this.updateVizState({
266266
pivotConfig: nextPivotConfig
267267
});
268268
},
269269
update: (config) => {
270270
const { limit } = config;
271-
271+
272272
if (limit == null) {
273273
this.updateVizState({
274274
pivotConfig: {
@@ -299,7 +299,7 @@ export default class QueryBuilder extends React.Component {
299299
async updateVizState(state) {
300300
const { setQuery, setVizState } = this.props;
301301
const { query: stateQuery, pivotConfig: statePivotConfig } = this.state;
302-
302+
303303
let pivotQuery = {};
304304
let finalState = this.applyStateChangeHeuristics(state);
305305
const { order: _, ...query } = finalState.query || stateQuery;
@@ -385,17 +385,161 @@ export default class QueryBuilder extends React.Component {
385385
};
386386
}
387387

388+
defaultHeuristics(newState) {
389+
const { query, sessionGranularity } = this.state;
390+
const defaultGranularity = sessionGranularity || 'day';
391+
392+
if (Array.isArray(query)) {
393+
return newState;
394+
}
395+
396+
if (newState.query) {
397+
const oldQuery = query;
398+
let newQuery = newState.query;
399+
400+
const { meta } = this.state;
401+
402+
if (
403+
(oldQuery.timeDimensions || []).length === 1
404+
&& (newQuery.timeDimensions || []).length === 1
405+
&& newQuery.timeDimensions[0].granularity
406+
&& oldQuery.timeDimensions[0].granularity !== newQuery.timeDimensions[0].granularity
407+
) {
408+
newState = {
409+
...newState,
410+
sessionGranularity: newQuery.timeDimensions[0].granularity
411+
};
412+
}
413+
414+
if (
415+
((oldQuery.measures || []).length === 0 && (newQuery.measures || []).length > 0)
416+
|| ((oldQuery.measures || []).length === 1
417+
&& (newQuery.measures || []).length === 1
418+
&& oldQuery.measures[0] !== newQuery.measures[0])
419+
) {
420+
const defaultTimeDimension = meta.defaultTimeDimensionNameFor(newQuery.measures[0]);
421+
newQuery = {
422+
...newQuery,
423+
timeDimensions: defaultTimeDimension
424+
? [
425+
{
426+
dimension: defaultTimeDimension,
427+
granularity: defaultGranularity
428+
}
429+
]
430+
: []
431+
};
432+
433+
return {
434+
...newState,
435+
pivotConfig: null,
436+
shouldApplyHeuristicOrder: true,
437+
query: newQuery,
438+
chartType: defaultTimeDimension ? 'line' : 'number'
439+
};
440+
}
441+
442+
if ((oldQuery.dimensions || []).length === 0 && (newQuery.dimensions || []).length > 0) {
443+
newQuery = {
444+
...newQuery,
445+
timeDimensions: (newQuery.timeDimensions || []).map((td) => ({ ...td, granularity: undefined }))
446+
};
447+
448+
return {
449+
...newState,
450+
pivotConfig: null,
451+
shouldApplyHeuristicOrder: true,
452+
query: newQuery,
453+
chartType: 'table'
454+
};
455+
}
456+
457+
if ((oldQuery.dimensions || []).length > 0 && (newQuery.dimensions || []).length === 0) {
458+
newQuery = {
459+
...newQuery,
460+
timeDimensions: (newQuery.timeDimensions || []).map((td) => ({
461+
...td,
462+
granularity: td.granularity || defaultGranularity
463+
}))
464+
};
465+
466+
return {
467+
...newState,
468+
pivotConfig: null,
469+
shouldApplyHeuristicOrder: true,
470+
query: newQuery,
471+
chartType: (newQuery.timeDimensions || []).length ? 'line' : 'number'
472+
};
473+
}
474+
475+
if (
476+
((oldQuery.dimensions || []).length > 0 || (oldQuery.measures || []).length > 0)
477+
&& (newQuery.dimensions || []).length === 0
478+
&& (newQuery.measures || []).length === 0
479+
) {
480+
newQuery = {
481+
...newQuery,
482+
timeDimensions: [],
483+
filters: []
484+
};
485+
486+
return {
487+
...newState,
488+
pivotConfig: null,
489+
shouldApplyHeuristicOrder: true,
490+
query: newQuery,
491+
sessionGranularity: null
492+
};
493+
}
494+
return newState;
495+
}
496+
497+
if (newState.chartType) {
498+
const newChartType = newState.chartType;
499+
if (
500+
(newChartType === 'line' || newChartType === 'area')
501+
&& (query.timeDimensions || []).length === 1
502+
&& !query.timeDimensions[0].granularity
503+
) {
504+
const [td] = query.timeDimensions;
505+
return {
506+
...newState,
507+
pivotConfig: null,
508+
query: {
509+
...query,
510+
timeDimensions: [{ ...td, granularity: defaultGranularity }]
511+
}
512+
};
513+
}
514+
515+
if (
516+
(newChartType === 'pie' || newChartType === 'table' || newChartType === 'number')
517+
&& (query.timeDimensions || []).length === 1
518+
&& query.timeDimensions[0].granularity
519+
) {
520+
const [td] = query.timeDimensions;
521+
return {
522+
...newState,
523+
pivotConfig: null,
524+
shouldApplyHeuristicOrder: true,
525+
query: {
526+
...query,
527+
timeDimensions: [{ ...td, granularity: undefined }]
528+
}
529+
};
530+
}
531+
}
532+
533+
return newState;
534+
}
535+
388536
applyStateChangeHeuristics(newState) {
389-
const { query, meta, sessionGranularity } = this.state;
390537
const { stateChangeHeuristics, disableHeuristics } = this.props;
391538
if (disableHeuristics) {
392539
return newState;
393540
}
394541
return (stateChangeHeuristics && stateChangeHeuristics(this.state, newState))
395-
|| defaultHeuristics(newState.query, query, {
396-
meta,
397-
sessionGranularity
398-
});
542+
|| this.defaultHeuristics(newState);
399543
}
400544

401545
render() {

0 commit comments

Comments
 (0)