diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source.tsx index 64431a800487f..30f5009ac0b3c 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source.tsx @@ -6,6 +6,9 @@ */ import React, { useEffect } from 'react'; +import { useLocation } from 'react-router-dom'; + +import { Location } from 'history'; import { useActions, useValues } from 'kea'; @@ -31,6 +34,7 @@ import { SaveCustom } from './save_custom'; import './add_source.scss'; export const AddSource: React.FC = (props) => { + const { search } = useLocation() as Location; const { initializeAddSource, setAddSourceStep, @@ -83,9 +87,9 @@ export const AddSource: React.FC = (props) => { const saveCustomSuccess = () => setAddSourceStep(AddSourceSteps.SaveCustomStep); const goToSaveCustom = () => createContentSource(CUSTOM_SERVICE_TYPE, saveCustomSuccess); - const goToFormSourceCreated = (sourceName: string) => { + const goToFormSourceCreated = () => { KibanaLogic.values.navigateToUrl( - `${getSourcesPath(SOURCE_ADDED_PATH, isOrganization)}/?name=${sourceName}` + `${getSourcesPath(SOURCE_ADDED_PATH, isOrganization)}${search}` ); }; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.test.ts index d0ab40399fa59..6c60cd74a9c9f 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.test.ts @@ -20,7 +20,7 @@ jest.mock('../../../../app_logic', () => ({ })); import { AppLogic } from '../../../../app_logic'; -import { SOURCES_PATH, getSourcesPath } from '../../../../routes'; +import { ADD_GITHUB_PATH, SOURCES_PATH, getSourcesPath } from '../../../../routes'; import { CustomSource } from '../../../../types'; import { SourcesLogic } from '../../sources_logic'; @@ -55,10 +55,12 @@ describe('AddSourceLogic', () => { sourceConfigData: {} as SourceConfigData, sourceConnectData: {} as SourceConnectData, newCustomSource: {} as CustomSource, + oauthConfigCompleted: false, currentServiceType: '', githubOrganizations: [], selectedGithubOrganizationsMap: {} as OrganizationsMap, selectedGithubOrganizations: [], + preContentSourceId: '', }; const sourceConnectData = { @@ -182,6 +184,12 @@ describe('AddSourceLogic', () => { expect(AddSourceLogic.values.selectedGithubOrganizationsMap).toEqual({ foo: true }); }); + it('setPreContentSourceId', () => { + AddSourceLogic.actions.setPreContentSourceId('123'); + + expect(AddSourceLogic.values.preContentSourceId).toEqual('123'); + }); + it('setButtonNotLoading', () => { AddSourceLogic.actions.setButtonNotLoading(); @@ -317,6 +325,34 @@ describe('AddSourceLogic', () => { expect(navigateToUrl).toHaveBeenCalledWith(getSourcesPath(SOURCES_PATH, false)); }); + it('redirects to oauth config when preContentSourceId is present', async () => { + const preContentSourceId = 'id123'; + const setPreContentSourceIdSpy = jest.spyOn( + AddSourceLogic.actions, + 'setPreContentSourceId' + ); + + http.get.mockReturnValue( + Promise.resolve({ + ...response, + hasConfigureStep: true, + preContentSourceId, + }) + ); + AddSourceLogic.actions.saveSourceParams(queryString); + expect(http.get).toHaveBeenCalledWith('/api/workplace_search/sources/create', { + query: { + ...params, + kibana_host: '', + }, + }); + + await nextTick(); + + expect(setPreContentSourceIdSpy).toHaveBeenCalledWith(preContentSourceId); + expect(navigateToUrl).toHaveBeenCalledWith(`${ADD_GITHUB_PATH}/configure${queryString}`); + }); + it('handles error', async () => { http.get.mockReturnValue(Promise.reject('this is an error')); @@ -440,13 +476,14 @@ describe('AddSourceLogic', () => { describe('getPreContentSourceConfigData', () => { it('calls API and sets values', async () => { + mount({ preContentSourceId: '123' }); const setPreContentSourceConfigDataSpy = jest.spyOn( AddSourceLogic.actions, 'setPreContentSourceConfigData' ); http.get.mockReturnValue(Promise.resolve(config)); - AddSourceLogic.actions.getPreContentSourceConfigData('123'); + AddSourceLogic.actions.getPreContentSourceConfigData(); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/pre_sources/123'); await nextTick(); @@ -456,7 +493,7 @@ describe('AddSourceLogic', () => { it('handles error', async () => { http.get.mockReturnValue(Promise.reject('this is an error')); - AddSourceLogic.actions.getPreContentSourceConfigData('123'); + AddSourceLogic.actions.getPreContentSourceConfigData(); await nextTick(); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); @@ -616,7 +653,8 @@ describe('AddSourceLogic', () => { }); it('getPreContentSourceConfigData', () => { - AddSourceLogic.actions.getPreContentSourceConfigData('123'); + mount({ preContentSourceId: '123' }); + AddSourceLogic.actions.getPreContentSourceConfigData(); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/account/pre_sources/123'); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.ts index e1f554d87551d..ed63f82764f7e 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/add_source_logic.ts @@ -22,7 +22,7 @@ import { KibanaLogic } from '../../../../../shared/kibana'; import { parseQueryParams } from '../../../../../shared/query_params'; import { AppLogic } from '../../../../app_logic'; import { CUSTOM_SERVICE_TYPE, WORKPLACE_SEARCH_URL_PREFIX } from '../../../../constants'; -import { SOURCES_PATH, getSourcesPath } from '../../../../routes'; +import { SOURCES_PATH, ADD_GITHUB_PATH, getSourcesPath } from '../../../../routes'; import { CustomSource } from '../../../../types'; import { staticSourceData } from '../../source_data'; import { SourcesLogic } from '../../sources_logic'; @@ -74,6 +74,7 @@ export interface AddSourceActions { setSourceIndexPermissionsValue(indexPermissionsValue: boolean): boolean; setCustomSourceData(data: CustomSource): CustomSource; setPreContentSourceConfigData(data: PreContentSourceResponse): PreContentSourceResponse; + setPreContentSourceId(preContentSourceId: string): string; setSelectedGithubOrganizations(option: string): string; resetSourceState(): void; createContentSource( @@ -92,7 +93,7 @@ export interface AddSourceActions { successCallback: (oauthUrl: string) => void ): { serviceType: string; successCallback(oauthUrl: string): void }; getSourceReConnectData(sourceId: string): { sourceId: string }; - getPreContentSourceConfigData(preContentSourceId: string): { preContentSourceId: string }; + getPreContentSourceConfigData(): void; setButtonNotLoading(): void; } @@ -144,6 +145,8 @@ interface AddSourceValues { githubOrganizations: string[]; selectedGithubOrganizationsMap: OrganizationsMap; selectedGithubOrganizations: string[]; + preContentSourceId: string; + oauthConfigCompleted: boolean; } interface PreContentSourceResponse { @@ -181,6 +184,7 @@ export const AddSourceLogic = kea indexPermissionsValue, setCustomSourceData: (data: CustomSource) => data, setPreContentSourceConfigData: (data: PreContentSourceResponse) => data, + setPreContentSourceId: (preContentSourceId: string) => preContentSourceId, setSelectedGithubOrganizations: (option: string) => option, getSourceConfigData: (serviceType: string) => ({ serviceType }), getSourceConnectData: (serviceType: string, successCallback: (oauthUrl: string) => string) => ({ @@ -188,7 +192,7 @@ export const AddSourceLogic = kea ({ sourceId }), - getPreContentSourceConfigData: (preContentSourceId: string) => ({ preContentSourceId }), + getPreContentSourceConfigData: () => true, saveSourceConfig: (isUpdating: boolean, successCallback?: () => void) => ({ isUpdating, successCallback, @@ -344,6 +348,20 @@ export const AddSourceLogic = kea ({}), }, ], + preContentSourceId: [ + '', + { + setPreContentSourceId: (_, preContentSourceId) => preContentSourceId, + setPreContentSourceConfigData: () => '', + resetSourceState: () => '', + }, + ], + oauthConfigCompleted: [ + false, + { + setPreContentSourceConfigData: () => true, + }, + ], }, selectors: ({ selectors }) => ({ selectedGithubOrganizations: [ @@ -407,8 +425,9 @@ export const AddSourceLogic = kea { + getPreContentSourceConfigData: async () => { const { isOrganization } = AppLogic.values; + const { preContentSourceId } = values; const route = isOrganization ? `/api/workplace_search/org/pre_sources/${preContentSourceId}` : `/api/workplace_search/account/pre_sources/${preContentSourceId}`; @@ -480,12 +499,24 @@ export const AddSourceLogic = kea = ({ name, onFormCreated, header }) => { - const { search } = useLocation() as Location; - - const { preContentSourceId } = (parseQueryParams(search) as unknown) as OauthQueryParams; const [formLoading, setFormLoading] = useState(false); const { @@ -58,7 +48,7 @@ export const ConfigureOauth: React.FC = ({ name, onFormCrea const checkboxOptions = githubOrganizations.map((item) => ({ id: item, label: item })); useEffect(() => { - getPreContentSourceConfigData(preContentSourceId); + getPreContentSourceConfigData(); }, []); const handleChange = (option: string) => setSelectedGithubOrganizations(option); @@ -101,6 +91,7 @@ export const ConfigureOauth: React.FC = ({ name, onFormCrea return ( <> {header} + {sectionLoading ? : configfieldsForm} ); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/constants.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/constants.ts index dd756a51fded3..712be15e7c046 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/constants.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/constants.ts @@ -176,7 +176,7 @@ export const CONFIG_CUSTOM_BUTTON = i18n.translate( export const CONFIG_OAUTH_LABEL = i18n.translate( 'xpack.enterpriseSearch.workplaceSearch.contentSource.configOauth.label', { - defaultMessage: 'Complete connection', + defaultMessage: 'Select GitHub organizations to sync', } );