forked from clearlydefined/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support cocoapods search on harvest page
Task: clearlydefined#963
- Loading branch information
1 parent
0ad352b
commit 25d9d00
Showing
6 changed files
with
187 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// (c) Copyright 2022, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { getCocoaPodsSearch } from '../api/clearlyDefined' | ||
import { AsyncTypeahead } from 'react-bootstrap-typeahead' | ||
import searchSvg from '../images/icons/searchSvg.svg' | ||
import 'react-bootstrap-typeahead/css/Typeahead.css' | ||
|
||
export default class CocoaPodsSelector extends Component { | ||
static propTypes = { | ||
onChange: PropTypes.func | ||
} | ||
|
||
constructor(props) { | ||
super(props) | ||
this.state = { isLoading: false, options: [], focus: true } | ||
this.getOptions = this.getOptions.bind(this) | ||
this.onChange = this.onChange.bind(this) | ||
} | ||
|
||
onChange(values) { | ||
const { onChange } = this.props | ||
const value = values.length === 0 ? null : values[0] | ||
value && onChange && onChange({ type: 'pod', provider: 'cocoapods', name: value.id }, 'package') | ||
} | ||
|
||
async getOptions(value) { | ||
try { | ||
this.setState({ ...this.state, isLoading: true }) | ||
const options = await getCocoaPodsSearch(this.props.token, value) | ||
this.setState({ ...this.state, options, isLoading: false }) | ||
} catch (error) { | ||
this.setState({ ...this.state, options: [], isLoading: false }) | ||
} | ||
} | ||
|
||
render() { | ||
const { options, isLoading, focus } = this.state | ||
return ( | ||
<div className={`harvest-searchbar ${focus ? 'active' : ''}`}> | ||
<div className="search-logo"> | ||
<img src={searchSvg} alt="search" /> | ||
</div> | ||
<AsyncTypeahead | ||
id="pod-selector" | ||
className="harvest-search" | ||
useCache={false} | ||
options={options} | ||
placeholder={'Pick a CocoaPod to harvest'} | ||
onChange={this.onChange} | ||
labelKey="id" | ||
onFocus={() => this.setState({ ...this.state, focus: true })} | ||
onBlur={() => this.setState({ ...this.state, focus: false })} | ||
clearButton | ||
highlightOnlyResult | ||
emptyLabel="" | ||
selectHintOnEnter | ||
isLoading={isLoading} | ||
onSearch={this.getOptions} | ||
/> | ||
</div> | ||
) | ||
} | ||
} |
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,95 @@ | ||
// (c) Copyright 2022, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { getCocoaPodsRevisions } from '../api/clearlyDefined' | ||
import Autocomplete from './Navigation/Ui/Autocomplete' | ||
import searchSvg from '../images/icons/searchSvg.svg' | ||
|
||
export default class CocoaPodsVersionPicker extends Component { | ||
static propTypes = { | ||
onChange: PropTypes.func, | ||
request: PropTypes.object.isRequired, | ||
defaultInputValue: PropTypes.string | ||
} | ||
|
||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
customValues: [], | ||
options: [], | ||
focus: false, | ||
selected: props.request.revision ? [props.request.revision] : [] | ||
} | ||
this.onChange = this.onChange.bind(this) | ||
this.filter = this.filter.bind(this) | ||
} | ||
|
||
componentDidMount() { | ||
this.getOptions('') | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
this.setState({ ...this.state, selected: nextProps.request.revision ? [nextProps.request.revision] : [] }) | ||
} | ||
|
||
async getOptions(value) { | ||
try { | ||
const { name } = this.props.request | ||
const options = await getCocoaPodsRevisions(this.props.token, name) | ||
this.setState({ ...this.state, options }) | ||
} catch (error) { | ||
this.setState({ ...this.state, options: [] }) | ||
} | ||
} | ||
|
||
onChange(values) { | ||
const { onChange } = this.props | ||
if (!onChange) return | ||
let value = values.length === 0 ? null : values[0] | ||
if (!value) return onChange(value) | ||
if (value.customOption) { | ||
value = value.label | ||
this.setState({ ...this.state, customValues: [...this.state.customValues, value] }) | ||
} | ||
onChange(value) | ||
} | ||
|
||
filter(option, props) { | ||
if (this.props.request.revision) return true | ||
return option.toLowerCase().indexOf(props.text.toLowerCase()) !== -1 | ||
} | ||
|
||
render() { | ||
const { defaultInputValue } = this.props | ||
const { customValues, options, selected, focus } = this.state | ||
const list = customValues.concat(options) | ||
return ( | ||
<div className={`harvest-searchbar ${focus ? 'active' : ''}`}> | ||
<div className="search-logo"> | ||
<img src={searchSvg} alt="search" /> | ||
</div>{' '} | ||
<Autocomplete | ||
id="cocoapods-version-picker" | ||
selected={selected} | ||
options={list} | ||
defaultInputValue={defaultInputValue} | ||
placeholder={ | ||
options.length === 0 ? 'Could not fetch versions, type a CocoaPod version' : 'Pick a CocoaPod version' | ||
} | ||
onChange={this.onChange} | ||
onFocus={() => this.setState({ ...this.state, focus: true })} | ||
onBlur={() => this.setState({ ...this.state, focus: false })} | ||
positionFixed | ||
clearButton | ||
allowNew | ||
newSelectionPrefix="Version:" | ||
emptyLabel="" | ||
filterBy={this.filter} | ||
selectHintOnEnter | ||
/> | ||
</div> | ||
) | ||
} | ||
} |
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