Skip to content

Commit

Permalink
[Workplace Search] Enable check for org context based on URL (#83487)
Browse files Browse the repository at this point in the history
* Add regex check to determine whether url is org

As a part of the Kibana migration, we are switching the URL structure to put the prefix on the personal dashboard. In ent-search, org routes were prefixed with `/org`. In Kibana the prefix switches to non-org routes and they will be prefixed with`/p`

* Add isOrganization boolean to logic
  • Loading branch information
scottybollinger authored Nov 17, 2020
1 parent 9b5605f commit e4516ee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('AppLogic', () => {
account: {},
hasInitialized: false,
isFederatedAuth: true,
isOrganization: false,
organization: {},
};

Expand All @@ -34,6 +35,7 @@ describe('AppLogic', () => {
},
hasInitialized: true,
isFederatedAuth: false,
isOrganization: false,
organization: {
defaultOrgName: 'My Organization',
name: 'ACME Donuts',
Expand Down Expand Up @@ -61,4 +63,12 @@ describe('AppLogic', () => {
});
});
});

describe('setContext()', () => {
it('sets context', () => {
AppLogic.actions.setContext(true);

expect(AppLogic.values.isOrganization).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
interface AppValues extends WorkplaceSearchInitialData {
hasInitialized: boolean;
isFederatedAuth: boolean;
isOrganization: boolean;
}
interface AppActions {
initializeAppData(props: InitialAppData): InitialAppData;
setContext(isOrganization: boolean): boolean;
}

const emptyOrg = {} as Organization;
Expand All @@ -31,6 +33,7 @@ export const AppLogic = kea<MakeLogicType<AppValues, AppActions>>({
workplaceSearch,
isFederatedAuth,
}),
setContext: (isOrganization) => isOrganization,
},
reducers: {
hasInitialized: [
Expand All @@ -45,6 +48,12 @@ export const AppLogic = kea<MakeLogicType<AppValues, AppActions>>({
initializeAppData: (_, { isFederatedAuth }) => !!isFederatedAuth,
},
],
isOrganization: [
false,
{
setContext: (_, isOrganization) => isOrganization,
},
],
organization: [
emptyOrg,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ describe('WorkplaceSearchUnconfigured', () => {
});

describe('WorkplaceSearchConfigured', () => {
const initializeAppData = jest.fn();
const setContext = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
setMockActions({ initializeAppData: () => {} });
setMockActions({ initializeAppData, setContext });
});

it('renders layout and header actions', () => {
Expand All @@ -60,17 +63,12 @@ describe('WorkplaceSearchConfigured', () => {
});

it('initializes app data with passed props', () => {
const initializeAppData = jest.fn();
setMockActions({ initializeAppData });

shallow(<WorkplaceSearchConfigured isFederatedAuth={true} />);

expect(initializeAppData).toHaveBeenCalledWith({ isFederatedAuth: true });
});

it('does not re-initialize app data or re-render header actions', () => {
const initializeAppData = jest.fn();
setMockActions({ initializeAppData });
setMockValues({ hasInitialized: true });

shallow(<WorkplaceSearchConfigured />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, { useEffect } from 'react';
import { Route, Redirect, Switch } from 'react-router-dom';
import { Route, Redirect, Switch, useLocation } from 'react-router-dom';
import { useActions, useValues } from 'kea';

import { WORKPLACE_SEARCH_PLUGIN } from '../../../common/constants';
Expand All @@ -31,17 +31,32 @@ export const WorkplaceSearch: React.FC<InitialAppData> = (props) => {

export const WorkplaceSearchConfigured: React.FC<InitialAppData> = (props) => {
const { hasInitialized } = useValues(AppLogic);
const { initializeAppData } = useActions(AppLogic);
const { initializeAppData, setContext } = useActions(AppLogic);
const { renderHeaderActions } = useValues(KibanaLogic);
const { errorConnecting, readOnlyMode } = useValues(HttpLogic);

const { pathname } = useLocation();

/**
* Personal dashboard urls begin with /p/
* EX: http://localhost:5601/app/enterprise_search/workplace_search/p/sources
*/
const personalSourceUrlRegex = /^\/p\//g; // matches '/p/*'

// TODO: Once auth is figured out, we need to have a check for the equivilent of `isAdmin`.
const isOrganization = !pathname.match(personalSourceUrlRegex);

useEffect(() => {
if (!hasInitialized) {
initializeAppData(props);
renderHeaderActions(WorkplaceSearchHeaderActions);
}
}, [hasInitialized]);

useEffect(() => {
setContext(isOrganization);
}, [isOrganization]);

return (
<Switch>
<Route path={SETUP_GUIDE_PATH}>
Expand Down

0 comments on commit e4516ee

Please sign in to comment.