@@ -475,14 +475,14 @@ export const dataFormulatorSlice = createSlice({
475
475
updateChartType : ( state , action : PayloadAction < { chartId : string , chartType : string } > ) => {
476
476
let chartId = action . payload . chartId ;
477
477
let chartType = action . payload . chartType ;
478
- state . charts = state . charts . map ( chart => {
479
- if ( chart . id == chartId ) {
480
- return adaptChart ( chart , getChartTemplate ( chartType ) as ChartTemplate ) ;
481
- } else {
482
- return chart
483
- }
484
- } )
478
+
479
+ let chart = dfSelectors . getAllCharts ( state ) . find ( c => c . id == chartId ) ;
480
+ if ( chart ) {
481
+ chart = adaptChart ( chart , getChartTemplate ( chartType ) as ChartTemplate ) ;
482
+ dfSelectors . replaceChart ( state , chart ) ;
483
+ }
485
484
} ,
485
+
486
486
updateTableRef : ( state , action : PayloadAction < { chartId : string , tableRef : string } > ) => {
487
487
let chartId = action . payload . chartId ;
488
488
let tableRef = action . payload . tableRef ;
@@ -494,57 +494,25 @@ export const dataFormulatorSlice = createSlice({
494
494
}
495
495
} )
496
496
} ,
497
- replaceTable : ( state , action : PayloadAction < { chartId : string , table : DictTable } > ) => {
498
- let chartId = action . payload . chartId ;
499
- let chart = state . charts . find ( c => c . id == chartId ) as Chart ;
500
- let table = action . payload . table ;
501
- let allCharts = dfSelectors . getAllCharts ( state ) ;
502
- let currentTableRef = getDataTable ( chart , state . tables , allCharts , state . conceptShelfItems ) . id ;
503
- state . charts = state . charts . map ( c => {
504
- if ( c . id == chartId ) {
505
- return { ...c , tableRef : table . id }
506
- } else {
507
- return c
508
- }
509
- } )
510
-
511
- if ( ! state . charts . some ( c => c . id != chartId && getDataTable ( c , state . tables , allCharts , state . conceptShelfItems ) . id == currentTableRef ) ) {
512
- state . tables = [ ...state . tables . filter ( t => t . id != currentTableRef ) , table ] ;
513
- } else {
514
- state . tables = [ ...state . tables , table ] ;
515
- }
516
- } ,
517
- updateChartEncodingMap : ( state , action : PayloadAction < { chartId : string , encodingMap : EncodingMap } > ) => {
518
- let chartId = action . payload . chartId ;
519
- let encodingMap = action . payload . encodingMap ;
520
- state . charts = state . charts . map ( c => {
521
- if ( c . id == chartId ) {
522
- return { ...c , encodingMap : encodingMap }
523
- } else {
524
- return c
525
- }
526
- } )
527
- } ,
528
497
updateChartEncoding : ( state , action : PayloadAction < { chartId : string , channel : Channel , encoding : EncodingItem } > ) => {
529
498
let chartId = action . payload . chartId ;
530
499
let channel = action . payload . channel ;
531
500
let encoding = action . payload . encoding ;
532
- let chart = state . charts . find ( chart => chart . id == chartId ) ;
501
+ let chart = dfSelectors . getAllCharts ( state ) . find ( c => c . id == chartId ) ;
533
502
if ( chart ) {
534
- //TODO: check this, finding reference and directly update??
535
- ( state . charts . find ( chart => chart . id == chartId ) as Chart ) . encodingMap [ channel ] = encoding ;
503
+ chart . encodingMap [ channel ] = encoding ;
536
504
}
537
505
} ,
538
506
updateChartEncodingProp : ( state , action : PayloadAction < { chartId : string , channel : Channel , prop : string , value : any } > ) => {
539
507
let chartId = action . payload . chartId ;
540
508
let channel = action . payload . channel ;
541
509
let prop = action . payload . prop ;
542
510
let value = action . payload . value ;
543
- let chart = state . charts . find ( chart => chart . id == chartId ) ;
511
+ let chart = dfSelectors . getAllCharts ( state ) . find ( c => c . id == chartId ) ;
544
512
545
513
if ( chart ) {
546
514
//TODO: check this, finding reference and directly update??
547
- let encoding = ( state . charts . find ( chart => chart . id == chartId ) as Chart ) . encodingMap [ channel ] ;
515
+ let encoding = chart . encodingMap [ channel ] ;
548
516
if ( prop == 'fieldID' ) {
549
517
encoding . fieldID = value ;
550
518
@@ -571,7 +539,7 @@ export const dataFormulatorSlice = createSlice({
571
539
let channel1 = action . payload . channel1 ;
572
540
let channel2 = action . payload . channel2 ;
573
541
574
- let chart = state . charts . find ( chart => chart . id == chartId )
542
+ let chart = dfSelectors . getAllCharts ( state ) . find ( c => c . id == chartId ) ;
575
543
if ( chart ) {
576
544
let enc1 = chart . encodingMap [ channel1 ] ;
577
545
let enc2 = chart . encodingMap [ channel2 ] ;
@@ -601,8 +569,9 @@ export const dataFormulatorSlice = createSlice({
601
569
} ,
602
570
deleteConceptItemByID : ( state , action : PayloadAction < string > ) => {
603
571
let conceptID = action . payload ;
572
+ let allCharts = dfSelectors . getAllCharts ( state ) ;
604
573
// remove concepts from encoding maps
605
- if ( state . charts . some ( chart => chart . saved
574
+ if ( allCharts . some ( chart => chart . saved
606
575
&& Object . entries ( chart . encodingMap ) . some ( ( [ channel , encoding ] ) => encoding . fieldID && conceptID == encoding . fieldID ) ) ) {
607
576
console . log ( "cannot delete!" )
608
577
} else {
@@ -620,7 +589,7 @@ export const dataFormulatorSlice = createSlice({
620
589
}
621
590
state . conceptShelfItems = state . conceptShelfItems . filter ( f => f . id != conceptID ) ;
622
591
623
- for ( let chart of state . charts ) {
592
+ for ( let chart of allCharts ) {
624
593
for ( let [ channel , encoding ] of Object . entries ( chart . encodingMap ) ) {
625
594
if ( encoding . fieldID && conceptID == encoding . fieldID ) {
626
595
// clear the encoding
@@ -631,14 +600,15 @@ export const dataFormulatorSlice = createSlice({
631
600
}
632
601
} ,
633
602
batchDeleteConceptItemByID : ( state , action : PayloadAction < string [ ] > ) => {
603
+ let allCharts = dfSelectors . getAllCharts ( state ) ;
634
604
for ( let conceptID of action . payload ) {
635
605
// remove concepts from encoding maps
636
- if ( state . charts . some ( chart => chart . saved
606
+ if ( allCharts . some ( chart => chart . saved
637
607
&& Object . entries ( chart . encodingMap ) . some ( ( [ channel , encoding ] ) => encoding . fieldID && conceptID == encoding . fieldID ) ) ) {
638
608
console . log ( "cannot delete!" )
639
609
} else {
640
610
state . conceptShelfItems = state . conceptShelfItems . filter ( field => field . id != conceptID ) ;
641
- for ( let chart of state . charts ) {
611
+ for ( let chart of allCharts ) {
642
612
for ( let [ channel , encoding ] of Object . entries ( chart . encodingMap ) ) {
643
613
if ( encoding . fieldID && conceptID == encoding . fieldID ) {
644
614
// clear the encoding
@@ -669,7 +639,7 @@ export const dataFormulatorSlice = createSlice({
669
639
} ,
670
640
clearUnReferencedCustomConcepts : ( state ) => {
671
641
let fieldNamesFromTables = state . tables . map ( t => t . names ) . flat ( ) ;
672
- let fieldIdsReferredByCharts = state . charts . map ( c => Object . values ( c . encodingMap ) . map ( enc => enc . fieldID ) . filter ( fid => fid != undefined ) as string [ ] ) . flat ( ) ;
642
+ let fieldIdsReferredByCharts = dfSelectors . getAllCharts ( state ) . map ( c => Object . values ( c . encodingMap ) . map ( enc => enc . fieldID ) . filter ( fid => fid != undefined ) as string [ ] ) . flat ( ) ;
673
643
674
644
state . conceptShelfItems = state . conceptShelfItems . filter ( field => ! ( field . source == "custom"
675
645
&& ! ( fieldNamesFromTables . includes ( field . name ) || fieldIdsReferredByCharts . includes ( field . id ) ) ) )
@@ -820,6 +790,19 @@ export const dfSelectors = {
820
790
return [ ...userCharts , ...triggerCharts ] ;
821
791
}
822
792
) ,
793
+
794
+ replaceChart : ( state : DataFormulatorState , chart : Chart ) => {
795
+ if ( state . charts . find ( c => c . id == chart . id ) ) {
796
+ // chart is from charts
797
+ state . charts = state . charts . map ( c => c . id == chart . id ? chart : c ) ;
798
+ } else {
799
+ // chart is from tables
800
+ let table = state . tables . find ( t => t . derive ?. trigger ?. chart ?. id == chart . id ) as DictTable ;
801
+ if ( table . derive ?. trigger ) {
802
+ table . derive = { ...table . derive , trigger : { ...table . derive ?. trigger , chart : chart } } ;
803
+ }
804
+ }
805
+ } ,
823
806
}
824
807
825
808
// derived field: extra all field items from the table
0 commit comments