-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Set up curation search server route - not really sure which API endpoint to use, hedging my bets * Set up AddResultLogic - fairly simple, mostly concerned with flyout behavior & search query - could likely be reused (or replaced with??) query tester logic in the future * Add main AddResultFlyout component - with custom isPromoted / isHidden logic & actions * Update AddResultButton to open flyout * Update Curation page to render the flyout * PR feedback: reset search query on flyout re-open Co-authored-by: Constance <[email protected]>
- Loading branch information
1 parent
7946d27
commit 290f0cd
Showing
11 changed files
with
566 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
.../applications/app_search/components/curations/curation/results/add_result_flyout.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { setMockActions, setMockValues } from '../../../../../__mocks__'; | ||
|
||
import React from 'react'; | ||
|
||
import { shallow } from 'enzyme'; | ||
|
||
import { EuiFlyout, EuiFieldSearch, EuiEmptyPrompt } from '@elastic/eui'; | ||
|
||
import { CurationResult, AddResultFlyout } from './'; | ||
|
||
describe('AddResultFlyout', () => { | ||
const values = { | ||
dataLoading: false, | ||
searchQuery: '', | ||
searchResults: [], | ||
promotedIds: [], | ||
hiddenIds: [], | ||
}; | ||
const actions = { | ||
search: jest.fn(), | ||
closeFlyout: jest.fn(), | ||
addPromotedId: jest.fn(), | ||
removePromotedId: jest.fn(), | ||
addHiddenId: jest.fn(), | ||
removeHiddenId: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
setMockValues(values); | ||
setMockActions(actions); | ||
}); | ||
|
||
it('renders a closeable flyout', () => { | ||
const wrapper = shallow(<AddResultFlyout />); | ||
expect(wrapper.find(EuiFlyout)).toHaveLength(1); | ||
|
||
wrapper.find(EuiFlyout).simulate('close'); | ||
expect(actions.closeFlyout).toHaveBeenCalled(); | ||
}); | ||
|
||
describe('search input', () => { | ||
it('renders isLoading state correctly', () => { | ||
setMockValues({ ...values, dataLoading: true }); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
|
||
expect(wrapper.find(EuiFieldSearch).prop('isLoading')).toEqual(true); | ||
}); | ||
|
||
it('renders value correctly', () => { | ||
setMockValues({ ...values, searchQuery: 'hello world' }); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
|
||
expect(wrapper.find(EuiFieldSearch).prop('value')).toEqual('hello world'); | ||
}); | ||
|
||
it('calls search on input change', () => { | ||
const wrapper = shallow(<AddResultFlyout />); | ||
wrapper.find(EuiFieldSearch).simulate('change', { target: { value: 'lorem ipsum' } }); | ||
|
||
expect(actions.search).toHaveBeenCalledWith('lorem ipsum'); | ||
}); | ||
}); | ||
|
||
describe('search results', () => { | ||
it('renders an empty state', () => { | ||
setMockValues({ ...values, searchResults: [] }); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
|
||
expect(wrapper.find(EuiEmptyPrompt)).toHaveLength(1); | ||
expect(wrapper.find(CurationResult)).toHaveLength(0); | ||
}); | ||
|
||
it('renders a result component for each item in searchResults', () => { | ||
setMockValues({ | ||
...values, | ||
searchResults: [ | ||
{ id: { raw: 'doc-1' } }, | ||
{ id: { raw: 'doc-2' } }, | ||
{ id: { raw: 'doc-3' } }, | ||
], | ||
}); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
|
||
expect(wrapper.find(CurationResult)).toHaveLength(3); | ||
}); | ||
|
||
describe('actions', () => { | ||
it('renders a hide result button if the document ID is not already in the hiddenIds list', () => { | ||
setMockValues({ | ||
...values, | ||
searchResults: [{ id: { raw: 'visible-document' } }], | ||
hiddenIds: ['hidden-document'], | ||
}); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
wrapper.find(CurationResult).prop('actions')[0].onClick(); | ||
|
||
expect(actions.addHiddenId).toHaveBeenCalledWith('visible-document'); | ||
}); | ||
|
||
it('renders a show result button if the document ID is already in the hiddenIds list', () => { | ||
setMockValues({ | ||
...values, | ||
searchResults: [{ id: { raw: 'hidden-document' } }], | ||
hiddenIds: ['hidden-document'], | ||
}); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
wrapper.find(CurationResult).prop('actions')[0].onClick(); | ||
|
||
expect(actions.removeHiddenId).toHaveBeenCalledWith('hidden-document'); | ||
}); | ||
|
||
it('renders a promote result button if the document ID is not already in the promotedIds list', () => { | ||
setMockValues({ | ||
...values, | ||
searchResults: [{ id: { raw: 'some-document' } }], | ||
promotedIds: ['promoted-document'], | ||
}); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
wrapper.find(CurationResult).prop('actions')[1].onClick(); | ||
|
||
expect(actions.addPromotedId).toHaveBeenCalledWith('some-document'); | ||
}); | ||
|
||
it('renders a demote result button if the document ID is already in the promotedIds list', () => { | ||
setMockValues({ | ||
...values, | ||
searchResults: [{ id: { raw: 'promoted-document' } }], | ||
promotedIds: ['promoted-document'], | ||
}); | ||
const wrapper = shallow(<AddResultFlyout />); | ||
wrapper.find(CurationResult).prop('actions')[1].onClick(); | ||
|
||
expect(actions.removePromotedId).toHaveBeenCalledWith('promoted-document'); | ||
}); | ||
}); | ||
}); | ||
}); |
121 changes: 121 additions & 0 deletions
121
...ublic/applications/app_search/components/curations/curation/results/add_result_flyout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { useValues, useActions } from 'kea'; | ||
|
||
import { | ||
EuiPortal, | ||
EuiFlyout, | ||
EuiFlyoutHeader, | ||
EuiFlyoutBody, | ||
EuiTitle, | ||
EuiText, | ||
EuiSpacer, | ||
EuiFieldSearch, | ||
EuiEmptyPrompt, | ||
} from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { FlashMessages } from '../../../../../shared/flash_messages'; | ||
|
||
import { | ||
RESULT_ACTIONS_DIRECTIONS, | ||
PROMOTE_DOCUMENT_ACTION, | ||
DEMOTE_DOCUMENT_ACTION, | ||
HIDE_DOCUMENT_ACTION, | ||
SHOW_DOCUMENT_ACTION, | ||
} from '../../constants'; | ||
import { CurationLogic } from '../curation_logic'; | ||
|
||
import { AddResultLogic, CurationResult } from './'; | ||
|
||
export const AddResultFlyout: React.FC = () => { | ||
const { searchQuery, searchResults, dataLoading } = useValues(AddResultLogic); | ||
const { search, closeFlyout } = useActions(AddResultLogic); | ||
|
||
const { promotedIds, hiddenIds } = useValues(CurationLogic); | ||
const { addPromotedId, removePromotedId, addHiddenId, removeHiddenId } = useActions( | ||
CurationLogic | ||
); | ||
|
||
return ( | ||
<EuiPortal> | ||
<EuiFlyout ownFocus onClose={closeFlyout} aria-labelledby="addResultFlyout"> | ||
<EuiFlyoutHeader hasBorder> | ||
<EuiTitle size="m"> | ||
<h2 id="addResultFlyout"> | ||
{i18n.translate('xpack.enterpriseSearch.appSearch.engine.curations.addResult.title', { | ||
defaultMessage: 'Add result to curation', | ||
})} | ||
</h2> | ||
</EuiTitle> | ||
<EuiText color="subdued"> | ||
<p>{RESULT_ACTIONS_DIRECTIONS}</p> | ||
</EuiText> | ||
</EuiFlyoutHeader> | ||
<EuiFlyoutBody banner={<FlashMessages />}> | ||
<EuiFieldSearch | ||
value={searchQuery} | ||
onChange={(e) => search(e.target.value)} | ||
isLoading={dataLoading} | ||
placeholder={i18n.translate( | ||
'xpack.enterpriseSearch.appSearch.engine.curations.addResult.searchPlaceholder', | ||
{ defaultMessage: 'Search engine documents' } | ||
)} | ||
fullWidth | ||
autoFocus | ||
/> | ||
<EuiSpacer /> | ||
|
||
{searchResults.length > 0 ? ( | ||
searchResults.map((result) => { | ||
const id = result.id.raw; | ||
const isPromoted = promotedIds.includes(id); | ||
const isHidden = hiddenIds.includes(id); | ||
|
||
return ( | ||
<CurationResult | ||
key={id} | ||
result={result} | ||
actions={[ | ||
isHidden | ||
? { | ||
...SHOW_DOCUMENT_ACTION, | ||
onClick: () => removeHiddenId(id), | ||
} | ||
: { | ||
...HIDE_DOCUMENT_ACTION, | ||
onClick: () => addHiddenId(id), | ||
}, | ||
isPromoted | ||
? { | ||
...DEMOTE_DOCUMENT_ACTION, | ||
onClick: () => removePromotedId(id), | ||
} | ||
: { | ||
...PROMOTE_DOCUMENT_ACTION, | ||
onClick: () => addPromotedId(id), | ||
}, | ||
]} | ||
/> | ||
); | ||
}) | ||
) : ( | ||
<EuiEmptyPrompt | ||
body={i18n.translate( | ||
'xpack.enterpriseSearch.appSearch.engine.curations.addResult.searchEmptyDescription', | ||
{ defaultMessage: 'No matching content found.' } | ||
)} | ||
/> | ||
)} | ||
</EuiFlyoutBody> | ||
</EuiFlyout> | ||
</EuiPortal> | ||
); | ||
}; |
Oops, something went wrong.