Skip to content

Commit

Permalink
Revert "Prevent flicker on Getting Started page (elastic#11826)"
Browse files Browse the repository at this point in the history
This reverts commit c4b3ade.
  • Loading branch information
ycombinator committed Jun 3, 2017
1 parent 4aee053 commit 6debe7f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { uiModules } from 'ui/modules';
import uiChrome from 'ui/chrome';
import 'ui/getting_started/opt_out_directive';
import { GettingStartedRegistryProvider } from 'ui/getting_started/registry';
import { GETTING_STARTED_REGISTRY_TYPES } from 'ui/getting_started/constants';
Expand Down Expand Up @@ -31,6 +32,12 @@ app.directive('gettingStarted', function ($injector) {
controllerAs: 'gettingStarted',
controller: class GettingStartedController {
constructor() {
if (this.hasOptedOut()) {
uiChrome.setVisible(true);
} else {
uiChrome.setVisible(false);
}

const registeredTopMessages = registry.byType[GETTING_STARTED_REGISTRY_TYPES.TOP_MESSAGE] || [];
this.topMessages = registeredTopMessages.map(item => item.template);

Expand Down

This file was deleted.

158 changes: 67 additions & 91 deletions src/core_plugins/getting_started/public/lib/add_setup_work.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,78 @@ import uiRoutes, { WAIT_FOR_URL_CHANGE_TOKEN } from 'ui/routes';
import uiChrome from 'ui/chrome';
import { Notifier } from 'ui/notify/notifier';
import { IndexPatternsGetIdsProvider } from 'ui/index_patterns/_get_ids';
import KbnUrlProvider from 'ui/url';
import { hasOptedOutOfGettingStarted, optOutOfGettingStarted } from 'ui/getting_started/opt_out_helpers';

import {
GETTING_STARTED_ROUTE,
CREATE_INDEX_PATTERN_ROUTE
} from './constants';

function handleExistingIndexPatternsScenario(indexPatterns, currentRoute, config) {
// If index patterns exist, we're not going to show the user the Getting Started page.
// So we can show the chrome again at this point.
uiChrome.setVisible(true);

// The user need not see the Getting Started page, so opt them out of it
optOutOfGettingStarted();

// Some routes require a default index pattern to be present. If we're
// NOT on such a route, there's nothing more to do; send the user on their way
if (!currentRoute.requireDefaultIndex) {
return;
}

// Otherwise, check if we have a default index pattern
let defaultIndexPattern = config.get('defaultIndex');

// If we don't have an default index pattern, make the first index pattern the
// default one
if (!Boolean(defaultIndexPattern)) {
defaultIndexPattern = indexPatterns[0];
config.set('defaultIndex', defaultIndexPattern);
}

// At this point, we have a default index pattern and are all set!
return;
}

function handleGettingStartedOptedOutScenario(currentRoute, kbnUrl) {
// If the user has opted out of the Getting Started page, we're not going to show them that page.
// So we can show the chrome again at this point.
uiChrome.setVisible(true);

// Some routes require a default index pattern to be present. If we're
// NOT on such a route, there's nothing more to do; send the user on their way
if (!currentRoute.requireDefaultIndex) {
return;
}

// Otherwise, redirect the user to the index pattern creation page with
// a notification about creating an index pattern
const notify = new Notifier({
location: 'Index Patterns'
uiRoutes
.addSetupWork(function gettingStartedGateCheck(Private, $injector) {
const getIds = Private(IndexPatternsGetIdsProvider);
const config = $injector.get('config');
const kbnUrl = $injector.get('kbnUrl');
const $route = $injector.get('$route');

const currentRoute = get($route, 'current.$$route');

return getIds()
.then(indexPatterns => {
const indexPatternsExist = Array.isArray(indexPatterns) && indexPatterns.length > 0;
const isOnGettingStartedPage = get(currentRoute, 'originalPath') === GETTING_STARTED_ROUTE;

if (indexPatternsExist) {

// The user need not see the Getting Started page, so opt them out of it
optOutOfGettingStarted();

// Some routes require a default index pattern to be present. If we're
// NOT on such a route, there's nothing more to do; send the user on their way
if (!currentRoute.requireDefaultIndex) {
return;
}

// Otherwise, check if we have a default index pattern
let defaultIndexPattern = config.get('defaultIndex');

// If we don't have an default index pattern, make the first index pattern the
// default one
if (!Boolean(defaultIndexPattern)) {
defaultIndexPattern = indexPatterns[0];
config.set('defaultIndex', defaultIndexPattern);
}

// At this point, we have a default index pattern and are all set!
return;
}

// At this point, no index patterns exist.

// If the user has explicitly opted out of the Getting Started page
if (hasOptedOutOfGettingStarted()) {

// Some routes require a default index pattern to be present. If we're
// NOT on such a route, there's nothing more to do; send the user on their way
if (!currentRoute.requireDefaultIndex) {
return;
}

// Otherwise, redirect the user to the index pattern creation page with
// a notification about creating an index pattern
const notify = new Notifier({
location: 'Index Patterns'
});
notify.error('Please create a new index pattern');
kbnUrl.change(CREATE_INDEX_PATTERN_ROUTE);
throw WAIT_FOR_URL_CHANGE_TOKEN;
}

// Redirect the user to the Getting Started page (unless they are on it already)
if (!isOnGettingStartedPage) {
uiChrome.setVisible(false);
kbnUrl.change(GETTING_STARTED_ROUTE);
throw WAIT_FOR_URL_CHANGE_TOKEN;
}
});
});
notify.error('Please create a new index pattern');

kbnUrl.change(CREATE_INDEX_PATTERN_ROUTE);
throw WAIT_FOR_URL_CHANGE_TOKEN;
}

function showGettingStartedPage(kbnUrl, isOnGettingStartedPage) {
// Redirect the user to the Getting Started page (unless they are on it already)
if (!isOnGettingStartedPage) {
kbnUrl.change(GETTING_STARTED_ROUTE);
throw WAIT_FOR_URL_CHANGE_TOKEN;
}
}

/*
* This function is exported for unit testing
*/
export function gettingStartedGateCheck(getIds, kbnUrl, config, $route) {
const currentRoute = get($route, 'current.$$route');
const isOnGettingStartedPage = get(currentRoute, 'originalPath') === GETTING_STARTED_ROUTE;

return getIds()
.then(indexPatterns => {
const indexPatternsExist = Array.isArray(indexPatterns) && indexPatterns.length > 0;

if (indexPatternsExist) {
return handleExistingIndexPatternsScenario(indexPatterns, currentRoute, config);
}

if (hasOptedOutOfGettingStarted()) {
return handleGettingStartedOptedOutScenario(currentRoute, kbnUrl);
}

return showGettingStartedPage(kbnUrl, isOnGettingStartedPage);
});
}

// Start out with the chrome not being shown to prevent a flicker by
// hiding it later
uiChrome.setVisible(false);
uiRoutes.addSetupWork((Private, $injector) => {
const getIds = Private(IndexPatternsGetIdsProvider);
const kbnUrl = Private(KbnUrlProvider);
const config = $injector.get('config');
const $route = $injector.get('$route');
return gettingStartedGateCheck(getIds, kbnUrl, config, $route);
});
11 changes: 3 additions & 8 deletions src/ui/public/getting_started/opt_out_helpers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import uiChrome from 'ui/chrome';
import { GETTING_STARTED_OPT_OUT_FLAG } from './constants';

export function hasOptedOutOfGettingStarted() {
return Boolean(window.localStorage.getItem(GETTING_STARTED_OPT_OUT_FLAG));
return window.localStorage.getItem(GETTING_STARTED_OPT_OUT_FLAG) || false;
}

export function optOutOfGettingStarted() {
window.localStorage.setItem(GETTING_STARTED_OPT_OUT_FLAG, true);
}

/**
* This function is intended for unit testing
*/
export function undoOptOutOfGettingStarted() {
window.localStorage.removeItem(GETTING_STARTED_OPT_OUT_FLAG);
uiChrome.setVisible(true);
}
Loading

0 comments on commit 6debe7f

Please sign in to comment.