From 0cb1a705bc5c22ae1a01d4b52498a9a85e2b877c Mon Sep 17 00:00:00 2001 From: islxyqwe Date: Wed, 19 Jun 2024 17:03:12 +0800 Subject: [PATCH] fix: merge fields filter function to storeStateLib --- .../graphic-walker/src/components/pivotTable/index.tsx | 9 +++++---- packages/graphic-walker/src/models/visSpec.ts | 4 +++- packages/graphic-walker/src/store/storeStateLib.ts | 10 ++++++++++ packages/graphic-walker/src/store/visualSpecStore.ts | 7 ++++--- 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 packages/graphic-walker/src/store/storeStateLib.ts diff --git a/packages/graphic-walker/src/components/pivotTable/index.tsx b/packages/graphic-walker/src/components/pivotTable/index.tsx index b6aedf10..31411731 100644 --- a/packages/graphic-walker/src/components/pivotTable/index.tsx +++ b/packages/graphic-walker/src/components/pivotTable/index.tsx @@ -14,11 +14,12 @@ import { useCompututaion, useVizStore } from '../../store'; import { fold2 } from '../../lib/op/fold'; import { getFieldIdentifier, getSort, getSortedEncoding } from '../../utils'; import { GWGlobalConfig } from '@/vis/theme'; +import { getAllFields, getViewEncodingFields } from '@/store/storeStateLib'; interface PivotTableProps { vizThemeConfig?: IThemeKey | GWGlobalConfig; data: IRow[]; - draggableFieldState: DeepReadonly; + draggableFieldState: DraggableFieldState; visualConfig: IVisualConfigNew; layout: IVisualLayout; disableCollapse?: boolean; @@ -183,9 +184,9 @@ const PivotTable: React.FC = function PivotTableComponent(props setIsLoading(true); appRef.current?.updateRenderStatus('computing'); const groupbyPromises: Promise[] = groupbyCombList.map((dimComb) => { - const viewFilters = draggableFieldState.filters.map((x) => ({ ...x })); - const allFields = [...draggableFieldState.dimensions, ...draggableFieldState.measures]; - const viewFields = [...draggableFieldState.columns, ...draggableFieldState.rows]; + const viewFilters = draggableFieldState.filters; + const allFields = getAllFields(draggableFieldState); + const viewFields = getViewEncodingFields(draggableFieldState, 'table'); const viewMeasures = viewFields.filter((f) => f.analyticType === 'measure'); const sort = getSort(draggableFieldState); const { limit } = visualConfig; diff --git a/packages/graphic-walker/src/models/visSpec.ts b/packages/graphic-walker/src/models/visSpec.ts index 9800c189..23ec266c 100644 --- a/packages/graphic-walker/src/models/visSpec.ts +++ b/packages/graphic-walker/src/models/visSpec.ts @@ -1,4 +1,6 @@ -export const viewEncodingKeys = (geom: string) => { +import { DraggableFieldState } from '..'; + +export const viewEncodingKeys = (geom: string): Exclude[] => { switch (geom) { case 'choropleth': return ['geoId', 'color', 'opacity', 'text', 'details']; diff --git a/packages/graphic-walker/src/store/storeStateLib.ts b/packages/graphic-walker/src/store/storeStateLib.ts new file mode 100644 index 00000000..74754914 --- /dev/null +++ b/packages/graphic-walker/src/store/storeStateLib.ts @@ -0,0 +1,10 @@ +import { viewEncodingKeys } from '@/models/visSpec'; +import { DraggableFieldState, IViewField } from '../interfaces'; + +export function getAllFields(encodings: { dimensions: IViewField[]; measures: IViewField[] }) { + return [...encodings.dimensions, ...encodings.measures]; +} + +export function getViewEncodingFields(encodings: Partial>, geom: string) { + return viewEncodingKeys(geom).flatMap((k) => encodings[k] ?? []); +} diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts index 8987f9bc..d1e1be7f 100644 --- a/packages/graphic-walker/src/store/visualSpecStore.ts +++ b/packages/graphic-walker/src/store/visualSpecStore.ts @@ -52,6 +52,7 @@ import { getSQLItemAnalyticType, parseSQLExpr } from '../lib/sql'; import { IPaintMapAdapter } from '../lib/paint'; import { toChatMessage } from '@/models/chat'; import { viewEncodingKeys } from '@/models/visSpec'; +import { getAllFields, getViewEncodingFields } from './storeStateLib'; const encodingKeys = (Object.keys(emptyEncodings) as (keyof DraggableFieldState)[]).filter((dkey) => !GLOBAL_CONFIG.META_FIELD_KEYS.includes(dkey)); export class VizSpecStore { @@ -153,7 +154,7 @@ export class VizSpecStore { } get allFields() { - return [...this.dimensions, ...this.measures]; + return getAllFields(this); } get config() { @@ -179,7 +180,7 @@ export class VizSpecStore { return result; } - get viewEncodings() { + get viewEncodings(): Partial> { const result: Record = {}; viewEncodingKeys(this.config.geoms[0]).forEach((k) => { result[k] = this.currentEncodings[k]; @@ -188,7 +189,7 @@ export class VizSpecStore { } get viewEncodingFields() { - return viewEncodingKeys(this.config.geoms[0]).flatMap((k) => this.viewEncodings[k]); + return getViewEncodingFields(this.viewEncodings, this.config.geoms[0]); } get viewDimensions() {