@cubejs-client/react 1.7.13 → 1.7.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CubeContext.d.ts +35 -0
- package/dist/CubeContext.d.ts.map +1 -0
- package/dist/CubeProvider.d.ts +31 -0
- package/dist/CubeProvider.d.ts.map +1 -0
- package/dist/QueryBuilder.d.ts +135 -0
- package/dist/QueryBuilder.d.ts.map +1 -0
- package/dist/QueryRenderer.d.ts +34 -0
- package/dist/QueryRenderer.d.ts.map +1 -0
- package/dist/QueryRendererWithTotals.d.ts +4 -0
- package/dist/QueryRendererWithTotals.d.ts.map +1 -0
- package/dist/cubejs-client-react.cjs.js +248 -23
- package/dist/cubejs-client-react.cjs.js.map +1 -1
- package/dist/cubejs-client-react.esm.js +253 -31
- package/dist/cubejs-client-react.esm.js.map +1 -1
- package/dist/cubejs-client-react.umd.js +248 -23
- package/dist/cubejs-client-react.umd.js.map +1 -1
- package/dist/hooks/cube-fetch.d.ts +6 -0
- package/dist/hooks/cube-fetch.d.ts.map +1 -0
- package/dist/hooks/cube-meta.d.ts +4 -0
- package/dist/hooks/cube-meta.d.ts.map +1 -0
- package/dist/hooks/cube-query.d.ts +39 -0
- package/dist/hooks/cube-query.d.ts.map +1 -0
- package/dist/hooks/cube-sql.d.ts +12 -0
- package/dist/hooks/cube-sql.d.ts.map +1 -0
- package/dist/hooks/deep-compare-memoize.d.ts +2 -0
- package/dist/hooks/deep-compare-memoize.d.ts.map +1 -0
- package/dist/hooks/dry-run.d.ts +7 -0
- package/dist/hooks/dry-run.d.ts.map +1 -0
- package/dist/hooks/lazy-dry-run.d.ts +7 -0
- package/dist/hooks/lazy-dry-run.d.ts.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/types.d.ts +765 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +14 -10
- package/src/CubeContext.ts +37 -0
- package/src/CubeProvider.tsx +42 -0
- package/src/{QueryBuilder.jsx → QueryBuilder.tsx} +238 -89
- package/src/{QueryRenderer.jsx → QueryRenderer.tsx} +39 -14
- package/src/QueryRendererWithTotals.tsx +24 -0
- package/src/hooks/cube-fetch.ts +98 -0
- package/src/hooks/cube-meta.ts +8 -0
- package/src/hooks/{cube-query.js → cube-query.ts} +90 -14
- package/src/hooks/cube-sql.ts +22 -0
- package/src/hooks/{deep-compare-memoize.js → deep-compare-memoize.ts} +2 -2
- package/src/hooks/dry-run.ts +17 -0
- package/src/hooks/lazy-dry-run.ts +24 -0
- package/src/index.ts +77 -0
- package/src/types.ts +837 -0
- package/src/{utils.js → utils.ts} +3 -3
- package/index.d.ts +0 -697
- package/src/CubeContext.js +0 -3
- package/src/CubeProvider.jsx +0 -14
- package/src/QueryRendererWithTotals.jsx +0 -20
- package/src/hooks/cube-fetch.js +0 -66
- package/src/hooks/cube-meta.js +0 -5
- package/src/hooks/cube-sql.js +0 -8
- package/src/hooks/dry-run.js +0 -8
- package/src/hooks/lazy-dry-run.js +0 -11
- package/src/index.js +0 -18
|
@@ -339,13 +339,59 @@
|
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
341
|
|
|
342
|
+
/**
|
|
343
|
+
* In case when you need direct access to `cubeApi` you can use `CubeContext` anywhere in your app
|
|
344
|
+
*
|
|
345
|
+
* ```js
|
|
346
|
+
* import React from 'react';
|
|
347
|
+
* import { CubeContext } from '@cubejs-client/react';
|
|
348
|
+
*
|
|
349
|
+
* export default function DisplayComponent() {
|
|
350
|
+
* const { cubeApi } = React.useContext(CubeContext);
|
|
351
|
+
* const [rawResults, setRawResults] = React.useState([]);
|
|
352
|
+
* const query = {
|
|
353
|
+
* ...
|
|
354
|
+
* };
|
|
355
|
+
*
|
|
356
|
+
* React.useEffect(() => {
|
|
357
|
+
* cubeApi.load(query).then((resultSet) => {
|
|
358
|
+
* setRawResults(resultSet.rawData());
|
|
359
|
+
* });
|
|
360
|
+
* }, [query]);
|
|
361
|
+
*
|
|
362
|
+
* return (
|
|
363
|
+
* <>
|
|
364
|
+
* {rawResults.map(row => (
|
|
365
|
+
* ...
|
|
366
|
+
* ))}
|
|
367
|
+
* </>
|
|
368
|
+
* )
|
|
369
|
+
* }
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
// The context has no default value: `cubeApi` is only available under a
|
|
373
|
+
// `CubeProvider`, and consumers guard against a missing context.
|
|
342
374
|
var CubeContext = /*#__PURE__*/React.createContext(null);
|
|
343
375
|
|
|
376
|
+
/**
|
|
377
|
+
* `<QueryRenderer />` a react component that accepts a query, fetches the given query, and uses the render prop to render the resulting data
|
|
378
|
+
* @stickyTypes QueryRendererProps, QueryRendererRenderProps
|
|
379
|
+
* @noInheritDoc
|
|
380
|
+
*/
|
|
344
381
|
var QueryRenderer = /*#__PURE__*/function (_React$Component) {
|
|
345
382
|
function QueryRenderer(props) {
|
|
346
383
|
var _this;
|
|
347
384
|
_classCallCheck(this, QueryRenderer);
|
|
348
385
|
_this = _callSuper(this, QueryRenderer, [props]);
|
|
386
|
+
// These sit after the lifecycle methods because `react/sort-comp` sorts
|
|
387
|
+
// TypeScript field declarations into `everything-else`, which the configured
|
|
388
|
+
// order puts after `lifecycle`. Runtime is unaffected: field initializers run
|
|
389
|
+
// right after `super()`, so the constructor's assignments still win.
|
|
390
|
+
//
|
|
391
|
+
// `this.context` is not re-declared: React types it as `any`, and a field
|
|
392
|
+
// declaration would be emitted at runtime and shadow the context React
|
|
393
|
+
// assigns.
|
|
394
|
+
_defineProperty(_this, "mutexObj", void 0);
|
|
349
395
|
_this.state = {};
|
|
350
396
|
_this.mutexObj = {};
|
|
351
397
|
return _this;
|
|
@@ -542,6 +588,8 @@
|
|
|
542
588
|
sqlQuery: sqlQuery
|
|
543
589
|
};
|
|
544
590
|
if (render) {
|
|
591
|
+
// The prop is declared as returning `void` for backwards compatibility,
|
|
592
|
+
// while what it returns is what gets rendered
|
|
545
593
|
return render(loadState);
|
|
546
594
|
}
|
|
547
595
|
return null;
|
|
@@ -573,6 +621,7 @@
|
|
|
573
621
|
restProps = _objectWithoutProperties(_ref, _excluded$1);
|
|
574
622
|
return /*#__PURE__*/React__default["default"].createElement(QueryRenderer, _extends({
|
|
575
623
|
queries: {
|
|
624
|
+
// `granularity: null` is sent to the API on purpose to get totals
|
|
576
625
|
totals: _objectSpread2(_objectSpread2({}, query), {}, {
|
|
577
626
|
dimensions: [],
|
|
578
627
|
timeDimensions: query.timeDimensions ? query.timeDimensions.map(function (td) {
|
|
@@ -633,13 +682,89 @@
|
|
|
633
682
|
name: 'year',
|
|
634
683
|
title: 'Year'
|
|
635
684
|
}];
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* `<QueryBuilder />` is used to build interactive analytics query builders. It abstracts state management and API calls to Cube.js Backend. It uses render prop technique and doesn’t render anything itself, but calls the render function instead.
|
|
688
|
+
*
|
|
689
|
+
* **Example**
|
|
690
|
+
*
|
|
691
|
+
* [Open in CodeSandbox](https://codesandbox.io/s/z6r7qj8wm)
|
|
692
|
+
* ```js
|
|
693
|
+
* import React from 'react';
|
|
694
|
+
* import ReactDOM from 'react-dom';
|
|
695
|
+
* import { Layout, Divider, Empty, Select } from 'antd';
|
|
696
|
+
* import { QueryBuilder } from '@cubejs-client/react';
|
|
697
|
+
* import cube from '@cubejs-client/core';
|
|
698
|
+
* import 'antd/dist/antd.css';
|
|
699
|
+
*
|
|
700
|
+
* import ChartRenderer from './ChartRenderer';
|
|
701
|
+
*
|
|
702
|
+
* const cubeApi = cube('YOUR-CUBE-API-TOKEN', {
|
|
703
|
+
* apiUrl: 'http://localhost:4000/cubejs-api/v1',
|
|
704
|
+
* });
|
|
705
|
+
*
|
|
706
|
+
* const App = () => (
|
|
707
|
+
* <QueryBuilder
|
|
708
|
+
* query={{
|
|
709
|
+
* timeDimensions: [
|
|
710
|
+
* {
|
|
711
|
+
* dimension: 'LineItems.createdAt',
|
|
712
|
+
* granularity: 'month',
|
|
713
|
+
* },
|
|
714
|
+
* ],
|
|
715
|
+
* }}
|
|
716
|
+
* cubeApi={cubeApi}
|
|
717
|
+
* render={({ resultSet, measures, availableMeasures, updateMeasures }) => (
|
|
718
|
+
* <Layout.Content style={{ padding: '20px' }}>
|
|
719
|
+
* <Select
|
|
720
|
+
* mode="multiple"
|
|
721
|
+
* style={{ width: '100%' }}
|
|
722
|
+
* placeholder="Please select"
|
|
723
|
+
* onSelect={(measure) => updateMeasures.add(measure)}
|
|
724
|
+
* onDeselect={(measure) => updateMeasures.remove(measure)}
|
|
725
|
+
* >
|
|
726
|
+
* {availableMeasures.map((measure) => (
|
|
727
|
+
* <Select.Option key={measure.name} value={measure}>
|
|
728
|
+
* {measure.title}
|
|
729
|
+
* </Select.Option>
|
|
730
|
+
* ))}
|
|
731
|
+
* </Select>
|
|
732
|
+
* <Divider />
|
|
733
|
+
* {measures.length > 0 ? (
|
|
734
|
+
* <ChartRenderer resultSet={resultSet} />
|
|
735
|
+
* ) : (
|
|
736
|
+
* <Empty description="Select measure or dimension to get started" />
|
|
737
|
+
* )}
|
|
738
|
+
* </Layout.Content>
|
|
739
|
+
* )}
|
|
740
|
+
* />
|
|
741
|
+
* );
|
|
742
|
+
*
|
|
743
|
+
* const rootElement = document.getElementById("root");
|
|
744
|
+
* ReactDOM.render(<App />, rootElement);
|
|
745
|
+
* ```
|
|
746
|
+
* @stickyTypes QueryBuilderProps, QueryBuilderRenderProps, QueryBuilderState
|
|
747
|
+
* @noInheritDoc
|
|
748
|
+
* @order 2
|
|
749
|
+
*/
|
|
636
750
|
var QueryBuilder = /*#__PURE__*/function (_React$Component) {
|
|
637
751
|
function QueryBuilder(props) {
|
|
638
752
|
var _this;
|
|
639
753
|
_classCallCheck(this, QueryBuilder);
|
|
640
754
|
_this = _callSuper(this, QueryBuilder, [props]);
|
|
755
|
+
// These sit after the lifecycle methods because `react/sort-comp` sorts
|
|
756
|
+
// TypeScript field declarations into `everything-else`, which the configured
|
|
757
|
+
// order puts after `lifecycle`. Runtime is unaffected: field initializers run
|
|
758
|
+
// right after `super()`, so the constructor's assignments still win.
|
|
759
|
+
//
|
|
760
|
+
// `this.context` is not re-declared: React types it as `any`, and a field
|
|
761
|
+
// declaration would be emitted at runtime and shadow the context React
|
|
762
|
+
// assigns.
|
|
763
|
+
_defineProperty(_this, "mutexObj", void 0);
|
|
764
|
+
_defineProperty(_this, "orderMembersOrderKeys", void 0);
|
|
765
|
+
_defineProperty(_this, "prevContext", void 0);
|
|
641
766
|
_defineProperty(_this, "fetchMeta", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
642
|
-
var meta, metaError, richMetaError, metaErrorStack,
|
|
767
|
+
var meta, metaError, richMetaError, metaErrorStack, _requestError$respons, _requestError$respons2, requestError, _t;
|
|
643
768
|
return _regenerator().w(function (_context) {
|
|
644
769
|
while (1) switch (_context.p = _context.n) {
|
|
645
770
|
case 0:
|
|
@@ -665,9 +790,10 @@
|
|
|
665
790
|
case 4:
|
|
666
791
|
_context.p = 4;
|
|
667
792
|
_t = _context.v;
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
793
|
+
requestError = _t;
|
|
794
|
+
metaError = ((_requestError$respons = requestError.response) === null || _requestError$respons === void 0 ? void 0 : _requestError$respons.plainError) || requestError;
|
|
795
|
+
richMetaError = requestError;
|
|
796
|
+
metaErrorStack = ((_requestError$respons2 = requestError.response) === null || _requestError$respons2 === void 0 || (_requestError$respons2 = _requestError$respons2.stack) === null || _requestError$respons2 === void 0 ? void 0 : _requestError$respons2.replace(requestError.message || '', '')) || '';
|
|
671
797
|
case 5:
|
|
672
798
|
_this.setState({
|
|
673
799
|
meta: meta,
|
|
@@ -918,6 +1044,9 @@
|
|
|
918
1044
|
var indexedMeasures = ramda.indexBy(ramda.prop('cubeName'), availableMembers.measures);
|
|
919
1045
|
var indexedDimensions = ramda.indexBy(ramda.prop('cubeName'), availableMembers.dimensions);
|
|
920
1046
|
var cubeNames = ramda.uniq([].concat(_toConsumableArray(Object.keys(indexedMeasures)), _toConsumableArray(Object.keys(indexedDimensions)))).sort();
|
|
1047
|
+
|
|
1048
|
+
// Measures and dimensions of a cube are merged into one member list, which
|
|
1049
|
+
// the declared type describes as either one or the other
|
|
921
1050
|
availableFilterMembers = cubeNames.map(function (name) {
|
|
922
1051
|
var _indexedMeasures$name, _indexedDimensions$na;
|
|
923
1052
|
var cube = indexedMeasures[name] || indexedDimensions[name];
|
|
@@ -970,6 +1099,8 @@
|
|
|
970
1099
|
chartType: chartType,
|
|
971
1100
|
measures: measures,
|
|
972
1101
|
dimensions: dimensions,
|
|
1102
|
+
// The declared granularity options require a `name`, which the
|
|
1103
|
+
// "w/o grouping" option leaves out
|
|
973
1104
|
timeDimensions: timeDimensions,
|
|
974
1105
|
segments: segments,
|
|
975
1106
|
filters: filters,
|
|
@@ -1074,7 +1205,7 @@
|
|
|
1074
1205
|
value: function () {
|
|
1075
1206
|
var _updateVizState = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(state) {
|
|
1076
1207
|
var _this4 = this;
|
|
1077
|
-
var _this$props2, setQuery, setVizState, _this$state2, stateQuery, statePivotConfig, chartType, meta, finalState, vizStateSent, handleVizStateChange, runSetters, shouldFetchDryRun, response,
|
|
1208
|
+
var _this$props2, setQuery, setVizState, _this$state2, stateQuery, statePivotConfig, chartType, meta, finalState, vizStateSent, handleVizStateChange, runSetters, shouldFetchDryRun, response, _requestError$respons3, requestError, _t3;
|
|
1078
1209
|
return _regenerator().w(function (_context5) {
|
|
1079
1210
|
while (1) switch (_context5.p = _context5.n) {
|
|
1080
1211
|
case 0:
|
|
@@ -1152,9 +1283,10 @@
|
|
|
1152
1283
|
case 3:
|
|
1153
1284
|
_context5.p = 3;
|
|
1154
1285
|
_t3 = _context5.v;
|
|
1286
|
+
requestError = _t3;
|
|
1155
1287
|
this.setState({
|
|
1156
|
-
queryError: new Error(((
|
|
1157
|
-
richQueryError: new Error(
|
|
1288
|
+
queryError: new Error(((_requestError$respons3 = requestError.response) === null || _requestError$respons3 === void 0 ? void 0 : _requestError$respons3.plainError) || requestError.message),
|
|
1289
|
+
richQueryError: new Error(requestError.message || requestError.toString())
|
|
1158
1290
|
});
|
|
1159
1291
|
case 4:
|
|
1160
1292
|
this.setState(finalState, function () {
|
|
@@ -1297,7 +1429,33 @@
|
|
|
1297
1429
|
setVizState: null
|
|
1298
1430
|
});
|
|
1299
1431
|
|
|
1300
|
-
|
|
1432
|
+
/**
|
|
1433
|
+
* Cube.js context provider
|
|
1434
|
+
* ```js
|
|
1435
|
+
* import React from 'react';
|
|
1436
|
+
* import cube from '@cubejs-client/core';
|
|
1437
|
+
* import { CubeProvider } from '@cubejs-client/react';
|
|
1438
|
+
*
|
|
1439
|
+
* const API_URL = 'https://harsh-eel.aws-us-east-2.cubecloudapp.dev';
|
|
1440
|
+
* const CUBE_TOKEN =
|
|
1441
|
+
* 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.* eyJpYXQiOjE1OTE3MDcxNDgsImV4cCI6MTU5NDI5OTE0OH0.* n5jGLQJ14igg6_Hri_Autx9qOIzVqp4oYxmX27V-4T4';
|
|
1442
|
+
*
|
|
1443
|
+
* const cubeApi = cube(CUBE_TOKEN, {
|
|
1444
|
+
* apiUrl: `${API_URL}/cubejs-api/v1`,
|
|
1445
|
+
* });
|
|
1446
|
+
*
|
|
1447
|
+
* export default function App() {
|
|
1448
|
+
* return (
|
|
1449
|
+
* <CubeProvider cubeApi={cubeApi}>
|
|
1450
|
+
* //...
|
|
1451
|
+
* </CubeProvider>
|
|
1452
|
+
* )
|
|
1453
|
+
* }
|
|
1454
|
+
* ```
|
|
1455
|
+
* @stickyTypes
|
|
1456
|
+
* @order 10
|
|
1457
|
+
*/
|
|
1458
|
+
var CubeProvider = function CubeProvider(_ref) {
|
|
1301
1459
|
var cubeApi = _ref.cubeApi,
|
|
1302
1460
|
children = _ref.children,
|
|
1303
1461
|
_ref$options = _ref.options,
|
|
@@ -1308,7 +1466,7 @@
|
|
|
1308
1466
|
options: options
|
|
1309
1467
|
}
|
|
1310
1468
|
}, children);
|
|
1311
|
-
}
|
|
1469
|
+
};
|
|
1312
1470
|
|
|
1313
1471
|
function useDeepCompareMemoize(value) {
|
|
1314
1472
|
var ref = React.useRef([]);
|
|
@@ -1318,6 +1476,10 @@
|
|
|
1318
1476
|
return ref.current;
|
|
1319
1477
|
}
|
|
1320
1478
|
|
|
1479
|
+
/**
|
|
1480
|
+
* @hidden
|
|
1481
|
+
*/
|
|
1482
|
+
|
|
1321
1483
|
function useCubeFetch(method) {
|
|
1322
1484
|
var _options$query;
|
|
1323
1485
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -1345,10 +1507,12 @@
|
|
|
1345
1507
|
ignoreSkip,
|
|
1346
1508
|
cubeApi,
|
|
1347
1509
|
query,
|
|
1510
|
+
onlyViews,
|
|
1348
1511
|
queryCondition,
|
|
1349
1512
|
coreOptions,
|
|
1350
1513
|
args,
|
|
1351
|
-
|
|
1514
|
+
fetchMethod,
|
|
1515
|
+
fetchResponse,
|
|
1352
1516
|
_args = arguments,
|
|
1353
1517
|
_t;
|
|
1354
1518
|
return _regenerator().w(function (_context) {
|
|
@@ -1358,6 +1522,7 @@
|
|
|
1358
1522
|
ignoreSkip = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
|
|
1359
1523
|
cubeApi = options.cubeApi || (context === null || context === void 0 ? void 0 : context.cubeApi);
|
|
1360
1524
|
query = loadOptions.query || options.query;
|
|
1525
|
+
onlyViews = 'onlyViews' in loadOptions ? loadOptions.onlyViews : options.onlyViews;
|
|
1361
1526
|
queryCondition = method === 'meta' ? true : query && core.isQueryPresent(query);
|
|
1362
1527
|
if (!(cubeApi && (ignoreSkip || !skip) && queryCondition)) {
|
|
1363
1528
|
_context.n = 4;
|
|
@@ -1368,20 +1533,24 @@
|
|
|
1368
1533
|
isLoading: true,
|
|
1369
1534
|
response: null
|
|
1370
1535
|
});
|
|
1371
|
-
coreOptions = _objectSpread2({
|
|
1536
|
+
coreOptions = _objectSpread2(_objectSpread2({
|
|
1372
1537
|
mutexObj: mutexRef.current,
|
|
1373
1538
|
mutexKey: method
|
|
1374
1539
|
}, options.baseRequestId ? {
|
|
1375
1540
|
baseRequestId: options.baseRequestId
|
|
1541
|
+
} : {}), method === 'meta' && onlyViews ? {
|
|
1542
|
+
onlyViews: true
|
|
1376
1543
|
} : {});
|
|
1377
|
-
args = method === 'meta' ? [coreOptions] : [query, coreOptions];
|
|
1544
|
+
args = method === 'meta' ? [coreOptions] : [query, coreOptions]; // `method` picks between overloaded `CubeApi` methods, so the call is
|
|
1545
|
+
// dispatched dynamically while keeping `cubeApi` as the receiver
|
|
1546
|
+
fetchMethod = cubeApi[method];
|
|
1378
1547
|
_context.p = 1;
|
|
1379
1548
|
_context.n = 2;
|
|
1380
|
-
return
|
|
1549
|
+
return fetchMethod.apply(cubeApi, args);
|
|
1381
1550
|
case 2:
|
|
1382
|
-
|
|
1551
|
+
fetchResponse = _context.v;
|
|
1383
1552
|
setResponse({
|
|
1384
|
-
response:
|
|
1553
|
+
response: fetchResponse,
|
|
1385
1554
|
isLoading: false
|
|
1386
1555
|
});
|
|
1387
1556
|
_context.n = 4;
|
|
@@ -1403,15 +1572,24 @@
|
|
|
1403
1572
|
}
|
|
1404
1573
|
React.useEffect(function () {
|
|
1405
1574
|
load();
|
|
1575
|
+
// `order` is read off a single query; an array of queries does not carry it
|
|
1406
1576
|
}, useDeepCompareMemoize([Object.keys(((_options$query = options.query) === null || _options$query === void 0 ? void 0 : _options$query.order) || {}), options, context]));
|
|
1407
1577
|
return _objectSpread2(_objectSpread2({}, response), {}, {
|
|
1408
1578
|
error: error,
|
|
1409
|
-
refetch: function refetch(
|
|
1410
|
-
return load(
|
|
1579
|
+
refetch: function refetch(refetchOptions) {
|
|
1580
|
+
return load(refetchOptions, true);
|
|
1411
1581
|
}
|
|
1412
1582
|
});
|
|
1413
1583
|
}
|
|
1414
1584
|
|
|
1585
|
+
/**
|
|
1586
|
+
* The hook resolves with a `SqlQuery`, while its `response` stays declared as a
|
|
1587
|
+
* dry-run response for backwards compatibility — `UseCubeSqlResult` spells the
|
|
1588
|
+
* real shape. `refetch` is declared, as it is on `useDryRun` and `useCubeMeta`,
|
|
1589
|
+
* because all three return the same object.
|
|
1590
|
+
*
|
|
1591
|
+
* @hidden
|
|
1592
|
+
*/
|
|
1415
1593
|
function useCubeSql(query) {
|
|
1416
1594
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1417
1595
|
return useCubeFetch('sql', _objectSpread2(_objectSpread2({}, options), {}, {
|
|
@@ -1419,6 +1597,9 @@
|
|
|
1419
1597
|
}));
|
|
1420
1598
|
}
|
|
1421
1599
|
|
|
1600
|
+
/**
|
|
1601
|
+
* @hidden
|
|
1602
|
+
*/
|
|
1422
1603
|
function useDryRun(query) {
|
|
1423
1604
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1424
1605
|
return useCubeFetch('dryRun', _objectSpread2(_objectSpread2({}, options), {}, {
|
|
@@ -1427,6 +1608,9 @@
|
|
|
1427
1608
|
}
|
|
1428
1609
|
|
|
1429
1610
|
var _excluded = ["refetch"];
|
|
1611
|
+
/**
|
|
1612
|
+
* @hidden
|
|
1613
|
+
*/
|
|
1430
1614
|
function useLazyDryRun(query) {
|
|
1431
1615
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1432
1616
|
var _useCubeFetch = useCubeFetch('dryRun', _objectSpread2(_objectSpread2({}, options), {}, {
|
|
@@ -1438,6 +1622,42 @@
|
|
|
1438
1622
|
return [refetch, result];
|
|
1439
1623
|
}
|
|
1440
1624
|
|
|
1625
|
+
/**
|
|
1626
|
+
* A React hook for executing Cube.js queries
|
|
1627
|
+
* ```js
|
|
1628
|
+
* import React from 'react';
|
|
1629
|
+
* import { Table } from 'antd';
|
|
1630
|
+
* import { useCubeQuery } from '@cubejs-client/react';
|
|
1631
|
+
*
|
|
1632
|
+
* export default function App() {
|
|
1633
|
+
* const { resultSet, isLoading, error, progress } = useCubeQuery({
|
|
1634
|
+
* measures: ['Orders.count'],
|
|
1635
|
+
* dimensions: ['Orders.createdAt.month'],
|
|
1636
|
+
* });
|
|
1637
|
+
*
|
|
1638
|
+
* if (isLoading) {
|
|
1639
|
+
* return <div>{progress?.stage || 'Loading...'}</div>;
|
|
1640
|
+
* }
|
|
1641
|
+
*
|
|
1642
|
+
* if (error) {
|
|
1643
|
+
* return <div>{error.toString()}</div>;
|
|
1644
|
+
* }
|
|
1645
|
+
*
|
|
1646
|
+
* if (!resultSet) {
|
|
1647
|
+
* return null;
|
|
1648
|
+
* }
|
|
1649
|
+
*
|
|
1650
|
+
* const dataSource = resultSet.tablePivot();
|
|
1651
|
+
* const columns = resultSet.tableColumns();
|
|
1652
|
+
*
|
|
1653
|
+
* return <Table columns={columns} dataSource={dataSource} />;
|
|
1654
|
+
* }
|
|
1655
|
+
*
|
|
1656
|
+
* ```
|
|
1657
|
+
* @order 1
|
|
1658
|
+
* @stickyTypes
|
|
1659
|
+
*/
|
|
1660
|
+
|
|
1441
1661
|
function useCubeQuery(query) {
|
|
1442
1662
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1443
1663
|
var mutexRef = React.useRef({});
|
|
@@ -1463,9 +1683,10 @@
|
|
|
1463
1683
|
setError = _useState0[1];
|
|
1464
1684
|
var context = React.useContext(CubeContext);
|
|
1465
1685
|
var subscribeRequest = null;
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1686
|
+
|
|
1687
|
+
// `progressResponse` is not part of the public `ProgressResult` API
|
|
1688
|
+
var progressCallback = function progressCallback(progressResult) {
|
|
1689
|
+
return setProgress(progressResult.progressResponse);
|
|
1469
1690
|
};
|
|
1470
1691
|
function fetch() {
|
|
1471
1692
|
return _fetch.apply(this, arguments);
|
|
@@ -1533,7 +1754,7 @@
|
|
|
1533
1754
|
}
|
|
1534
1755
|
function _loadQuery() {
|
|
1535
1756
|
_loadQuery = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
1536
|
-
var _t;
|
|
1757
|
+
var previousQuery, _t;
|
|
1537
1758
|
return _regenerator().w(function (_context) {
|
|
1538
1759
|
while (1) switch (_context.p = _context.n) {
|
|
1539
1760
|
case 0:
|
|
@@ -1541,7 +1762,10 @@
|
|
|
1541
1762
|
_context.n = 7;
|
|
1542
1763
|
break;
|
|
1543
1764
|
}
|
|
1544
|
-
|
|
1765
|
+
// `areQueriesEqual` is declared for a single query, and reads no more
|
|
1766
|
+
// than `order` off one when given an array of queries
|
|
1767
|
+
previousQuery = currentQuery;
|
|
1768
|
+
if (!core.areQueriesEqual(previousQuery, query)) {
|
|
1545
1769
|
if (resetResultSetOnChange == null || resetResultSetOnChange) {
|
|
1546
1770
|
setResultSet(null);
|
|
1547
1771
|
}
|
|
@@ -1607,7 +1831,8 @@
|
|
|
1607
1831
|
subscribeRequest = null;
|
|
1608
1832
|
}
|
|
1609
1833
|
};
|
|
1610
|
-
|
|
1834
|
+
// `order` is read off a single query; an array of queries does not carry it
|
|
1835
|
+
}, useDeepCompareMemoize([query, Object.keys((query === null || query === void 0 ? void 0 : query.order) || {}), options, context]));
|
|
1611
1836
|
return {
|
|
1612
1837
|
isLoading: isLoading,
|
|
1613
1838
|
resultSet: resultSet,
|