Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support local Tileset Search in react-workers #903

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions packages/react-widgets/src/models/TableModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ export function getTable(props) {

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, page, rowsPerPage } = props;
const {
source,
sortBy,
sortDirection,
sortByColumnType,
page,
rowsPerPage,
searchFilterText,
searchFilterColumn
} = props;

return executeTask(source.id, Methods.FEATURES_RAW, {
filters: source.filters,
filtersLogicalOperator: source.filtersLogicalOperator,
searchFilterText,
searchFilterColumn,
sortBy,
sortByDirection: sortDirection,
sortByColumnType,
Expand All @@ -29,14 +40,26 @@ function formatResult(res) {

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

const searchFilter =
searchFilterText && searchFilterColumn
? { [searchFilterColumn]: { stringSearch: { values: [searchFilterText] } } }
: null;

return _executeModel({
model: 'table',
source,
spatialFilter,
searchFilter,
...(searchFilter && { searchFilter }),
params: {
column: columns,
sortBy,
Expand Down
5 changes: 2 additions & 3 deletions packages/react-widgets/src/widgets/TableWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ function TableWidget({
dataSource,
params: {
columns: [...columns.map((c) => c.field), ...hiddenColumnFields],
searchFilter: containsStringSearchFilter && {
[searchColumn]: { stringSearch: { values: [searchText] } }
},
searchFilterText: searchText,
searchFilterColumn: searchColumn,
sortBy,
sortDirection,
sortByColumnType,
Expand Down
41 changes: 41 additions & 0 deletions packages/react-workers/__tests__/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,46 @@ describe('Worker Methods', () => {
.sort((a, b) => b.size_m2 - a.size_m2)
});
});

it('should not filter when searchFilterColumn is provided, but searchFilterText is not', () => {
expect(
getRawFeatures({
searchFilterColumn: Object.keys(sampleGeoJson.features[0].properties)[0],
searchFilterText: null
})
).toEqual({
totalCount: 6,
hasData: true,
rows: sampleGeoJson.features.map((f) => f.properties)
});
});

it('should not filter when searchFilterText is provided, but searchFilterColumn is not', () => {
expect(
getRawFeatures({
searchFilterColumn: null,
searchFilterText: 'any-text'
})
).toEqual({
totalCount: 6,
hasData: true,
rows: sampleGeoJson.features.map((f) => f.properties)
});
});

it('should filter when searchFilterColumn and searchFilterText are provided', () => {
const searchFilterColumn = Object.keys(sampleGeoJson.features[0].properties)[0];

const result = getRawFeatures({
searchFilterColumn,
searchFilterText: sampleGeoJson.features[0].properties[searchFilterColumn]
});

expect(result).toEqual({
totalCount: 1,
hasData: true,
rows: [sampleGeoJson.features.map((f) => f.properties)[0]]
});
});
});
});
28 changes: 23 additions & 5 deletions packages/react-workers/src/workers/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@carto/react-core';
import { InvalidColumnError } from '@carto/react-core/';
import { applySorting } from '../utils/sorting';
import { current } from '@reduxjs/toolkit';

let currentFeatures;
let currentGeoJSON;
Expand Down Expand Up @@ -224,6 +225,8 @@ export function getRange({ filters, filtersLogicalOperator, column }) {
export function getRawFeatures({
filters,
filtersLogicalOperator,
searchFilterColumn,
searchFilterText,
sortBy,
sortByDirection = 'asc',
sortByColumnType,
Expand All @@ -236,11 +239,26 @@ export function getRawFeatures({
let hasData = false;

if (currentFeatures) {
rows = applySorting(getFilteredFeatures(filters, filtersLogicalOperator), {
sortBy,
sortByDirection,
sortByColumnType
});
const filteredFeatures = applySorting(
getFilteredFeatures(filters, filtersLogicalOperator),
{
sortBy,
sortByDirection,
sortByColumnType
}
);

if (searchFilterColumn && searchFilterText) {
jmgaya marked this conversation as resolved.
Show resolved Hide resolved
rows = filteredFeatures.filter(
(row) =>
row[searchFilterColumn] &&
String(row[searchFilterColumn])
.toLowerCase()
.includes(String(searchFilterText).toLowerCase())
);
} else {
rows = filteredFeatures;
}

totalCount = rows.length;
hasData = true;
Expand Down
Loading