Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NP] Get rid of usage redirectWhenMissing service #59777

Merged
merged 18 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export { npSetup, npStart } from 'ui/new_platform';
export { subscribeWithScope } from 'ui/utils/subscribe_with_scope';
export { KbnUrl } from 'ui/url/kbn_url';
// @ts-ignore
export { KbnUrlProvider, RedirectWhenMissingProvider } from 'ui/url/index';
export { KbnUrlProvider } from 'ui/url/index';
export { IInjector } from 'ui/chrome';
export { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
KbnUrlProvider,
PrivateProvider,
PromiseServiceCreator,
RedirectWhenMissingProvider,
} from '../legacy_imports';
// @ts-ignore
import { initDashboardApp } from './legacy_app';
Expand Down Expand Up @@ -146,8 +145,7 @@ function createLocalIconModule() {
function createLocalKbnUrlModule() {
angular
.module('app/dashboard/KbnUrl', ['app/dashboard/Private', 'ngRoute'])
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider))
.service('redirectWhenMissing', (Private: IPrivate) => Private(RedirectWhenMissingProvider));
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider));
}

function createLocalConfigModule(core: AppMountContext['core']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { initDashboardAppDirective } from './dashboard_app';
import { createDashboardEditUrl, DashboardConstants } from './dashboard_constants';
import {
createKbnUrlStateStorage,
redirectWhenMissing,
InvalidJSONProperty,
SavedObjectNotFound,
} from '../../../../../../plugins/kibana_utils/public';
Expand Down Expand Up @@ -136,7 +137,7 @@ export function initDashboardApp(app, deps) {
});
},
resolve: {
dash: function($rootScope, $route, redirectWhenMissing, kbnUrl, history) {
dash: function($rootScope, $route, kbnUrl, history) {
return ensureDefaultIndexPattern(deps.core, deps.data, $rootScope, kbnUrl).then(() => {
const savedObjectsClient = deps.savedObjectsClient;
const title = $route.current.params.title;
Expand Down Expand Up @@ -171,14 +172,18 @@ export function initDashboardApp(app, deps) {
controller: createNewDashboardCtrl,
requireUICapability: 'dashboard.createNew',
resolve: {
dash: function(redirectWhenMissing, $rootScope, kbnUrl) {
dash: function($rootScope, kbnUrl, history) {
return ensureDefaultIndexPattern(deps.core, deps.data, $rootScope, kbnUrl)
.then(() => {
return deps.savedDashboards.get();
})
.catch(
redirectWhenMissing({
dashboard: DashboardConstants.LANDING_PAGE_PATH,
history,
mapping: {
dashboard: DashboardConstants.LANDING_PAGE_PATH,
},
toastNotifications: deps.core.notifications.toasts,
})
);
},
Expand All @@ -189,7 +194,7 @@ export function initDashboardApp(app, deps) {
template: dashboardTemplate,
controller: createNewDashboardCtrl,
resolve: {
dash: function($rootScope, $route, redirectWhenMissing, kbnUrl, history) {
dash: function($rootScope, $route, kbnUrl, history) {
const id = $route.current.params.id;

return ensureDefaultIndexPattern(deps.core, deps.data, $rootScope, kbnUrl)
Expand All @@ -207,7 +212,7 @@ export function initDashboardApp(app, deps) {
.catch(error => {
// A corrupt dashboard was detected (e.g. with invalid JSON properties)
if (error instanceof InvalidJSONProperty) {
deps.toastNotifications.addDanger(error.message);
deps.core.notifications.toasts.addDanger(error.message);
sulemanof marked this conversation as resolved.
Show resolved Hide resolved
kbnUrl.redirect(DashboardConstants.LANDING_PAGE_PATH);
return;
}
Expand All @@ -221,7 +226,7 @@ export function initDashboardApp(app, deps) {
pathname: DashboardConstants.CREATE_NEW_DASHBOARD_URL,
});

deps.toastNotifications.addWarning(
deps.core.notifications.toasts.addWarning(
i18n.translate('kbn.dashboard.urlWasRemovedInSixZeroWarningMessage', {
defaultMessage:
'The url "dashboard/create" was removed in 6.0. Please update your bookmarks.',
Expand All @@ -234,7 +239,11 @@ export function initDashboardApp(app, deps) {
})
.catch(
redirectWhenMissing({
dashboard: DashboardConstants.LANDING_PAGE_PATH,
history,
mapping: {
dashboard: DashboardConstants.LANDING_PAGE_PATH,
},
toastNotifications: deps.core.notifications.toasts,
})
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { CoreStart, LegacyCoreStart, IUiSettingsClient } from 'kibana/public';
// @ts-ignore
import { StateManagementConfigProvider } from 'ui/state_management/config_provider';
// @ts-ignore
import { KbnUrlProvider, RedirectWhenMissingProvider } from 'ui/url';
import { KbnUrlProvider } from 'ui/url';
import { DataPublicPluginStart } from '../../../../../plugins/data/public';
import { Storage } from '../../../../../plugins/kibana_utils/public';
import { NavigationPublicPluginStart as NavigationStart } from '../../../../../plugins/navigation/public';
Expand Down Expand Up @@ -173,8 +173,7 @@ export function initializeInnerAngularModule(
function createLocalKbnUrlModule() {
angular
.module('discoverKbnUrl', ['discoverPrivate', 'ngRoute'])
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider))
.service('redirectWhenMissing', (Private: IPrivate) => Private(RedirectWhenMissingProvider));
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider));
}

function createLocalConfigModule(uiSettings: IUiSettingsClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export { subscribeWithScope } from 'ui/utils/subscribe_with_scope';
// @ts-ignore
export { timezoneProvider } from 'ui/vis/lib/timezone';
export { tabifyAggResponse } from '../../../data/public';
export { unhashUrl } from '../../../../../plugins/kibana_utils/public';
export { unhashUrl, redirectWhenMissing } from '../../../../../plugins/kibana_utils/public';
export {
ensureDefaultIndexPattern,
formatMsg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { debounceTime } from 'rxjs/operators';
import moment from 'moment';
import dateMath from '@elastic/datemath';
import { i18n } from '@kbn/i18n';
import { createHashHistory } from 'history';
import { getState, splitState } from './discover_state';

import { RequestAdapter } from '../../../../../../../plugins/inspector/public';
Expand Down Expand Up @@ -51,6 +52,7 @@ import {
tabifyAggResponse,
getAngularModule,
ensureDefaultIndexPattern,
redirectWhenMissing,
} from '../../kibana_services';

const {
Expand Down Expand Up @@ -86,6 +88,8 @@ const fetchStatuses = {

const app = getAngularModule();

app.factory('history', () => createHashHistory());
kertal marked this conversation as resolved.
Show resolved Hide resolved

app.config($routeProvider => {
const defaults = {
requireDefaultIndex: true,
Expand Down Expand Up @@ -113,10 +117,10 @@ app.config($routeProvider => {
template: indexTemplate,
reloadOnSearch: false,
resolve: {
savedObjects: function(redirectWhenMissing, $route, kbnUrl, Promise, $rootScope) {
savedObjects: function($route, kbnUrl, Promise, $rootScope, history) {
const savedSearchId = $route.current.params.id;
return ensureDefaultIndexPattern(core, data, $rootScope, kbnUrl).then(() => {
const { appStateContainer } = getState({});
const { appStateContainer } = getState({ history });
const { index } = appStateContainer.getState();
return Promise.props({
ip: indexPatterns.getCache().then(indexPatternList => {
Expand Down Expand Up @@ -151,9 +155,13 @@ app.config($routeProvider => {
})
.catch(
redirectWhenMissing({
search: '/discover',
'index-pattern':
'/management/kibana/objects/savedSearches/' + $route.current.params.id,
history,
mapping: {
search: '/discover',
'index-pattern':
'/management/kibana/objects/savedSearches/' + $route.current.params.id,
},
toastNotifications,
})
),
});
Expand Down Expand Up @@ -181,7 +189,8 @@ function discoverController(
config,
kbnUrl,
localStorage,
uiCapabilities
uiCapabilities,
history
) {
const { isDefault: isDefaultType } = indexPatternsUtils;
const subscriptions = new Subscription();
Expand All @@ -207,6 +216,7 @@ function discoverController(
} = getState({
defaultAppState: getStateDefaults(),
storeInSessionStorage: config.get('state:storeInSessionStorage'),
history,
});
if (appStateContainer.getState().index !== $scope.indexPattern.id) {
//used index pattern is different than the given by url/state which is invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Test discover state', () => {
history.push('/');
state = getState({
defaultAppState: { index: 'test' },
hashHistory: history,
history,
});
await state.replaceUrlAppState({});
await state.startSync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { isEqual } from 'lodash';
import { createHashHistory, History } from 'history';
import { History } from 'history';
import {
createStateContainer,
createKbnUrlStateStorage,
Expand Down Expand Up @@ -65,9 +65,9 @@ interface GetStateParams {
*/
storeInSessionStorage?: boolean;
/**
* Browser history used for testing
* Browser history
*/
hashHistory?: History;
history: History;
}

export interface GetStateReturn {
Expand Down Expand Up @@ -121,11 +121,11 @@ const APP_STATE_URL_KEY = '_a';
export function getState({
defaultAppState = {},
storeInSessionStorage = false,
hashHistory,
history,
}: GetStateParams): GetStateReturn {
const stateStorage = createKbnUrlStateStorage({
useHash: storeInSessionStorage,
history: hashHistory ? hashHistory : createHashHistory(),
history,
});

const appStateFromUrl = stateStorage.get(APP_STATE_URL_KEY) as AppState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

export { subscribeWithScope } from 'ui/utils/subscribe_with_scope';
// @ts-ignore
export { KbnUrlProvider, RedirectWhenMissingProvider } from 'ui/url';
export { KbnUrlProvider } from 'ui/url';
export { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url';
export { KibanaParsedUrl } from 'ui/url/kibana_parsed_url';
export { wrapInI18nContext } from 'ui/i18n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { AppMountContext } from 'kibana/public';
import {
configureAppAngularModule,
KbnUrlProvider,
RedirectWhenMissingProvider,
IPrivate,
PrivateProvider,
PromiseServiceCreator,
Expand Down Expand Up @@ -102,8 +101,7 @@ function createLocalAngularModule(core: AppMountContext['core'], navigation: Nav
function createLocalKbnUrlModule() {
angular
.module('app/visualize/KbnUrl', ['app/visualize/Private', 'ngRoute'])
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider))
.service('redirectWhenMissing', (Private: IPrivate) => Private(RedirectWhenMissingProvider));
.service('kbnUrl', (Private: IPrivate) => Private(KbnUrlProvider));
}

function createLocalPromiseModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { VisualizeConstants } from '../visualize_constants';
import { getEditBreadcrumbs } from '../breadcrumbs';

import { addHelpMenuToAppChrome } from '../help_menu/help_menu_util';
import { unhashUrl } from '../../../../../../../plugins/kibana_utils/public';
import { unhashUrl, redirectWhenMissing } from '../../../../../../../plugins/kibana_utils/public';
import { kbnBaseUrl } from '../../../../../../../plugins/kibana_legacy/public';
import {
SavedObjectSaveModal,
Expand Down Expand Up @@ -75,7 +75,6 @@ function VisualizeAppController(
$injector,
$timeout,
kbnUrl,
redirectWhenMissing,
kbnUrlStateStorage,
history
) {
Expand Down Expand Up @@ -319,10 +318,16 @@ function VisualizeAppController(
if (!_.isEqual(stateContainer.getState().vis, stateDefaults.vis)) {
try {
vis.setState(stateContainer.getState().vis);
} catch {
redirectWhenMissing({
'index-pattern-field': '/visualize',
} catch (error) {
const redirect = redirectWhenMissing({
history,
mapping: {
'index-pattern-field': '/visualize',
},
toastNotifications,
});

redirect(error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import { find } from 'lodash';
import { i18n } from '@kbn/i18n';
import { createHashHistory } from 'history';

import { createKbnUrlStateStorage } from '../../../../../../plugins/kibana_utils/public';
import {
createKbnUrlStateStorage,
redirectWhenMissing,
} from '../../../../../../plugins/kibana_utils/public';

import editorTemplate from './editor/editor.html';
import visualizeListingTemplate from './listing/visualize_listing.html';
Expand Down Expand Up @@ -100,8 +103,8 @@ export function initVisualizeApp(app, deps) {
template: editorTemplate,
k7Breadcrumbs: getCreateBreadcrumbs,
resolve: {
savedVis: function(redirectWhenMissing, $route, $rootScope, kbnUrl) {
const { core, data, savedVisualizations, visualizations } = deps;
savedVis: function($route, $rootScope, kbnUrl, history) {
const { core, data, savedVisualizations, visualizations, toastNotifications } = deps;
const visTypes = visualizations.all();
const visType = find(visTypes, { name: $route.current.params.type });
const shouldHaveIndex = visType.requiresSearch && visType.options.showIndexSelection;
Expand All @@ -128,7 +131,11 @@ export function initVisualizeApp(app, deps) {
})
.catch(
redirectWhenMissing({
'*': '/visualize',
history,
mapping: {
'*': '/visualize',
},
toastNotifications,
})
);
},
Expand All @@ -139,8 +146,8 @@ export function initVisualizeApp(app, deps) {
template: editorTemplate,
k7Breadcrumbs: getEditBreadcrumbs,
resolve: {
savedVis: function(redirectWhenMissing, $route, $rootScope, kbnUrl) {
const { chrome, core, data, savedVisualizations } = deps;
savedVis: function($route, $rootScope, kbnUrl, history) {
const { chrome, core, data, savedVisualizations, toastNotifications } = deps;
return ensureDefaultIndexPattern(core, data, $rootScope, kbnUrl)
.then(() => savedVisualizations.get($route.current.params.id))
.then(savedVis => {
Expand All @@ -155,13 +162,17 @@ export function initVisualizeApp(app, deps) {
})
.catch(
redirectWhenMissing({
visualization: '/visualize',
search:
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
'index-pattern':
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
'index-pattern-field':
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
history,
mapping: {
visualization: '/visualize',
search:
kertal marked this conversation as resolved.
Show resolved Hide resolved
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
'index-pattern':
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
'index-pattern-field':
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
},
toastNotifications,
})
);
},
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/public/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { removeQueryParam } from './remove_query_param';
export { redirectWhenMissing } from './redirect_when_missing';
Loading