@@ -15,6 +15,7 @@ import { moveFile, updateRowFile } from "helpers/VaultManagement";
1515import { randomColor } from "helpers/Colors" ;
1616import { DatabaseColumn , RowDatabaseFields } from "cdm/DatabaseModel" ;
1717import NoteInfo from "services/NoteInfo" ;
18+ import { DataviewService } from "services/DataviewService" ;
1819
1920export function databaseReducer ( state : TableDataType , action : ActionType ) {
2021 LOGGER . debug (
@@ -151,55 +152,41 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
151152 const typeIndex = state . columns . findIndex (
152153 ( column ) => column . id === action . columnId
153154 ) ;
155+ /** Check if type is changed */
156+ if ( state . columns [ typeIndex ] . dataType === action . dataType ) {
157+ return state ;
158+ }
159+ /** If changed, then parsed information */
154160 // Update configuration on disk
155161 state . view . diskConfig . updateColumnProperties ( action . columnId , {
156162 input : action . dataType ,
157163 } ) ;
164+ // Parse data
165+ const parsedData = state . data . map ( ( row : any ) => ( {
166+ ...row ,
167+ [ action . columnId ] : DataviewService . parseLiteral (
168+ row [ action . columnId ] ,
169+ action . dataType // Destination type to parse
170+ ) ,
171+ } ) ) ;
158172 // Update state
159173 switch ( action . dataType ) {
160- case DataTypes . NUMBER :
161- if ( state . columns [ typeIndex ] . dataType === DataTypes . NUMBER ) {
162- return state ;
163- } else {
164- return {
165- ...state ,
166- columns : [
167- ...state . columns . slice ( 0 , typeIndex ) ,
168- { ...state . columns [ typeIndex ] , dataType : action . dataType } ,
169- ...state . columns . slice ( typeIndex + 1 , state . columns . length ) ,
170- ] ,
171- data : state . data . map ( ( row : any ) => ( {
172- ...row ,
173- [ action . columnId ] : isNaN ( row [ action . columnId ] )
174- ? ""
175- : Number . parseInt ( row [ action . columnId ] ) ,
176- } ) ) ,
177- } ;
178- }
179174 case DataTypes . SELECT :
180- if ( state . columns [ typeIndex ] . dataType === DataTypes . SELECT ) {
181- return {
182- ...state ,
183- columns : [
184- ...state . columns . slice ( 0 , typeIndex ) ,
185- { ...state . columns [ typeIndex ] , dataType : action . dataType } ,
186- ...state . columns . slice ( typeIndex + 1 , state . columns . length ) ,
187- ] ,
188- skipReset : true ,
189- } ;
190- } else {
191- const options : any = [ ] ;
192- state . data . forEach ( ( row ) => {
193- if ( row [ action . columnId ] ) {
194- options . push ( {
195- label : row [ action . columnId ] ,
196- backgroundColor : randomColor ( ) ,
197- } ) ;
198- }
199- } ) ;
200- return {
201- ...state ,
202- columns : [
175+ const options : any = [ ] ;
176+ // Generate selected options
177+ parsedData . forEach ( ( row ) => {
178+ if ( row [ action . columnId ] ) {
179+ options . push ( {
180+ label : row [ action . columnId ] ,
181+ backgroundColor : randomColor ( ) ,
182+ } ) ;
183+ }
184+ } ) ;
185+ // Update column to SELECT type
186+ return update ( state , {
187+ skipReset : { $set : true } ,
188+ columns : {
189+ $set : [
203190 ...state . columns . slice ( 0 , typeIndex ) ,
204191 {
205192 ...state . columns [ typeIndex ] ,
@@ -208,39 +195,34 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
208195 } ,
209196 ...state . columns . slice ( typeIndex + 1 , state . columns . length ) ,
210197 ] ,
211- skipReset : true ,
212- } ;
213- }
214- case DataTypes . TEXT :
215- if ( state . columns [ typeIndex ] . dataType === DataTypes . TEXT ) {
216- return state ;
217- } else if ( state . columns [ typeIndex ] . dataType === DataTypes . SELECT ) {
218- return {
219- ...state ,
220- skipReset : true ,
221- columns : [
222- ...state . columns . slice ( 0 , typeIndex ) ,
223- { ...state . columns [ typeIndex ] , dataType : action . dataType } ,
224- ...state . columns . slice ( typeIndex + 1 , state . columns . length ) ,
225- ] ,
226- } ;
227- } else {
228- return {
229- ...state ,
230- skipReset : true ,
231- columns : [
198+ } ,
199+ data : {
200+ $set : parsedData ,
201+ } ,
202+ } ) ;
203+ default :
204+ /**
205+ * GENERIC update change
206+ * Update column dataType & parsed data
207+ * Aplied to:
208+ * - TEXT
209+ * - NUMBER
210+ * - CALENDAR
211+ */
212+ console . log ( "GENERIC update change" ) ;
213+ return update ( state , {
214+ skipReset : { $set : true } ,
215+ columns : {
216+ $set : [
232217 ...state . columns . slice ( 0 , typeIndex ) ,
233218 { ...state . columns [ typeIndex ] , dataType : action . dataType } ,
234219 ...state . columns . slice ( typeIndex + 1 , state . columns . length ) ,
235220 ] ,
236- data : state . data . map ( ( row ) => ( {
237- ...row ,
238- [ action . columnId ] : row [ action . columnId ] + "" ,
239- } ) ) ,
240- } ;
241- }
242- default :
243- return state ;
221+ } ,
222+ data : {
223+ $set : parsedData ,
224+ } ,
225+ } ) ;
244226 }
245227 /**
246228 * Add new column to the table to the left of the column with the given id
0 commit comments