|
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' |
3 | 6 | import { deleteConnectionByIdService } from 'services/connections/deleteConnectionByIdService'
|
4 | 7 | 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' |
7 | 10 | import { StaticContext } from 'utils/context/staticContext'
|
8 |
| -import { ExtraButtonsConnection } from '../extraButtons' |
| 11 | +import { SelectData } from 'utils/interfaces/select' |
9 | 12 |
|
10 | 13 | interface Parameters {
|
11 | 14 | path: string
|
12 | 15 | }
|
13 | 16 |
|
14 |
| -export const ListConnections = ({path}: Parameters) => { |
| 17 | +export const ListConnections = ({ path }: Parameters) => { |
15 | 18 |
|
16 | 19 | 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() |
17 | 24 |
|
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) => { |
21 | 40 | return {
|
22 |
| - label : name, |
23 |
| - value : id |
| 41 | + label: ar.name, |
| 42 | + value: ar |
24 | 43 | }
|
25 | 44 | }))
|
26 |
| - if(thenFunction) {thenFunction()} |
27 | 45 | }).catch(() => {
|
28 | 46 | console.log("error")
|
29 |
| - if(thenFunction) {thenFunction()} |
30 | 47 | })
|
31 | 48 | }
|
32 | 49 |
|
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 | + } |
35 | 54 | }
|
36 | 55 |
|
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 | + } |
39 | 89 | }
|
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 | + |
53 | 139 | }
|
0 commit comments