Skip to content

Commit 598a625

Browse files
committed
groups filter from api
1 parent 6a1758e commit 598a625

File tree

8 files changed

+315
-150
lines changed

8 files changed

+315
-150
lines changed

package-lock.json

Lines changed: 155 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"react-redux": "^6.0.0",
8080
"react-redux-toastr": "^7.5.1",
8181
"react-router-dom": "^4.3.1",
82-
"react-select": "^1.2.0",
82+
"react-select": "^3.1.1",
8383
"react-stickynode": "^2.1.1",
8484
"react-svg": "^4.1.1",
8585
"react-tabs": "^3.0.0",
Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import Select from '../../Select'
3+
import AsyncSelect from '../../Select/AsyncSelect'
44
import cn from 'classnames'
55
import styles from './Groups-Field.module.scss'
6+
import _ from 'lodash'
7+
import { axiosInstance } from '../../../services/axiosWithAuth'
8+
import { AUTOCOMPLETE_MIN_LENGTH, AUTOCOMPLETE_DEBOUNCE_TIME_MS, GROUPS_API_URL } from '../../../config/constants'
9+
10+
const GroupsField = ({ onUpdateMultiSelect, challenge }) => {
11+
async function fetchGroups (name) {
12+
if (!name) return []
13+
console.log('url')
14+
console.log(GROUPS_API_URL)
15+
const url = `${GROUPS_API_URL}?name=${name}`
16+
return axiosInstance.get(url)
17+
}
18+
19+
const onInputChange = React.useCallback(_.debounce(async (inputValue, callback) => {
20+
if (!inputValue) return
21+
const preparedValue = inputValue.trim()
22+
if (preparedValue.length < AUTOCOMPLETE_MIN_LENGTH) {
23+
return []
24+
}
25+
const { data } = await fetchGroups(inputValue)
26+
const suggestions = data.map(suggestion => ({
27+
label: suggestion.name,
28+
value: suggestion.id
29+
}))
30+
callback && callback(suggestions)
31+
}, AUTOCOMPLETE_DEBOUNCE_TIME_MS), [])
632

7-
const GroupsField = ({ groups, onUpdateMultiSelect, challenge }) => {
833
return (
934
<div className={styles.row}>
1035
<div className={cn(styles.field, styles.col1)}>
1136
<label htmlFor='type'>Groups :</label>
1237
</div>
1338
<div className={cn(styles.field, styles.col2)}>
14-
<Select
39+
<AsyncSelect
1540
name='group'
1641
multi
17-
options={groups.map(g => ({ label: g.name, value: g.id }))}
42+
loadOptions={(inputValue, callback) => {
43+
onInputChange(inputValue, callback)
44+
}}
1845
simpleValue
1946
value={challenge.groups.join(',')}
2047
placeholder='Select groups'
@@ -25,14 +52,9 @@ const GroupsField = ({ groups, onUpdateMultiSelect, challenge }) => {
2552
)
2653
}
2754

28-
GroupsField.defaultProps = {
29-
groups: []
30-
}
31-
3255
GroupsField.propTypes = {
3356
onUpdateMultiSelect: PropTypes.func.isRequired,
34-
challenge: PropTypes.shape().isRequired,
35-
groups: PropTypes.arrayOf(PropTypes.shape()).isRequired
57+
challenge: PropTypes.shape().isRequired
3658
}
3759

3860
export default GroupsField

src/components/ChallengeEditor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ class ChallengeEditor extends Component {
13541354
<React.Fragment>
13551355
{/* remove terms field and use default term */}
13561356
{false && (<TermsField terms={metadata.challengeTerms} challenge={challenge} onUpdateMultiSelect={this.onUpdateMultiSelect} />)}
1357-
<GroupsField groups={metadata.groups} onUpdateMultiSelect={this.onUpdateMultiSelect} challenge={challenge} />
1357+
<GroupsField onUpdateMultiSelect={this.onUpdateMultiSelect} challenge={challenge} />
13581358
</React.Fragment>
13591359
)}
13601360
{

src/components/Select/AsyncSelect.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react'
2+
import _ from 'lodash'
3+
import ReactSelect from 'react-select/async'
4+
import PT from 'prop-types'
5+
6+
export default function Select (props) {
7+
const { selectRef } = props
8+
const customStyles = {
9+
container: (provided) => ({
10+
...provided,
11+
width: '100%',
12+
minWidth: '150px',
13+
background: 'red'
14+
}),
15+
control: (provided) => ({
16+
...provided,
17+
borderRadius: '2px !important',
18+
':focus': {
19+
border: '1px solid #2C95D7',
20+
boxShadow: 'none'
21+
}
22+
}),
23+
menu: (provided) => ({
24+
...provided,
25+
boxSizing: 'border-box',
26+
fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
27+
fontSize: '15px',
28+
fontWeight: 300,
29+
lineHeight: '18px',
30+
color: '#2a2a2a',
31+
border: '1px solid #2C95D7',
32+
zIndex: 4
33+
}),
34+
option: (provided) => ({
35+
...provided,
36+
paddingLeft: '20px'
37+
}),
38+
placeholder: (provided) => ({
39+
...provided,
40+
fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
41+
fontSize: '15px',
42+
fontWeight: 300,
43+
paddingLeft: '20px',
44+
color: '#2a2a2a'
45+
}),
46+
input: (provided) => ({
47+
...provided,
48+
backgroundColor: 'transparent',
49+
marginLeft: 0,
50+
paddingRight: '6px',
51+
paddingLeft: '10px',
52+
border: 'none'
53+
})
54+
}
55+
return (
56+
<ReactSelect
57+
ref={selectRef}
58+
{...props}
59+
autosize={false}
60+
styles={customStyles}
61+
/>
62+
)
63+
}
64+
65+
Select.defaultProps = {
66+
selectRef: _.noop
67+
}
68+
69+
Select.propTypes = {
70+
selectRef: PT.func
71+
}

0 commit comments

Comments
 (0)