From a1b3f00a69d35cc570d9e2ccb33b330d88c75073 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Mon, 14 Dec 2020 15:29:40 +0000 Subject: [PATCH] ui: Show local datacenter by default on first visit (#9377) * Add `Local` property to Datacenters * If you have not previous datacenter, redirect the user to the local dc * Add an `is-local` class to the local datacenter in the DC picker --- .../app/components/hashicorp-consul/index.hbs | 2 +- ui/packages/consul-ui/app/models/dc.js | 1 + ui/packages/consul-ui/app/serializers/dc.js | 4 ++++ ui/packages/consul-ui/app/services/repository/dc.js | 10 ++++++---- .../tests/integration/services/repository/dc-test.js | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ui/packages/consul-ui/app/components/hashicorp-consul/index.hbs b/ui/packages/consul-ui/app/components/hashicorp-consul/index.hbs index 4433ea553..0f5078c3c 100644 --- a/ui/packages/consul-ui/app/components/hashicorp-consul/index.hbs +++ b/ui/packages/consul-ui/app/components/hashicorp-consul/index.hbs @@ -90,7 +90,7 @@ {{#each (sort-by 'Name' dcs) as |item|}} diff --git a/ui/packages/consul-ui/app/models/dc.js b/ui/packages/consul-ui/app/models/dc.js index 65cdd2066..1d8f321d0 100644 --- a/ui/packages/consul-ui/app/models/dc.js +++ b/ui/packages/consul-ui/app/models/dc.js @@ -7,6 +7,7 @@ export const SLUG_KEY = 'Name'; export default class Datacenter extends Model { @attr('string') uid; @attr('string') Name; + @attr('boolean') Local; @attr('boolean', { defaultValue: () => true }) MeshEnabled; } diff --git a/ui/packages/consul-ui/app/serializers/dc.js b/ui/packages/consul-ui/app/serializers/dc.js index 8fdd3ee6a..23780b5ad 100644 --- a/ui/packages/consul-ui/app/serializers/dc.js +++ b/ui/packages/consul-ui/app/serializers/dc.js @@ -1,6 +1,9 @@ +import { inject as service } from '@ember/service'; import Serializer from './application'; export default class DcSerializer extends Serializer { + @service('env') env; + primaryKey = 'Name'; respondForQuery(respond, query) { @@ -14,6 +17,7 @@ export default class DcSerializer extends Serializer { case 'query': return payload.map(item => { return { + Local: this.env.var('CONSUL_DATACENTER_LOCAL') === item, [this.primaryKey]: item, }; }); diff --git a/ui/packages/consul-ui/app/services/repository/dc.js b/ui/packages/consul-ui/app/services/repository/dc.js index 8fdc17879..552a7939c 100644 --- a/ui/packages/consul-ui/app/services/repository/dc.js +++ b/ui/packages/consul-ui/app/services/repository/dc.js @@ -5,8 +5,8 @@ import Error from '@ember/error'; const modelName = 'dc'; export default class DcService extends RepositoryService { - @service('settings') - settings; + @service('settings') settings; + @service('env') env; getModelName() { return modelName; @@ -35,8 +35,10 @@ export default class DcService extends RepositoryService { const settings = this.settings; return Promise.all([name || settings.findBySlug('dc'), items || this.findAll()]).then( ([name, items]) => { - return this.findBySlug(name, items).catch(function() { - const item = get(items, 'firstObject'); + return this.findBySlug(name, items).catch(e => { + const item = + items.findBy('Name', this.env.var('CONSUL_DATACENTER_LOCAL')) || + get(items, 'firstObject'); settings.persist({ dc: get(item, 'Name') }); return item; }); diff --git a/ui/packages/consul-ui/tests/integration/services/repository/dc-test.js b/ui/packages/consul-ui/tests/integration/services/repository/dc-test.js index 6a71f98ff..c6c33c7d1 100644 --- a/ui/packages/consul-ui/tests/integration/services/repository/dc-test.js +++ b/ui/packages/consul-ui/tests/integration/services/repository/dc-test.js @@ -24,7 +24,7 @@ test('findAll returns the correct data for list endpoint', function(assert) { assert.deepEqual( actual, expected(function(payload) { - return payload.map(item => ({ Name: item })); + return payload.map((item, i) => ({ Name: item, Local: i === 0 ? true : false })); }) ); }