2022-03-30 01:36:59 -05:00
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
2023-01-02 00:23:55 -06:00
// Copyright (C) 2013 - 2023, The pgAdmin Development Team
2022-03-30 01:36:59 -05:00
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import React from 'react' ;
import pgAdmin from 'sources/pgadmin' ;
import getApiInstance from 'sources/api_instance' ;
import { makeStyles } from '@material-ui/core/styles' ;
import { Box , Switch } from '@material-ui/core' ;
import { generateCollectionURL } from '../../browser/static/js/node_ajax' ;
import Notify from '../../static/js/helpers/Notifier' ;
import gettext from 'sources/gettext' ;
import 'wcdocker' ;
import PgTable from 'sources/components/PgTable' ;
import Theme from 'sources/Theme' ;
import PropTypes from 'prop-types' ;
import { PgIconButton } from '../../static/js/components/Buttons' ;
2022-04-04 07:12:42 -05:00
import DeleteIcon from '@material-ui/icons/Delete' ;
import DeleteSweepIcon from '@material-ui/icons/DeleteSweep' ;
2023-06-19 04:34:40 -05:00
import DeleteForeverIcon from '@material-ui/icons/DeleteForever' ;
2022-04-05 01:40:51 -05:00
import EmptyPanelMessage from '../../static/js/components/EmptyPanelMessage' ;
2022-04-22 05:12:04 -05:00
import Loader from 'sources/components/Loader' ;
2023-06-19 04:34:40 -05:00
import { evalFunc } from '../../static/js/utils' ;
2022-03-30 01:36:59 -05:00
const useStyles = makeStyles ( ( theme ) => ( {
emptyPanel : {
minHeight : '100%' ,
minWidth : '100%' ,
2022-04-05 01:40:51 -05:00
background : theme . otherVars . emptySpaceBg ,
2022-03-30 01:36:59 -05:00
overflow : 'auto' ,
2022-04-05 01:40:51 -05:00
padding : '8px' ,
display : 'flex' ,
2022-03-30 01:36:59 -05:00
} ,
panelIcon : {
width : '80%' ,
margin : '0 auto' ,
marginTop : '25px !important' ,
position : 'relative' ,
textAlign : 'center' ,
} ,
panelMessage : {
marginLeft : '0.5rem' ,
fontSize : '0.875rem' ,
} ,
searchPadding : {
flex : 2.5
} ,
searchInput : {
flex : 1 ,
margin : '4 0 4 0' ,
borderLeft : 'none' ,
paddingLeft : 5
} ,
propertiesPanel : {
height : '100%'
} ,
autoResizer : {
height : '100% !important' ,
width : '100% !important' ,
background : theme . palette . grey [ 400 ] ,
padding : '8px' ,
2022-04-04 07:12:42 -05:00
overflow : 'hidden !important' ,
overflowX : 'auto !important'
2022-03-30 01:36:59 -05:00
} ,
dropButton : {
2022-08-11 00:19:45 -05:00
marginRight : '8px !important'
2022-03-30 01:36:59 -05:00
} ,
readOnlySwitch : {
opacity : 0.75 ,
'& .MuiSwitch-track' : {
opacity : theme . palette . action . disabledOpacity ,
}
}
} ) ) ;
export function CollectionNodeView ( {
node ,
treeNodeInfo ,
itemNodeData ,
item ,
pgBrowser
} ) {
const classes = useStyles ( ) ;
const [ data , setData ] = React . useState ( [ ] ) ;
const [ infoMsg , setInfoMsg ] = React . useState ( 'Please select an object in the tree view.' ) ;
const [ selectedObject , setSelectedObject ] = React . useState ( [ ] ) ;
2022-04-05 03:10:22 -05:00
const [ reload , setReload ] = React . useState ( false ) ;
2022-04-22 05:12:04 -05:00
const [ loaderText , setLoaderText ] = React . useState ( '' ) ;
2023-03-17 07:13:42 -05:00
const schemaRef = React . useRef ( ) ;
2022-03-30 01:36:59 -05:00
2022-04-28 23:56:24 -05:00
//Reload the collection node on refresh or change in children count
React . useEffect ( ( ) => {
setReload ( ! reload ) ;
} , [ item ? . _children ] ) ;
2022-03-30 01:36:59 -05:00
const [ pgTableColumns , setPgTableColumns ] = React . useState ( [
{
Header : 'properties' ,
accessor : 'Properties' ,
2022-08-11 00:19:45 -05:00
sortable : true ,
2022-05-18 07:19:54 -05:00
resizable : true ,
2022-03-30 01:36:59 -05:00
disableGlobalFilter : false ,
} ,
{
Header : 'value' ,
accessor : 'value' ,
2022-08-11 00:19:45 -05:00
sortable : true ,
2022-05-18 07:19:54 -05:00
resizable : true ,
2022-03-30 01:36:59 -05:00
disableGlobalFilter : false ,
} ,
] ) ;
const getTableSelectedRows = ( selRows ) => {
setSelectedObject ( selRows ) ;
} ;
const onDrop = ( type ) => {
let selRowModels = selectedObject ,
selRows = [ ] ,
selItem = pgBrowser . tree . selected ( ) ,
selectedItemData = selItem ? pgBrowser . tree . itemData ( selItem ) : null ,
selNode = selectedItemData && pgBrowser . Nodes [ selectedItemData . _type ] ,
url = undefined ,
msg = undefined ,
title = undefined ;
if ( selNode && selNode . type && selNode . type == 'coll-constraints' ) {
// In order to identify the constraint type, the type should be passed to the server
selRows = selRowModels . map ( ( row ) => ( {
2022-04-11 07:12:16 -05:00
id : row . original . oid ,
_type : row . original . _type ,
2022-03-30 01:36:59 -05:00
} ) ) ;
} else {
2023-03-17 07:13:42 -05:00
selRows = selRowModels . map ( ( row ) => row . original [ schemaRef . current . idAttribute ] ) ;
2022-03-30 01:36:59 -05:00
}
if ( selRows . length === 0 ) {
Notify . alert (
2023-06-19 04:34:40 -05:00
gettext ( 'Delete Multiple' ) ,
2022-03-30 01:36:59 -05:00
gettext ( 'Please select at least one object to delete.' )
) ;
return ;
}
if ( ! selNode ) return ;
if ( type === 'dropCascade' ) {
url = selNode . generate _url ( selItem , 'delete' ) ;
msg = gettext (
2023-06-19 04:34:40 -05:00
'Are you sure you want to delete all the selected objects and all the objects that depend on them?'
2022-03-30 01:36:59 -05:00
) ;
2023-06-19 04:34:40 -05:00
title = gettext ( 'Delete CASCADE multiple objects?' ) ;
} else if ( type === 'dropForce' ) {
url = selNode . generate _url ( selItem , 'delete' ) ;
msg = gettext (
'Delete databases with the force option will attempt to terminate all the existing connections to the selected databases. Are you sure you want to proceed?'
) ;
title = gettext ( 'Delete FORCE multiple objects?' ) ;
2022-03-30 01:36:59 -05:00
} else {
url = selNode . generate _url ( selItem , 'drop' ) ;
2023-06-19 04:34:40 -05:00
msg = gettext ( 'Are you sure you want to delete all the selected objects?' ) ;
title = gettext ( 'Delete multiple objects?' ) ;
2022-03-30 01:36:59 -05:00
}
const api = getApiInstance ( ) ;
let dropNodeProperties = function ( ) {
2023-06-19 04:34:40 -05:00
setLoaderText ( gettext ( 'Deleting Objects...' ) ) ;
2022-03-30 01:36:59 -05:00
api
. delete ( url , {
data : JSON . stringify ( { ids : selRows } ) ,
contentType : 'application/json; charset=utf-8' ,
} )
. then ( function ( res ) {
2023-06-19 04:34:40 -05:00
setLoaderText ( '' ) ;
2022-03-30 01:36:59 -05:00
if ( res . success == 0 ) {
2023-02-09 22:58:39 -06:00
Notify . alert ( res . errormsg , res . info ) ;
2022-03-30 01:36:59 -05:00
}
2022-04-09 01:05:11 -05:00
pgAdmin . Browser . tree . refresh ( selItem ) ;
2022-04-05 03:10:22 -05:00
setReload ( ! reload ) ;
2022-03-30 01:36:59 -05:00
} )
. catch ( function ( error ) {
2023-06-19 04:34:40 -05:00
setLoaderText ( '' ) ;
2022-04-04 07:12:42 -05:00
Notify . alert (
2023-06-19 04:34:40 -05:00
gettext ( 'Error deleting %s' , selectedItemData . _label . toLowerCase ( ) ) ,
2022-04-09 01:05:11 -05:00
_ . isUndefined ( error . response ) ? error . message : error . response . data . errormsg
2022-03-30 01:36:59 -05:00
) ;
} ) ;
} ;
if ( confirm ) {
Notify . confirm ( title , msg , dropNodeProperties , null ) ;
} else {
dropNodeProperties ( ) ;
}
} ;
React . useEffect ( ( ) => {
if ( node ) {
let nodeObj =
pgAdmin . Browser . Nodes [ itemNodeData ? . _type . replace ( 'coll-' , '' ) ] ;
let url = generateCollectionURL . call ( nodeObj , item , 'properties' ) ;
const api = getApiInstance ( ) ;
let tableColumns = [ ] ;
2022-09-08 04:46:48 -05:00
let column = { } ;
2023-06-19 04:34:40 -05:00
setLoaderText ( gettext ( 'Loading...' ) ) ;
2022-03-30 01:36:59 -05:00
2022-04-04 07:12:42 -05:00
if ( itemNodeData . _type . indexOf ( 'coll-' ) > - 1 && ! _ . isUndefined ( nodeObj . getSchema ) ) {
2023-03-17 07:13:42 -05:00
schemaRef . current = nodeObj . getSchema ? . call ( nodeObj , treeNodeInfo , itemNodeData ) ;
schemaRef . current ? . fields . forEach ( ( field ) => {
2022-03-30 01:36:59 -05:00
if ( node . columns . indexOf ( field . id ) > - 1 ) {
if ( field . label . indexOf ( '?' ) > - 1 ) {
column = {
Header : field . label ,
accessor : field . id ,
2022-08-11 00:19:45 -05:00
sortable : true ,
2022-05-18 07:19:54 -05:00
resizable : true ,
2022-03-30 01:36:59 -05:00
disableGlobalFilter : false ,
2022-04-05 01:40:51 -05:00
minWidth : 0 ,
2022-03-30 01:36:59 -05:00
// eslint-disable-next-line react/display-name
Cell : ( { value } ) => {
return ( < Switch color = "primary" checked = { value } className = { classes . readOnlySwitch } value = { value } readOnly title = { String ( value ) } / > ) ;
}
} ;
} else {
column = {
Header : field . label ,
accessor : field . id ,
2022-08-11 00:19:45 -05:00
sortable : true ,
2022-05-18 07:19:54 -05:00
resizable : true ,
2022-03-30 01:36:59 -05:00
disableGlobalFilter : false ,
2022-04-05 01:40:51 -05:00
minWidth : 0 ,
2022-03-30 01:36:59 -05:00
} ;
}
tableColumns . push ( column ) ;
}
} ) ;
2022-04-04 07:12:42 -05:00
} else {
node . columns . forEach ( ( field ) => {
column = {
Header : field ,
accessor : field ,
2022-08-11 00:19:45 -05:00
sortable : true ,
2022-05-18 07:19:54 -05:00
resizable : true ,
2022-04-04 07:12:42 -05:00
disableGlobalFilter : false ,
2022-04-05 01:40:51 -05:00
minWidth : 0 ,
2022-04-04 07:12:42 -05:00
} ;
tableColumns . push ( column ) ;
} ) ;
2022-03-30 01:36:59 -05:00
}
2022-04-04 07:12:42 -05:00
api ( {
url : url ,
type : 'GET' ,
} )
. then ( ( res ) => {
res . data . forEach ( ( element ) => {
element [ 'icon' ] = '' ;
} ) ;
setPgTableColumns ( tableColumns ) ;
setData ( res . data ) ;
setInfoMsg ( 'No properties are available for the selected object.' ) ;
2022-04-22 05:12:04 -05:00
setLoaderText ( '' ) ;
2022-04-04 07:12:42 -05:00
} )
. catch ( ( err ) => {
Notify . alert (
gettext ( 'Failed to retrieve data from the server.' ) ,
gettext ( err . message )
) ;
} ) ;
2022-03-30 01:36:59 -05:00
}
} , [ itemNodeData , node , item , reload ] ) ;
2022-08-11 00:19:45 -05:00
const CustomHeader = ( ) => {
2023-06-19 04:34:40 -05:00
const canDrop = evalFunc ( node , node . canDrop , itemNodeData , item , treeNodeInfo ) ;
const canDropCascade = evalFunc ( node , node . canDropCascade , itemNodeData , item , treeNodeInfo ) ;
const canDropForce = evalFunc ( node , node . canDropForce , itemNodeData , item , treeNodeInfo ) ;
2022-03-30 01:36:59 -05:00
return (
< Box >
< PgIconButton
className = { classes . dropButton }
2022-04-04 07:12:42 -05:00
icon = { < DeleteIcon / > }
2023-06-19 04:34:40 -05:00
aria - label = "Delete"
title = { gettext ( 'Delete' ) }
2022-03-30 01:36:59 -05:00
onClick = { ( ) => {
onDrop ( 'drop' ) ;
} }
disabled = {
( selectedObject . length > 0 )
2023-06-19 04:34:40 -05:00
? ! canDrop
2022-03-30 01:36:59 -05:00
: true
}
> < / PgIconButton >
2023-06-19 04:34:40 -05:00
{ node . type !== 'coll-database' ? < PgIconButton
2022-03-30 01:36:59 -05:00
className = { classes . dropButton }
2022-04-04 07:12:42 -05:00
icon = { < DeleteSweepIcon / > }
2023-06-19 04:34:40 -05:00
aria - label = "Delete Cascade"
title = { gettext ( 'Delete (Cascade)' ) }
2022-03-30 01:36:59 -05:00
onClick = { ( ) => {
onDrop ( 'dropCascade' ) ;
} }
disabled = {
( selectedObject . length > 0 )
2023-06-19 04:34:40 -05:00
? ! canDropCascade
2022-03-30 01:36:59 -05:00
: true
}
2023-06-19 04:34:40 -05:00
> < / PgIconButton > :
< PgIconButton
className = { classes . dropButton }
icon = { < DeleteForeverIcon / > }
aria - label = "Delete Force"
title = { gettext ( 'Delete (Force)' ) }
onClick = { ( ) => {
onDrop ( 'dropForce' ) ;
} }
disabled = {
( selectedObject . length > 0 )
? ! canDropForce
: true
}
> < / PgIconButton > }
2022-03-30 01:36:59 -05:00
< / Box > ) ;
} ;
return (
2022-04-04 07:12:42 -05:00
< Theme className = 'obj_properties' >
2023-06-19 04:34:40 -05:00
< Loader message = { loaderText } / >
2022-03-30 01:36:59 -05:00
< Box className = { classes . propertiesPanel } >
{ data . length > 0 ?
(
< PgTable
2022-04-04 07:12:42 -05:00
isSelectRow = { ! ( 'catalog' in treeNodeInfo ) && ( itemNodeData . label !== 'Catalogs' ) && _ . isUndefined ( node ? . canSelect ) }
2022-08-11 00:19:45 -05:00
CustomHeader = { CustomHeader }
2022-03-30 01:36:59 -05:00
className = { classes . autoResizer }
columns = { pgTableColumns }
data = { data }
type = { 'panel' }
isSearch = { false }
getSelectedRows = { getTableSelectedRows }
/ >
)
:
(
< div className = { classes . emptyPanel } >
2022-05-30 02:28:24 -05:00
{ loaderText ? ( < Loader message = { loaderText } / > ) :
2022-04-22 05:12:04 -05:00
< EmptyPanelMessage text = { gettext ( infoMsg ) } / >
}
2022-03-30 01:36:59 -05:00
< / div >
)
}
< / Box >
< / Theme >
) ;
}
CollectionNodeView . propTypes = {
node : PropTypes . func ,
itemData : PropTypes . object ,
itemNodeData : PropTypes . object ,
treeNodeInfo : PropTypes . object ,
item : PropTypes . object ,
pgBrowser : PropTypes . object ,
preferences : PropTypes . object ,
sid : PropTypes . number ,
did : PropTypes . number ,
row : PropTypes . object ,
serverConnected : PropTypes . bool ,
value : PropTypes . bool ,
} ;