Skip to content

Commit e66ccee

Browse files
committed
fixed bug close open connections
1 parent e367454 commit e66ccee

File tree

2 files changed

+116
-31
lines changed

2 files changed

+116
-31
lines changed
Lines changed: 116 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,139 @@
1-
import { SelectWithTextArea } from 'components/auxiliary/general/selectWithTextArea'
2-
import React, { useContext } from 'react'
1+
import { AppEvents, SelectableValue } from '@grafana/data'
2+
import { getAppEvents } from '@grafana/runtime'
3+
import { Button, Icon, LinkButton, Select, TextArea } from '@grafana/ui'
4+
import React, { Fragment, useContext, useEffect, useState } from 'react'
5+
import { closeConnectionService } from 'services/connections/closeConnectionService'
36
import { deleteConnectionByIdService } from 'services/connections/deleteConnectionByIdService'
47
import { getAllConnectionIdsService } from 'services/connections/getAllConnectionsService'
5-
import { getConnectionByIdService } from 'services/connections/getConnectionByIdService'
6-
import { Roles } from 'utils/auxFunctions/auth'
8+
import { openConnectionService } from 'services/connections/openConnectionService'
9+
import { getCurrentUserRole, hasAuth, Roles } from 'utils/auxFunctions/auth'
710
import { StaticContext } from 'utils/context/staticContext'
8-
import { ExtraButtonsConnection } from '../extraButtons'
11+
import { SelectData } from 'utils/interfaces/select'
912

1013
interface Parameters {
1114
path: string
1215
}
1316

14-
export const ListConnections = ({path}: Parameters) => {
17+
export const ListConnections = ({ path }: Parameters) => {
1518

1619
const context = useContext(StaticContext)
20+
const [userRole, setUserRole] = useState<string>(Roles.VIEWER)
21+
const [connections, setConnections] = useState<SelectData[]>([])
22+
const [selected, setSelected] = useState<SelectableValue<any>>()
23+
const appEvents = getAppEvents()
1724

18-
const updateConnections = (setObjects: any, thenFunction?: any) => {
19-
getAllConnectionIdsService(context).then((res: Array<{id: string, name: string}>) => {
20-
setObjects(res.map(({id, name}: {id: string, name: string}) => {
25+
useEffect(() => {
26+
updateConnections()
27+
getCurrentUserRole().then((role: string) => setUserRole(role))
28+
}, [])
29+
30+
useEffect(() => {
31+
if(selected && selected.value && selected.value.hasOwnProperty("id")){
32+
const refreshSelected = connections.find(v => v.label === selected.value.id)
33+
setSelected(refreshSelected)
34+
}
35+
}, [connections])
36+
37+
const updateConnections = () => {
38+
getAllConnectionIdsService(context).then((res: any[]) => {
39+
setConnections(res.map((ar: any) => {
2140
return {
22-
label : name,
23-
value : id
41+
label: ar.name,
42+
value: ar
2443
}
2544
}))
26-
if(thenFunction) {thenFunction()}
2745
}).catch(() => {
2846
console.log("error")
29-
if(thenFunction) {thenFunction()}
3047
})
3148
}
3249

33-
const getConnection = (id: string) => {
34-
return getConnectionByIdService(context, id)
50+
const handleOnClickDelete = () => {
51+
if (selected !== undefined && selected.value) {
52+
deleteConnectionByIdService(context, selected.value)
53+
}
3554
}
3655

37-
const deleteConnection = (id: string) => {
38-
return deleteConnectionByIdService(context, id)
56+
const handleOnClickStatusConnection = () => {
57+
if(selected && selected.value && selected.value.hasOwnProperty("id") && selected.value.hasOwnProperty("connectionStatus")){
58+
const selectedConnection = selected.value
59+
if(selectedConnection.connectionStatus.toLowerCase() === "closed"){
60+
openConnectionService(context, selected.value.id).then(() => {
61+
appEvents.publish({
62+
type: AppEvents.alertSuccess.name,
63+
payload: ["Connection " + selected.value.id + " opened correctly!"]
64+
});
65+
updateConnections()
66+
}).catch((e) => {
67+
console.log(e)
68+
appEvents.publish({
69+
type: AppEvents.alertError.name,
70+
payload: ["Error opening connection " + selected.value.id + ": " + e.message]
71+
});
72+
})
73+
} else {
74+
closeConnectionService(context, selected.value.id).then(() => {
75+
appEvents.publish({
76+
type: AppEvents.alertSuccess.name,
77+
payload: ["Connection " + selected.value.id + " closed correctly!"]
78+
});
79+
updateConnections()
80+
}).catch((e) => {
81+
console.log(e)
82+
appEvents.publish({
83+
type: AppEvents.alertError.name,
84+
payload: ["Error closing connection " + selected.value.id + ": " + e.message]
85+
});
86+
})
87+
}
88+
}
3989
}
40-
41-
return (
42-
<SelectWithTextArea
43-
path={path}
44-
name="connection"
45-
getByIdFunc={getConnection}
46-
getAllFunc={updateConnections}
47-
deleteFunc={deleteConnection}
48-
ExtraButtonComponent={ExtraButtonsConnection}
49-
minRole={Roles.EDITOR}
50-
/>
51-
)
52-
90+
91+
const closeOpenButton = (connection: any) => <Button style={{ marginRight: '10px' }} variant="secondary" onClick={handleOnClickStatusConnection} icon={(connection.connectionStatus.toLowerCase() === "open") ? "lock" : "unlock"}>
92+
{
93+
(connection.connectionStatus.toLowerCase() === "open") ? "Close" : "Open"
94+
}
95+
</Button>
96+
97+
const buttons = (selected !== undefined) ?
98+
<div style={{ display: 'flex', justifyItems: 'flex-start', justifyContent: 'flex-start' }}>
99+
{(selected.value && selected.value.hasOwnProperty("connectionStatus")) ? closeOpenButton(selected.value) : <div></div> }
100+
<Button variant="destructive" icon="trash-alt" onClick={handleOnClickDelete}>Delete</Button>
101+
</div>
102+
: <div></div>
103+
// <LinkButton variant="secondary" href={path + "&id=" + value.value + "&mode=edit"} icon="pen" disabled={showNotification.state !== enumNotification.READY}>Edit</LinkButton>
104+
105+
106+
const createButton = <div style={{ display: 'flex', alignItems: 'center' }}>
107+
<LinkButton variant="primary" href={path + "&mode=create"} icon="plus">
108+
Create new connection
109+
</LinkButton>
110+
</div>
111+
112+
const component = <Fragment>
113+
<div className='row justify-content-between mb-3'>
114+
<div className="col-12 col-sm-12 col-md-5 col-lg-5">
115+
<Select
116+
options={connections}
117+
value={selected}
118+
onChange={v => setSelected(v)}
119+
prefix={<Icon name="search" />}
120+
placeholder="Search"
121+
/>
122+
</div>
123+
<div className='col-12 col-sm-12 col-md-4 col-lg-4' >
124+
{buttons}
125+
</div>
126+
<div className='col-12 col-sm-12 col-md-3 col-lg-3' style={{ display: 'flex', justifyContent: 'flex-end' }}>
127+
{createButton}
128+
</div>
129+
</div>
130+
{(selected !== undefined) ? <TextArea className="mt-3" rows={25} value={(selected) ? JSON.stringify(selected, undefined, 4) : ""} readOnly /> : <div></div>}
131+
</Fragment>
132+
133+
const noAllow = <div style={{ display: 'flex', justifyItems: 'center', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', alignContent: 'center' }}>
134+
<h5>You do not have sufficient permissions to access this content</h5>
135+
</div>
136+
137+
return (hasAuth(userRole, Roles.EDITOR)) ? component : noAllow
138+
53139
}

src/utils/auxFunctions/dittoThing.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const fromMetaToValues = (meta: AppPluginMeta<KeyValue<any>>) => {
4444
if(data.extendedURL !== undefined) {res['ditto_extended_endpoint'] = data.extendedURL}
4545
if(data.agentsURL !== undefined) {res['agent_endpoint'] = data.agentsURL}
4646
if(data.agentsContext !== undefined) {res['agent_context'] = data.agentsContext}
47-
res['user_role'] = 'viewer'
4847
}
4948
return res
5049
}

0 commit comments

Comments
 (0)