@@ -4,26 +4,34 @@ import { AbstractTableAction } from "stateManagement/AbstractTableAction";
44
55export default class AlterOptionToColumnHandlerAction extends AbstractTableAction < ColumnsState > {
66 handle ( tableActionResponse : TableActionResponse < ColumnsState > ) : TableActionResponse < ColumnsState > {
7- const { view, set, implementation } = tableActionResponse ;
8- implementation . actions . addOptionToColumn = async (
7+ const { view, set, get , implementation } = tableActionResponse ;
8+ implementation . actions . addOptionToColumn = (
99 column : TableColumn ,
1010 option : string ,
1111 backgroundColor : string
1212 ) => {
13- // Save on disk
14- const newOptions = [ ...column . options , { label : option , backgroundColor : backgroundColor } ] ;
15- await view . diskConfig . updateColumnProperties ( column . id , {
16- options : newOptions ,
17- } ) ;
18- // Save on memory
19- set ( ( updater ) => {
20- const optionIndex = updater . columns . findIndex (
21- ( col : TableColumn ) => col . id === column . id
22- ) ;
23- const updatedColumns = [ ...updater . columns ] ;
24- updatedColumns [ optionIndex ] . options = newOptions ;
25- return { columns : updatedColumns } ;
26- } ) ;
13+ // Wrap in a promise of a queue to avoid concurrency issues
14+ const columnIndex = get ( ) . columns . findIndex (
15+ ( col : TableColumn ) => col . id === column . id
16+ ) ;
17+ const memoryColumn = get ( ) . columns [ columnIndex ] ;
18+ // Check if the option already exists
19+ const optionIndex = memoryColumn . options . findIndex ( ( o ) => o . label === option ) ;
20+ // Add the option to the column if it doesn't exist
21+ if ( optionIndex === - 1 ) {
22+ // Save on disk
23+ const newOptions = [ ...memoryColumn . options , { label : option , backgroundColor : backgroundColor } ] ;
24+ view . diskConfig . updateColumnProperties ( column . id , {
25+ options : newOptions ,
26+ } ) ;
27+
28+ // Save on memory
29+ set ( ( updater ) => {
30+ memoryColumn . options = newOptions ;
31+ updater . columns [ columnIndex ] = memoryColumn ;
32+ return { columns : updater . columns } ;
33+ } ) ;
34+ }
2735 }
2836
2937 tableActionResponse . implementation = implementation ;
0 commit comments