44 ActionTypes ,
55 DataTypes ,
66 MetadataColumns ,
7+ TableColumnsTemplate ,
78 UpdateRowOptions ,
89} from "helpers/Constants" ;
910import { TableColumn , TableDataType , RowDataType } from "cdm/FolderModel" ;
@@ -94,32 +95,47 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
9495 * Update key of column in all rows
9596 */
9697 case ActionTypes . UPDATE_COLUMN_LABEL :
97- const index = state . columns . findIndex (
98+ const update_column_label_index = state . columns . findIndex (
9899 ( column : any ) => column . id === action . columnId
99100 ) ;
100101 // trim label will get a valid yaml key
101102 const update_col_key : string = dbTrim ( action . label ) ;
102103 // Update configuration & row files on disk
103- state . view . diskConfig . updateColumnProperties (
104- action . columnId ,
105- { label : action . label , accessor : update_col_key , key : update_col_key } ,
106- state // Update all rows with new key
104+ state . view . diskConfig . updateColumnProperties ( action . columnId , {
105+ label : action . label ,
106+ accessor : update_col_key ,
107+ key : update_col_key ,
108+ } ) ;
109+ // Once the column is updated, update the rows in case the key is changed
110+ Promise . all (
111+ state . data . map ( async ( row : RowDataType ) => {
112+ updateRowFile (
113+ row . note . getFile ( ) ,
114+ state . columns [ update_column_label_index ] . key ,
115+ update_col_key ,
116+ state ,
117+ UpdateRowOptions . COLUMN_KEY
118+ ) ;
119+ } )
107120 ) ;
108121 // Update state
109122 return {
110123 ...state ,
111124 skipReset : true ,
112125 // Add column visually into the new label
113126 columns : [
114- ...state . columns . slice ( 0 , index ) ,
127+ ...state . columns . slice ( 0 , update_column_label_index ) ,
115128 {
116- ...state . columns [ index ] ,
129+ ...state . columns [ update_column_label_index ] ,
117130 label : action . label ,
118131 id : update_col_key ,
119132 key : update_col_key ,
120133 accessor : update_col_key ,
121134 } ,
122- ...state . columns . slice ( index + 1 , state . columns . length ) ,
135+ ...state . columns . slice (
136+ update_column_label_index + 1 ,
137+ state . columns . length
138+ ) ,
123139 ] ,
124140 // Add data visually into the new label
125141 data : state . data . map ( ( row : RowDataType ) => {
@@ -246,36 +262,25 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
246262 } ;
247263 // Update configuration on disk
248264 state . view . diskConfig . addColumn ( action . columnInfo . name , newLeftColumn ) ;
249-
250- Promise . all (
251- state . data . map ( async ( row : RowDataType ) => {
252- updateRowFile (
253- row . note . getFile ( ) ,
254- newLeftColumn . key ,
255- "" ,
256- state ,
257- UpdateRowOptions . COLUMN_VALUE
258- ) ;
259- } )
260- ) ;
261265 // Update state
262- return {
263- ...state ,
264- skipReset : true ,
265- columns : [
266- ...state . columns . slice ( 0 , leftIndex ) ,
267- {
268- id : newLeftColumn . accessor ,
269- label : newLeftColumn . label ,
270- key : newLeftColumn . key ,
271- accessor : newLeftColumn . accessor ,
272- dataType : DataTypes . TEXT ,
273- created : action . focus && true ,
274- options : [ ] ,
275- } ,
276- ...state . columns . slice ( leftIndex , state . columns . length ) ,
277- ] ,
278- } ;
266+ return update ( state , {
267+ skipReset : { $set : true } ,
268+ columns : {
269+ $set : [
270+ ...state . columns . slice ( 0 , leftIndex ) ,
271+ {
272+ ...TableColumnsTemplate ,
273+ id : newLeftColumn . accessor ,
274+ label : newLeftColumn . label ,
275+ key : newLeftColumn . key ,
276+ accessor : newLeftColumn . accessor ,
277+ position : newLeftColumn . position ,
278+ csvCandidate : true ,
279+ } ,
280+ ...state . columns . slice ( leftIndex , state . columns . length ) ,
281+ ] ,
282+ } ,
283+ } ) ;
279284 /**
280285 * Add new column to the table to the right of the column with the given id
281286 * and save it on disk
@@ -295,36 +300,25 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
295300 // Update configuration on disk
296301 state . view . diskConfig . addColumn ( action . columnInfo . name , newRIghtColumn ) ;
297302
298- Promise . all (
299- state . data . map ( async ( row : RowDataType ) => {
300- updateRowFile (
301- row . note . getFile ( ) ,
302- newRIghtColumn . key ,
303- "" ,
304- state ,
305- UpdateRowOptions . COLUMN_VALUE
306- ) ;
307- } )
308- ) ;
309-
310- return {
311- ...state ,
312- skipReset : true ,
303+ return update ( state , {
304+ skipReset : { $set : true } ,
313305 // Add column visually to the right of the column with the given id
314- columns : [
315- ...state . columns . slice ( 0 , rightIndex + 1 ) ,
316- {
317- id : newRIghtColumn . accessor ,
318- label : newRIghtColumn . label ,
319- key : newRIghtColumn . key ,
320- accessor : newRIghtColumn . accessor ,
321- dataType : DataTypes . TEXT ,
322- created : action . focus && true ,
323- options : [ ] ,
324- } ,
325- ...state . columns . slice ( rightIndex + 1 , state . columns . length ) ,
326- ] ,
327- } ;
306+ columns : {
307+ $set : [
308+ ...state . columns . slice ( 0 , rightIndex + 1 ) ,
309+ {
310+ ...TableColumnsTemplate ,
311+ id : newRIghtColumn . accessor ,
312+ label : newRIghtColumn . label ,
313+ key : newRIghtColumn . key ,
314+ accessor : newRIghtColumn . accessor ,
315+ position : newRIghtColumn . position ,
316+ csvCandidate : true ,
317+ } ,
318+ ...state . columns . slice ( rightIndex + 1 , state . columns . length ) ,
319+ ] ,
320+ } ,
321+ } ) ;
328322 case ActionTypes . DELETE_COLUMN :
329323 const deleteIndex = state . columns . findIndex (
330324 ( column ) => column . id === action . columnId
@@ -343,14 +337,15 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
343337 } )
344338 ) ;
345339 // Update state
346- return {
347- ...state ,
348- skipReset : true ,
349- columns : [
350- ...state . columns . slice ( 0 , deleteIndex ) ,
351- ...state . columns . slice ( deleteIndex + 1 , state . columns . length ) ,
352- ] ,
353- } ;
340+ return update ( state , {
341+ skipReset : { $set : true } ,
342+ columns : {
343+ $set : [
344+ ...state . columns . slice ( 0 , deleteIndex ) ,
345+ ...state . columns . slice ( deleteIndex + 1 , state . columns . length ) ,
346+ ] ,
347+ } ,
348+ } ) ;
354349 case ActionTypes . UPDATE_OPTION_CELL :
355350 // check if this column is configured as a group folder
356351 if ( dbconfig . group_folder_column === action . key ) {
@@ -421,7 +416,19 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
421416 state . view . diskConfig . updateColumnProperties ( action . columnId , {
422417 isInline : action . isInline ,
423418 } ) ;
424- return state ;
419+ const toggleInlineIndex = state . columns . findIndex (
420+ ( column ) => column . id === action . columnId
421+ ) ;
422+
423+ return update ( state , {
424+ columns : {
425+ [ toggleInlineIndex ] : {
426+ $merge : {
427+ isInline : action . isInline ,
428+ } ,
429+ } ,
430+ } ,
431+ } ) ;
425432 default :
426433 LOGGER . warn ( `<=> databaseReducer: unknown action ${ action . type } ` ) ;
427434 }
0 commit comments