Skip to content

Commit 311bcb1

Browse files
committed
requested changes in pull request
1 parent 5a282ba commit 311bcb1

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed

src/components/ChallengeEditor/Groups-Field/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,38 @@ import AsyncSelect from '../../Select/AsyncSelect'
44
import cn from 'classnames'
55
import styles from './Groups-Field.module.scss'
66
import _ from 'lodash'
7-
import { axiosInstance } from '../../../services/axiosWithAuth'
8-
import { AUTOCOMPLETE_MIN_LENGTH, AUTOCOMPLETE_DEBOUNCE_TIME_MS, GROUPS_API_URL } from '../../../config/constants'
7+
import { fetchGroups } from '../../../services/challenges'
8+
import { AUTOCOMPLETE_MIN_LENGTH, AUTOCOMPLETE_DEBOUNCE_TIME_MS } from '../../../config/constants'
99

1010
const GroupsField = ({ onUpdateMultiSelect, challenge }) => {
11-
async function fetchGroups (name) {
12-
if (!name) return []
13-
const url = `${GROUPS_API_URL}?name=${name}`
14-
return axiosInstance.get(url)
15-
}
11+
const [groups, setGroups] = React.useState([])
1612

1713
const onInputChange = React.useCallback(_.debounce(async (inputValue, callback) => {
1814
if (!inputValue) return
1915
const preparedValue = inputValue.trim()
2016
if (preparedValue.length < AUTOCOMPLETE_MIN_LENGTH) {
2117
return []
2218
}
23-
const { data } = await fetchGroups(inputValue)
19+
const data = await fetchGroups({ name: inputValue })
2420
const suggestions = data.map(suggestion => ({
2521
label: suggestion.name,
2622
value: suggestion.id
2723
}))
2824
callback && callback(suggestions)
2925
}, AUTOCOMPLETE_DEBOUNCE_TIME_MS), [])
3026

27+
React.useEffect(() => {
28+
Promise.all(
29+
challenge.groups
30+
.map(group => fetchGroups({}, `/${group}`))
31+
).then(groups => {
32+
setGroups(groups.map(group => ({
33+
label: group.name,
34+
value: group.id
35+
})))
36+
}).catch(console.error)
37+
}, [])
38+
3139
return (
3240
<div className={styles.row}>
3341
<div className={cn(styles.field, styles.col1)}>
@@ -41,9 +49,12 @@ const GroupsField = ({ onUpdateMultiSelect, challenge }) => {
4149
onInputChange(inputValue, callback)
4250
}}
4351
simpleValue
44-
multivalue={challenge.groups}
52+
value={groups}
4553
placeholder='Select groups'
46-
onChange={(e) => onUpdateMultiSelect(e, 'groups')}
54+
onChange={(e) => {
55+
onUpdateMultiSelect(e, 'groups')
56+
setGroups(e)
57+
}}
4758
/>
4859
</div>
4960
</div>

src/components/ChallengeEditor/TagsField/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const TagsField = ({ challengeTags, challenge, onUpdateMultiSelect, readOnly })
2222
isMulti
2323
options={challengeTags.map(mapOps)}
2424
simpleValue
25-
multivalue={challenge.tags && challenge.tags.map(tag => ({ label: tag, value: tag }))}
25+
value={challenge.tags && challenge.tags.map(tag => ({ label: tag, value: tag }))}
2626
onChange={(value) => onUpdateMultiSelect(value, 'tags')}
2727
/>)}
2828
</div>

src/components/ChallengeEditor/Terms-Field/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import styles from './Terms-Field.module.scss'
66

77
const TermsField = ({ terms, challenge, onUpdateMultiSelect }) => {
88
const mapOps = item => ({ label: item.title, value: item.id })
9+
10+
const [currTerms, setCurrTerms] = React.useState([])
11+
12+
React.useEffect(() => {
13+
const challengeTerms = new Set(challenge.terms)
14+
const defaultValue = terms
15+
.filter(term => challengeTerms.has(term.id))
16+
.map(mapOps)
17+
setCurrTerms(defaultValue)
18+
}, [])
19+
920
return (
1021
<div className={styles.row}>
1122
<div className={cn(styles.field, styles.col1)}>
@@ -18,8 +29,11 @@ const TermsField = ({ terms, challenge, onUpdateMultiSelect }) => {
1829
isMulti
1930
options={terms.map(mapOps)}
2031
simpleValue
21-
multivalue={challenge.terms}
22-
onChange={(value) => onUpdateMultiSelect(value, 'terms')}
32+
value={currTerms}
33+
onChange={(value) => {
34+
onUpdateMultiSelect(value, 'terms')
35+
setCurrTerms(setCurrTerms(terms))
36+
}}
2337
/>
2438
</div>
2539
</div>

src/components/Select/styles.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export default {
44
width: '100%'
55
}),
66
control: (provided, state) => {
7-
console.log('state')
8-
console.log(state)
97
let styles = {
108
...provided,
119
borderRadius: '2px !important'
@@ -46,7 +44,7 @@ export default {
4644
fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
4745
fontSize: '15px',
4846
fontWeight: 300,
49-
paddingLeft: '20px',
47+
paddingLeft: '10px',
5048
color: '#2a2a2a'
5149
}),
5250
input: (provided) => ({
@@ -62,6 +60,10 @@ export default {
6260
lineHeight: 'normal !important'
6361
}
6462
}),
63+
singleValue: (provided) => ({
64+
...provided,
65+
paddingLeft: '10px'
66+
}),
6567
multiValue: (provided) => ({
6668
...provided,
6769
backgroundColor: '#2c95d7'

src/config/constants.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export const {
1414
DES_TRACK_ID,
1515
DS_TRACK_ID,
1616
QA_TRACK_ID,
17-
SEGMENT_API_KEY,
18-
GROUPS_API_URL
17+
SEGMENT_API_KEY
1918
} = process.env
2019
export const CREATE_FORUM_TYPE_IDS = typeof process.env.CREATE_FORUM_TYPE_IDS === 'string' ? process.env.CREATE_FORUM_TYPE_IDS.split(',') : process.env.CREATE_FORUM_TYPE_IDS
2120

src/services/challenges.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ export async function fetchChallengeTags () {
5656
* @param filters
5757
* @returns {Promise<*>}
5858
*/
59-
export async function fetchGroups (filters) {
60-
const finalFilters = {
61-
...filters,
62-
perPage: GROUPS_DROPDOWN_PER_PAGE // make sure that we are retrieving all the groups
63-
}
64-
const response = await axiosInstance.get(`${GROUPS_API_URL}?${qs.stringify(finalFilters, { encode: false })}`)
59+
export async function fetchGroups (filters, params = '') {
60+
const finalFilters = filters && Object.keys(filters).length > 0
61+
? {
62+
...filters,
63+
perPage: GROUPS_DROPDOWN_PER_PAGE // make sure that we are retrieving all the groups
64+
}
65+
: {}
66+
const response = await axiosInstance.get(`${GROUPS_API_URL}${params}?${qs.stringify(finalFilters, { encode: false })}`)
6567
return _.get(response, 'data', [])
6668
}
6769

0 commit comments

Comments
 (0)