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

[Enhancement] Allow empty column when layer created from config #2206

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/reducers/src/vis-state-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ export function createLayerFromConfig(state: VisState, layerConfig: any): Layer
return null;
}
// first validate config against dataset
const {validated, failed} = validateLayersByDatasets(state.datasets, state.layerClasses, [
parsedLayerConfig
]);
const {validated, failed} = validateLayersByDatasets(
state.datasets,
state.layerClasses,
[parsedLayerConfig],
{allowEmptyColumn: true}
);

if (failed?.length || !validated.length) {
// failed
Expand Down Expand Up @@ -657,10 +660,15 @@ export function validateSavedVisualChannels(
return newLayer;
}

type ValidateLayerOption = {
allowEmptyColumn?: boolean;
};

export function validateLayersByDatasets(
datasets: Datasets,
layerClasses: VisState['layerClasses'],
layers: NonNullable<ParsedConfig['visState']>['layers'] = []
layers: NonNullable<ParsedConfig['visState']>['layers'] = [],
options?: ValidateLayerOption
) {
const validated: Layer[] = [];
const failed: NonNullable<ParsedConfig['visState']>['layers'] = [];
Expand All @@ -671,7 +679,12 @@ export function validateLayersByDatasets(
if (layer?.config?.dataId) {
if (datasets[layer.config.dataId]) {
// datasets are already loaded
validateLayer = validateLayerWithData(datasets[layer.config.dataId], layer, layerClasses);
validateLayer = validateLayerWithData(
datasets[layer.config.dataId],
layer,
layerClasses,
options
);
}
}

Expand All @@ -693,9 +706,7 @@ export function validateLayerWithData(
{fields, id: dataId}: KeplerTable,
savedLayer: ParsedLayer,
layerClasses: VisState['layerClasses'],
options: {
allowEmptyColumn?: boolean;
} = {}
options: ValidateLayerOption = {}
): Layer | null {
const {type} = savedLayer;
// layer doesnt have a valid type
Expand Down
38 changes: 38 additions & 0 deletions test/node/reducers/vis-state-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5414,3 +5414,41 @@ test('VisStateUpdater -> prepareStateForDatasetReplace', t => {

t.end();
});

test('VisStateUpdater -> addLayer with empty column', t => {
const initialState = StateWFiles.visState;
const oldLayers = initialState.layers;
let nextState;
t.doesNotThrow(() => {
nextState = reducer(
initialState,
VisStateActions.addLayer({
type: 'point',
id: 'taro-xxx',
config: {
dataId: testCsvDataId
// no column
}
})
);
}, 'should not throw error when add layer with empty column');

t.equal(nextState.layers.length, oldLayers.length + 1, 'should create 1 layer');
const newLayer = nextState.layers[nextState.layers.length - 1];

t.equal(newLayer.id, 'taro-xxx', 'newlayer should have correct id');
t.equal(newLayer.type, 'point', 'newlayer should have correct type');
t.deepEqual(
newLayer.config.columns.lat,
{value: null, fieldIdx: -1},
'newlayer column should be value: null'
);

t.deepEqual(
nextState.layerData[nextState.layerData.length - 1],
{},
'newlayer layerData should be empty'
);

t.end();
});