@@ -7,6 +7,7 @@ import Component from '@glimmer/component';
77import { action } from '@ember/object' ;
88import { tracked } from '@glimmer/tracking' ;
99import { service } from '@ember/service' ;
10+ import { isEmpty } from '@ember/utils' ;
1011
1112/**
1213 * @module NamespacePicker
@@ -21,6 +22,11 @@ import { service } from '@ember/service';
2122
2223export default class NamespacePicker extends Component {
2324 @service namespace ;
25+ @service router ;
26+ @service store ;
27+
28+ // Show/hide refresh & manage namespaces buttons
29+ @tracked showAfterOptions = false ;
2430
2531 @tracked selected = { } ;
2632 @tracked options = [ ] ;
@@ -30,17 +36,20 @@ export default class NamespacePicker extends Component {
3036 this . loadOptions ( ) ;
3137 }
3238
39+ // TODO make private when converting from js to ts
3340 #matchesPath( option , currentNamespace ) {
3441 // TODO: Revisit. A hardcoded check for "path" & "/path" seems hacky, but it fixes a breaking test:
3542 // "Acceptance | Enterprise | namespaces: it shows nested namespaces if you log in with a namespace starting with a /"
3643 // My assumption is that namespace shouldn't start with a "/", but is this a HVD thing? or is the test outdated?
3744 return option ?. path === currentNamespace ?. path || `/${ option ?. path } ` === currentNamespace ?. path ;
3845 }
3946
47+ // TODO make private when converting from js to ts
4048 #getSelected( options , currentNamespace ) {
4149 return options . find ( ( option ) => this . #matchesPath( option , currentNamespace ) ) ;
4250 }
4351
52+ // TODO make private when converting from js to ts
4453 #getOptions( namespace ) {
4554 /* Each namespace option has 3 properties: { id, path, and label }
4655 * - id: node / namespace name (displayed when the namespace picker is closed)
@@ -64,6 +73,40 @@ export default class NamespacePicker extends Component {
6473 ] ;
6574 }
6675
76+ // TODO make private when converting from js to ts
77+ #getNamespaceLink( location , namespace ) {
78+ const origin = this . #getOrigin( location ) ;
79+
80+ let queryParams = '' ;
81+ if ( ! isEmpty ( namespace . path ) ) {
82+ const encodedNamespace = encodeURIComponent ( namespace . path ) ;
83+ queryParams = `?namespace=${ encodedNamespace } ` ;
84+ }
85+
86+ // The full URL/origin is required so that the page is reloaded.
87+ return `${ origin } /ui/vault/dashboard${ queryParams } ` ;
88+ }
89+
90+ // TODO make private when converting from js to ts
91+ #getOrigin( location ) {
92+ return location . protocol + '//' + location . hostname + ( location . port ? ':' + location . port : '' ) ;
93+ }
94+
95+ @action
96+ async fetchListCapability ( ) {
97+ // TODO: Revist. This logic was carried over from previous component implmenetation.
98+ // When the user doesn't have this capability, shouldn't we just hide the "Manage" button,
99+ // instead of hiding both the "Manage" and "Refresh List" buttons?
100+ try {
101+ await this . store . findRecord ( 'capabilities' , 'sys/namespaces/' ) ;
102+ this . showAfterOptions = true ;
103+ } catch ( e ) {
104+ // If error out on findRecord call it's because you don't have permissions
105+ // and therefore don't have permission to manage namespaces
106+ this . showAfterOptions = false ;
107+ }
108+ }
109+
67110 @action
68111 async loadOptions ( ) {
69112 // TODO: namespace service's findNamespacesForUser will never throw an error.
@@ -72,11 +115,13 @@ export default class NamespacePicker extends Component {
72115
73116 this . options = this . #getOptions( this . namespace ) ;
74117 this . selected = this . #getSelected( this . options , this . namespace ) ;
118+
119+ await this . fetchListCapability ( ) ;
75120 }
76121
77122 @action
78- async onChange ( selectedOption ) {
79- // TODO: redirect to selected namespace
80- this . selected = selectedOption ;
123+ async onChange ( selected ) {
124+ this . selected = selected ;
125+ this . router . transitionTo ( 'vault.cluster.dashboard' , { queryParams : { namespace : selected . path } } ) ;
81126 }
82127}
0 commit comments