1
1
import styles from './table.module.scss'
2
- import React , { useState , useRef , useContext } from 'react'
2
+ import React , { useState , useContext } from 'react'
3
3
import { Cell , useRowSelect , useTable } from 'react-table'
4
4
import toast from 'react-hot-toast'
5
5
import { DBConnection , DBQueryData , Tab } from '../../../data/models'
6
6
import EditableCell from './editablecell'
7
7
import AddModal from './addmodal'
8
8
import ConfirmModal from '../../widgets/confirmModal'
9
- import { useAppDispatch , useAppSelector } from '../../../redux/hooks'
9
+ import { useAppDispatch } from '../../../redux/hooks'
10
10
import { deleteDBData , setQueryData , updateDBSingleData } from '../../../redux/dataModelSlice'
11
11
import { DBConnType } from '../../../data/defaults'
12
12
import TabContext from '../../layouts/tabcontext'
@@ -41,7 +41,7 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isEditable, showHeader
41
41
[ queryData ]
42
42
)
43
43
44
- const displayColumns = queryData . columns . filter ( col => col !== 'ctid' )
44
+ const displayColumns = dbConnection . type === DBConnType . POSTGRES ? queryData . columns . filter ( col => col !== 'ctid' ) : queryData . columns
45
45
const ctidExists = queryData . columns . length !== displayColumns . length
46
46
47
47
const columns = React . useMemo (
@@ -65,14 +65,21 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isEditable, showHeader
65
65
setEditCell ( [ ] )
66
66
}
67
67
68
- const onSaveCell = async ( ctid : string , columnIdx : string , newValue : string ) => {
68
+ const onSaveCell = async ( rowIdx : number , originalValue : any , columnIdx : string , newValue : string ) => {
69
+ if ( dbConnection . type === DBConnType . MYSQL && queryData . pkeys ?. length === 0 ) {
70
+ return toast . error ( "to perform edit operation primary keys are required on the table!" )
71
+ }
69
72
const columnName = queryData . columns [ parseInt ( columnIdx ) ]
70
- const result = await dispatch ( updateDBSingleData ( { dbConnectionId : dbConnection . id , schemaName : mSchema , name : mName , id : ctid , columnName, newValue, columnIdx } ) ) . unwrap ( )
73
+ const uniqueId = dbConnection . type === DBConnType . POSTGRES ? originalValue [ "0" ] : JSON . stringify ( queryData . pkeys ! . map ( ( pkey ) => ( { [ pkey ] : originalValue [ queryData . columns . findIndex ( x => x === pkey ) ] } ) ) . reduce ( ( ( r , c ) => Object . assign ( r , c ) ) , { } ) )
74
+ const result = await dispatch ( updateDBSingleData ( { dbConnectionId : dbConnection . id , schemaName : mSchema , name : mName , id : uniqueId , columnName, newValue, columnIdx } ) ) . unwrap ( )
71
75
if ( result . success ) {
72
- const rowIdx = queryData ! . rows . findIndex ( x => x [ "0" ] === ctid )
73
76
if ( rowIdx !== - 1 ) {
74
77
const newQueryData : DBQueryData = { ...queryData ! , rows : [ ...queryData ! . rows ] }
75
- newQueryData ! . rows [ rowIdx ] = { ...newQueryData ! . rows [ rowIdx ] , 0 : result . data . ctid }
78
+ if ( dbConnection . type === DBConnType . POSTGRES ) {
79
+ newQueryData ! . rows [ rowIdx ] = { ...newQueryData ! . rows [ rowIdx ] , 0 : result . data . ctid }
80
+ } else {
81
+ newQueryData ! . rows [ rowIdx ] = { ...newQueryData ! . rows [ rowIdx ] }
82
+ }
76
83
newQueryData ! . rows [ rowIdx ] [ columnIdx ] = newValue
77
84
dispatch ( setQueryData ( { data : newQueryData , tabId : activeTab . id } ) )
78
85
} else {
@@ -116,11 +123,16 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isEditable, showHeader
116
123
const newState : any = state // temporary typescript hack
117
124
const selectedRowIds : any = newState . selectedRowIds
118
125
const selectedRows : number [ ] = Object . keys ( selectedRowIds ) . map ( x => parseInt ( x ) )
119
- const selectedCTIDs = rows . filter ( ( _ , i ) => selectedRows . includes ( i ) ) . map ( x => x . original [ '0' ] ) . filter ( x => x )
126
+ const selectedIDs = dbConnection . type === DBConnType . POSTGRES ?
127
+ rows . filter ( ( _ , i ) => selectedRows . includes ( i ) ) . map ( x => x . original [ '0' ] ) . filter ( x => x )
128
+ : rows . filter ( ( _ , i ) => selectedRows . includes ( i ) ) . map ( x => queryData . pkeys ! . map ( ( pkey ) => ( { [ pkey ] : x . original [ queryData . columns . findIndex ( x => x === pkey ) ] } ) ) ) . map ( x => x . reduce ( ( ( r , c ) => Object . assign ( r , c ) ) , { } ) ) . map ( x => JSON . stringify ( x ) )
120
129
121
130
const deleteRows = async ( ) => {
122
- if ( selectedCTIDs . length > 0 ) {
123
- const result = await dispatch ( deleteDBData ( { dbConnectionId : dbConnection . id , schemaName : mSchema , name : mName , selectedIDs : selectedCTIDs } ) ) . unwrap ( )
131
+ if ( dbConnection . type === DBConnType . MYSQL && queryData . pkeys ?. length === 0 ) {
132
+ return toast . error ( "to perform delete operation primary keys are required on the table!" )
133
+ }
134
+ if ( selectedIDs . length > 0 ) {
135
+ const result = await dispatch ( deleteDBData ( { dbConnectionId : dbConnection . id , schemaName : mSchema , name : mName , selectedIDs : selectedIDs } ) ) . unwrap ( )
124
136
if ( result . success ) {
125
137
toast . success ( 'rows deleted' )
126
138
const filteredRows = queryData ! . rows . filter ( ( _ , i ) => ! selectedRows . includes ( i ) )
@@ -150,6 +162,11 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isEditable, showHeader
150
162
}
151
163
onFilterChanged ( filter )
152
164
}
165
+ const onFilterClear = ( ) => {
166
+ let filter : string [ ] | undefined = undefined
167
+ setFilterValue ( [ 'default' , 'default' , '' ] )
168
+ onFilterChanged ( filter )
169
+ }
153
170
154
171
const changeSort = ( newSortIdx : string ) => {
155
172
if ( ! isEditable ) {
@@ -216,11 +233,18 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isEditable, showHeader
216
233
< p className = "control" >
217
234
< button className = "button" onClick = { onFilter } > Filter</ button >
218
235
</ p >
236
+ { ( filterValue [ 0 ] !== 'default' || filterValue [ 1 ] !== 'default' ) && < p className = "control" >
237
+ < button className = "button" onClick = { onFilterClear } >
238
+ < span className = "icon is-small" >
239
+ < i className = "fas fa-circle-xmark" />
240
+ </ span >
241
+ </ button >
242
+ </ p > }
219
243
</ div >
220
244
</ div >
221
245
{ isEditable && < React . Fragment >
222
246
< div className = "column is-3 is-flex is-justify-content-flex-end" >
223
- < button className = "button" disabled = { dbConnection . type === DBConnType . MYSQL || selectedCTIDs . length === 0 } onClick = { ( ) => { setIsDeleting ( true ) } } >
247
+ < button className = "button" disabled = { selectedIDs . length === 0 } onClick = { ( ) => { setIsDeleting ( true ) } } >
224
248
< span className = "icon is-small" >
225
249
< i className = "fas fa-trash" />
226
250
</ span >
0 commit comments