Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
padawannn committed Nov 24, 2023
2 parents 66b09a3 + ea8c2af commit ef6f0c7
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 99 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

## 2.2

### 2.2.16 (2023-11-23)

- Fix TableWidget cannot render columns when it contains an array [#802](https://github.com/CartoDB/carto-react/pull/802)
- Allow server-side table widget without hard limit [#798](https://github.com/CartoDB/carto-react/pull/798)

### 2.2.15 (2023-11-15)

- Fix TableWidget not displaying quadbin field for a table with spatial index [#799](https://github.com/CartoDB/carto-react/pull/799)

### 2.2.14 (2023-10-25)

- AppBar: Add new color keys in the Theme [#794](https://github.com/CartoDB/carto-react/pull/794)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "2.2.14"
"version": "2.2.16"
}
8 changes: 4 additions & 4 deletions packages/react-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-api",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Api",
"author": "CARTO Dev Team",
"keywords": [
Expand Down Expand Up @@ -68,9 +68,9 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-core": "^2.2.14",
"@carto/react-redux": "^2.2.14",
"@carto/react-workers": "^2.2.14",
"@carto/react-core": "^2.2.16",
"@carto/react-redux": "^2.2.16",
"@carto/react-workers": "^2.2.16",
"@deck.gl/carto": "^8.9.18",
"@deck.gl/core": "^8.9.18",
"@deck.gl/extensions": "^8.9.18",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-auth",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Auth",
"author": "CARTO Dev Team",
"keywords": [
Expand Down Expand Up @@ -68,7 +68,7 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-core": "^2.2.14",
"@carto/react-core": "^2.2.16",
"react": "17.x || 18.x",
"react-dom": "17.x || 18.x"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-basemaps/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-basemaps",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Basemaps",
"keywords": [
"carto",
Expand Down Expand Up @@ -68,7 +68,7 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-core": "^2.2.14",
"@carto/react-core": "^2.2.16",
"@deck.gl/google-maps": "^8.9.18",
"react": "17.x || 18.x",
"react-dom": "17.x || 18.x"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-core",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Core",
"author": "CARTO Dev Team",
"keywords": [
Expand Down
6 changes: 3 additions & 3 deletions packages/react-redux/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-redux",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Redux",
"author": "CARTO Dev Team",
"keywords": [
Expand Down Expand Up @@ -67,8 +67,8 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-core": "^2.2.14",
"@carto/react-workers": "^2.2.14",
"@carto/react-core": "^2.2.16",
"@carto/react-workers": "^2.2.16",
"@deck.gl/carto": "^8.9.18",
"@deck.gl/core": "^8.9.18",
"@reduxjs/toolkit": "^1.5.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-redux/src/slices/cartoSlice.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Feature, Polygon, MultiPolygon } from 'geojson';
type FilterValues = string[] | number[] | number[][]

export type SourceFilters = {
[column: string]: Partial<Record<_FilterTypes, { values: FilterValues; owner?: string; params?: Record<string, unknown>; isAnimation?: boolean }>>
[column: string]: Partial<Record<_FilterTypes, { values: FilterValues; owner?: string; params?: Record<string, unknown>; ignore?: boolean }>>
}

type Source = SourceProps & {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-ui",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - UI",
"author": "CARTO Dev Team",
"keywords": [
Expand Down Expand Up @@ -82,7 +82,7 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-core": "^2.2.14",
"@carto/react-core": "^2.2.16",
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@mui/icons-material": "^5.11.16",
Expand Down
9 changes: 8 additions & 1 deletion packages/react-ui/src/widgets/TableWidgetUI/TableWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,16 @@ function TableBodyComponent({ columns, rows, onRowClick }) {
onClick={() => onRowClick && onRowClick(row)}
>
{columns.map(({ field, headerName, align, component }) => {
const cellValue = Object.entries(row).find(([key]) => {
let cellValue = Object.entries(row).find(([key]) => {
return key.toUpperCase() === field.toUpperCase();
})?.[1];
if (typeof cellValue === 'bigint') {
cellValue = cellValue.toString(); // otherwise TableCell will fail for displaying it
} else if (Array.isArray(cellValue)) {
cellValue = `[${cellValue
.map((c) => (typeof c === 'string' ? `"${c}"` : c))
.join(', ')}]`;
}
return (
(headerName || field) && (
<TableCellStyled
Expand Down
46 changes: 11 additions & 35 deletions packages/react-widgets/__tests__/models/TableModel.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTable, paginateTable } from '../../src/models/TableModel';
import { getTable } from '../../src/models/TableModel';
import { Methods, executeTask } from '@carto/react-workers';

const RESULT = {
Expand All @@ -9,9 +9,9 @@ const RESULT = {
{ id: 40, city: 'Paris', value: 400 },
{ id: 50, city: 'London', value: 500 }
],
totalCount: 5,
hasData: true,
isDataComplete: true
metadata: {
total: 5
}
};

const mockedExecuteModel = jest.fn();
Expand All @@ -30,32 +30,6 @@ jest.mock('@carto/react-workers', () => ({
}
}));

describe('paginateTable', () => {
const data = {
rows: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], // 11 must hidden
totalCount: 10 // not 11
};
const tests = [
[0, 5, [1, 2, 3, 4, 5], 2], // first 5 elems
[1, 5, [6, 7, 8, 9, 10], 2], // second 5 elems
[0, 7, [1, 2, 3, 4, 5, 6, 7], 2], // first 7 elems
[1, 7, [8, 9, 10], 2], // second 7 elems, but not 11 that is hidden
[2, 5, [], 2], // 11 is hidden
[3, 5, [], 2], // no data at all
[0, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1] // corner case, 1 page not 2
];
it.each(tests)(
'should work as expected',
(page, rowsPerPage, expectedRows, expectedPages) => {
const expected = {
rows: expectedRows,
pages: expectedPages
};
expect(paginateTable(data, page, rowsPerPage)).toStrictEqual(expected);
}
);
});

describe('getTable', () => {
const tableParams = {
source: {
Expand All @@ -81,8 +55,7 @@ describe('getTable', () => {
{ id: 7, city: 'b', value: 1 }
],
totalCount: 2,
hasData: true,
isDataComplete: true
hasData: true
})
);
const data = await getTable(tableParams);
Expand All @@ -92,8 +65,7 @@ describe('getTable', () => {
{ id: 7, city: 'b', value: 1 }
],
totalCount: 2,
hasData: true,
isDataComplete: true
hasData: true
});
});

Expand All @@ -115,7 +87,11 @@ describe('getTable', () => {
describe('remote mode', () => {
test('correctly returns data', async () => {
const data = await getTable({ ...tableParams, remoteCalculation: true });
expect(data).toStrictEqual(RESULT);
expect(data).toStrictEqual({
rows: RESULT.rows,
totalCount: RESULT.metadata.total,
hasData: true
});
});
});
});
12 changes: 6 additions & 6 deletions packages/react-widgets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carto/react-widgets",
"version": "2.2.14",
"version": "2.2.16",
"description": "CARTO for React - Widgets",
"author": "CARTO Dev Team",
"keywords": [
Expand Down Expand Up @@ -69,11 +69,11 @@
"@babel/runtime": "^7.13.9"
},
"peerDependencies": {
"@carto/react-api": "^2.2.14",
"@carto/react-core": "^2.2.14",
"@carto/react-redux": "^2.2.14",
"@carto/react-ui": "^2.2.14",
"@carto/react-workers": "^2.2.14",
"@carto/react-api": "^2.2.16",
"@carto/react-core": "^2.2.16",
"@carto/react-redux": "^2.2.16",
"@carto/react-ui": "^2.2.16",
"@carto/react-workers": "^2.2.16",
"@deck.gl/core": "^8.9.18",
"@deck.gl/layers": "^8.9.18",
"@emotion/react": "^11.10.6",
Expand Down
42 changes: 19 additions & 23 deletions packages/react-widgets/src/models/TableModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,52 @@ import { _executeModel } from '@carto/react-api';
import { Methods, executeTask } from '@carto/react-workers';
import { normalizeObjectKeys, wrapModelCall } from './utils';

// Make sure this is sync with the same constant in cloud-native/maps-api
export const HARD_LIMIT = 100;

export function getTable(props) {
return wrapModelCall(props, fromLocal, fromRemote);
}

function fromLocal(props) {
// Injecting sortByColumnType externally from metadata gives better results. It allows to avoid deriving type from row value itself (with potential null values)
const { source, sortBy, sortDirection, sortByColumnType } = props;
const { source, sortBy, sortDirection, sortByColumnType, page, rowsPerPage } = props;

return executeTask(source.id, Methods.FEATURES_RAW, {
filters: source.filters,
filtersLogicalOperator: source.filtersLogicalOperator,
sortBy,
sortByDirection: sortDirection,
sortByColumnType
sortByColumnType,
page,
rowsPerPage
});
}

export function paginateTable({ rows, totalCount }, page, rowsPerPage) {
const sliced = rows.slice(
Math.min(rowsPerPage * Math.max(0, page), totalCount),
Math.min(rowsPerPage * Math.max(1, page + 1), totalCount)
);
const pages = Math.ceil(totalCount / rowsPerPage);
return { rows: sliced, pages };
}

function formatResult(res) {
const hasData = res.length > 0;
// We can detect if the data is complete because we request HARD_LIMIT + 1
const isDataComplete = res.length <= HARD_LIMIT;
// The actual extra record is hidden from pagination logic
const totalCount = isDataComplete ? res.length : HARD_LIMIT;
return { rows: res, totalCount, hasData, isDataComplete };
const { rows, totalCount } = res;
const hasData = totalCount > 0;
return { rows, totalCount, hasData };
}

// From remote
function fromRemote(props) {
const { source, spatialFilter, abortController, ...params } = props;
const { columns, sortBy, sortDirection } = params;
const { columns, sortBy, sortDirection, page, rowsPerPage } = params;

return _executeModel({
model: 'table',
source,
spatialFilter,
params: { column: columns, sortBy, sortDirection, limit: HARD_LIMIT + 1 },
params: {
column: columns,
sortBy,
sortDirection,
limit: rowsPerPage,
offset: page * rowsPerPage
},
opts: { abortController }
})
.then((res) => normalizeObjectKeys(res.rows))
.then((res) => ({
rows: normalizeObjectKeys(res.rows),
totalCount: res.metadata.total
}))
.then(formatResult);
}
2 changes: 1 addition & 1 deletion packages/react-widgets/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { getCategories } from './CategoryModel';
export { geocodeStreetPoint } from './GeocodingModel';
export { getScatter, HARD_LIMIT as SCATTER_PLOT_HARD_LIMIT } from './ScatterPlotModel';
export { getTimeSeries } from './TimeSeriesModel';
export { getTable, paginateTable, HARD_LIMIT as TABLE_HARD_LIMIT } from './TableModel';
export { getTable } from './TableModel';
2 changes: 2 additions & 0 deletions packages/react-widgets/src/models/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ function getSqlEscapedSource(table, provider) {
export function normalizeObjectKeys(el) {
if (Array.isArray(el)) {
return el.map(normalizeObjectKeys);
} else if (typeof el !== 'object') {
return el;
}

return Object.entries(el).reduce((acc, [key, value]) => {
Expand Down
Loading

0 comments on commit ef6f0c7

Please sign in to comment.