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

feat(SearchBar): fetch and open note once clicked on a suggestion #774

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions src/components/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Autosuggest from 'react-autosuggest'
import debounce from 'lodash.debounce'
import { fetchRawIntent } from 'lib/intents'
import logger from 'lib/logger'
import { models, withClient } from 'cozy-client'

const INTENT_VERB = 'OPEN'
const INTENT_DOCTYPE = 'io.cozy.suggestions'
Expand Down Expand Up @@ -229,15 +230,15 @@ class SearchBar extends Component {
this.setState({ query: null, searching: false })
}

onSuggestionSelected = async (event, { suggestion }) => {
onSuggestionSelected = client => async (event, { suggestion }) => {
const { onSelect } = suggestion
// `onSelect` is a string that describes what should happen when the suggestion is selected. Currently, the only format we're supporting is `open:http://example.com` to change the url of the current page.

if (typeof onSelect === 'function') {
await onSelect()
} else if (/^open:/.test(onSelect)) {
const url = onSelect.substr(5)
if (/^id_note:/.test(onSelect)) {
const url = await models.note.fetchURL(client, { id: onSelect.substr(8) })
window.location.href = url
} else if (/^open:/.test(onSelect)) {
window.location.href = onSelect.substr(5)
} else {
// eslint-disable-next-line no-console
console.log(
Expand Down Expand Up @@ -288,7 +289,7 @@ class SearchBar extends Component {
sourceURLs
} = this.state
if (sourceURLs.length === 0) return null
const { t } = this.props
const { t, client } = this.props

const isInitialSearch = input !== '' && query === null
const hasSuggestions =
Expand Down Expand Up @@ -339,7 +340,7 @@ class SearchBar extends Component {
this.debouncedOnSuggestionsFetchRequested
}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
onSuggestionSelected={this.onSuggestionSelected}
onSuggestionSelected={this.onSuggestionSelected(client)}
getSuggestionValue={this.getSuggestionValue}
getSectionSuggestions={this.getSectionSuggestions}
renderSectionTitle={this.renderSectionTitle}
Expand All @@ -357,4 +358,4 @@ class SearchBar extends Component {
}
}

export default translate()(SearchBar)
export default translate()(withClient(SearchBar))
2 changes: 1 addition & 1 deletion src/components/__snapshots__/Bar.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ exports[`Bar should display the Searchbar 1`] = `
<div
className="u-flex-grow"
>
<withI18n(SearchBar) />
<withI18n(withClient(SearchBar)) />
</div>
<withI18n(Connect(Settings))
toggleSupport={[Function]}
Expand Down