1+ /* eslint-disable no-undef */
2+ import { Modal , Button , Tabs , Tab } from "react-bootstrap"
3+ import { JsonViewer } from "@cloudoperators/juno-ui-components"
4+ import { projectUrl , objectUrl , vCenterUrl } from "../../shared/object_link_helper"
5+ import React from "react"
6+
7+ import ProjectRoleAssignments from "plugins/identity/app/javascript/widgets/role_assignments/containers/project_role_assignments"
8+ import UserRoleAssignments from "plugins/identity/app/javascript/widgets/role_assignments/containers/user_role_assignments"
9+ import NetworkUsageStats from "plugins/networking/app/javascript/widgets/network_usage_stats/containers/application"
10+ import Asr from "plugins/networking/app/javascript/widgets/asr/application"
11+
12+ import ObjectTopology from "../../topology/containers/object_topology"
13+
14+ export default class ShowSearchObjectModal extends React . Component {
15+ state = {
16+ show : true ,
17+ isFetching : false ,
18+ error : null ,
19+ }
20+
21+ componentDidMount = ( ) => {
22+ // load object if it does not exist
23+ if ( ! this . props . item && this . props . match . params . id ) {
24+ this . setState ( { isFetching : true } , ( ) =>
25+ this . props . load ( this . props . match . params . id ) . catch ( ( error ) => this . setState ( { isFetching : false , error } ) )
26+ )
27+ }
28+ }
29+
30+ UNSAFE_componentWillReceiveProps = ( props ) => {
31+ if ( props . item ) {
32+ this . setState ( { show : true , isFetching : false , error : null } )
33+ }
34+ }
35+
36+ restoreUrl = ( e ) => {
37+ if ( this . state . show ) return
38+
39+ if ( this . props . match && this . props . match . path ) {
40+ const found = this . props . match . path . match ( / ( \/ [ ^ / ] + ) \/ : i d \/ s h o w / )
41+ if ( found ) {
42+ this . props . history . replace ( found [ 1 ] )
43+ return
44+ }
45+ }
46+
47+ this . props . history . goBack ( )
48+ }
49+
50+ hide = ( e ) => {
51+ if ( e ) e . stopPropagation ( )
52+ this . setState ( { show : false } )
53+ }
54+
55+ render ( ) {
56+ const { item, project, aggregates } = this . props
57+ const vcAggregates =
58+ aggregates && aggregates . items ? aggregates . items . filter ( ( a ) => a . name . indexOf ( "vc-" ) === 0 ) : [ ]
59+ const projectLink = projectUrl ( item )
60+ const objectLink = objectUrl ( item )
61+ const vCenterLink = vCenterUrl ( item , vcAggregates )
62+ const found = this . props . location . search . match ( / \? t a b = ( [ ^ & ] + ) / )
63+ let activeTab = found ? found [ 1 ] : null
64+ const isProject = item && item . cached_object_type == "project"
65+ const isUser = item && item . cached_object_type == "user"
66+ const isDomain = item && item . cached_object_type == "domain"
67+ const isRouter = item && item . cached_object_type == "router"
68+ if ( activeTab == "userRoles" && isDomain ) activeTab = "data"
69+
70+ return (
71+ < Modal
72+ show = { this . state . show }
73+ onExited = { this . restoreUrl }
74+ onHide = { this . hide }
75+ dialogClassName = "modal-xl"
76+ aria-labelledby = "contained-modal-title-lg"
77+ >
78+ < Modal . Header closeButton >
79+ < Modal . Title id = "contained-modal-title-lg" >
80+ Show{ " " }
81+ { item && (
82+ < >
83+ { item . cached_object_type } { item . name } ({ item . id } )
84+ </ >
85+ ) }
86+ </ Modal . Title >
87+ </ Modal . Header >
88+ < Modal . Body >
89+ { this . state . isFetching && (
90+ < >
91+ < span className = "spinner" />
92+ Loading...
93+ </ >
94+ ) }
95+ { this . state . error && < span > { this . state . error } </ span > }
96+ { item && (
97+ < Tabs defaultActiveKey = { activeTab || "data" } id = "item_payload" mountOnEnter >
98+ < Tab eventKey = "data" title = "Data" >
99+ < JsonViewer data = { item . payload } expanded = { 1 } />
100+ </ Tab >
101+ { isProject && policy . isAllowed ( "tools:universal_search_role_assignments" ) && (
102+ < Tab eventKey = "userRoles" title = "User Role Assignments" >
103+ < ProjectRoleAssignments projectId = { item . id } projectDomainId = { item . domain_id } type = "user" />
104+ </ Tab >
105+ ) }
106+ { isProject && policy . isAllowed ( "tools:universal_search_role_assignments" ) && (
107+ < Tab eventKey = "groupRoles" title = "Group Role Assignments" >
108+ < ProjectRoleAssignments projectId = { item . id } projectDomainId = { item . domain_id } type = "group" />
109+ </ Tab >
110+ ) }
111+ { isUser && policy . isAllowed ( "tools:universal_search_user_role_assignments" , { user : item } ) && (
112+ < Tab eventKey = "userRoles" title = "User Role Assignments" >
113+ < UserRoleAssignments userId = { item . id } />
114+ </ Tab >
115+ ) }
116+ { ( isProject || isDomain ) && policy . isAllowed ( "tools:universal_search_netstats" ) && (
117+ < Tab eventKey = "networkStats" title = "Network Statistics" >
118+ < NetworkUsageStats scopeId = { item . id } scopeType = { isProject ? "project" : "domain" } />
119+ </ Tab >
120+ ) }
121+ { isRouter && policy . isAllowed ( "tools:universal_search_asr" ) && (
122+ < Tab eventKey = "asr" title = "ASR Info" >
123+ < Asr routerId = { item . id } />
124+ </ Tab >
125+ ) }
126+
127+ { policy . isAllowed ( "tools:universal_search_asr" ) && (
128+ < Tab eventKey = "objectTopology" title = "Topology" >
129+ < ObjectTopology size = { [ 500 , 500 ] } objectId = { item . id } />
130+ </ Tab >
131+ ) }
132+ </ Tabs >
133+ ) }
134+ </ Modal . Body >
135+ < Modal . Footer >
136+ { vCenterLink && (
137+ < a href = { vCenterLink } target = "_blank" className = "btn btn-primary" rel = "noreferrer" >
138+ Switch to VCenter
139+ </ a >
140+ ) }
141+
142+ { objectLink && (
143+ < a href = { objectLink } target = "_blank" className = "btn btn-primary" rel = "noreferrer" >
144+ Show in Elektra
145+ </ a >
146+ ) }
147+
148+ { projectLink && policy . isAllowed ( "tools:switch_to_project" , { project : item } ) && (
149+ < a href = { projectLink } target = "_blank" className = "btn btn-primary" rel = "noreferrer" >
150+ Switch to Project
151+ </ a >
152+ ) }
153+ < Button onClick = { this . hide } > Close</ Button >
154+ </ Modal . Footer >
155+ </ Modal >
156+ )
157+ }
158+ }
0 commit comments