@@ -2,37 +2,88 @@ import React from 'react'
22import PropTypes from 'prop-types'
33import styles from './CheckpointPrizes-Field.module.scss'
44import cn from 'classnames'
5- import { range } from 'lodash'
5+ import _ from 'lodash'
66import { validateValue } from '../../../util/input-check'
7- import { VALIDATION_VALUE_TYPE , PRIZE_SETS_TYPE , CHALLENGE_PRIZE_TYPE } from '../../../config/constants'
7+ import {
8+ VALIDATION_VALUE_TYPE ,
9+ PRIZE_SETS_TYPE ,
10+ CHALLENGE_PRIZE_TYPE ,
11+ MAX_CHECKPOINT_PRIZE_COUNT ,
12+ DEFAULT_CHECKPOINT_PRIZE ,
13+ DEFAULT_CHECKPOINT_PRIZE_COUNT
14+ } from '../../../config/constants'
15+ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
16+ import { faDollarSign } from '@fortawesome/free-solid-svg-icons'
17+ import Select from '../../Select'
818
9- const CheckpointPrizesField = ( { challenge, onUpdateOthers } ) => {
19+ const CheckpointPrizesField = ( { challenge, onUpdateOthers, readOnly } ) => {
1020 const type = PRIZE_SETS_TYPE . CHECKPOINT_PRIZES
11- const checkpointPrize = challenge . prizeSets . find ( p => p . type === type ) || { type : CHALLENGE_PRIZE_TYPE . USD , prizes : [ ] }
12- const number = checkpointPrize . prizes . length
13- const amount = checkpointPrize . prizes . length ? checkpointPrize . prizes [ 0 ] . value : 0
21+ const prizeSets = _ . get ( challenge , 'prizeSets' ) || [ ]
22+ const checkpointPrize = prizeSets . find ( p => p . type === type ) || { type : PRIZE_SETS_TYPE . CHECKPOINT_PRIZES , prizes : [ ] , 'description' : 'Checkpoint Prizes' }
23+ const number = _ . get ( checkpointPrize , 'prizes.length' ) || DEFAULT_CHECKPOINT_PRIZE_COUNT
24+ const amount = _ . get ( checkpointPrize , 'prizes.length' ) ? checkpointPrize . prizes [ 0 ] . value : DEFAULT_CHECKPOINT_PRIZE
25+
26+ // update the check point prize with default values if it's not already defined
27+ if ( _ . get ( checkpointPrize , 'prizes.length' ) === 0 ) {
28+ onChange ( number , amount )
29+ }
1430
1531 function onChange ( number , amount ) {
16- checkpointPrize . prizes = range ( validateValue ( number , VALIDATION_VALUE_TYPE . INTEGER ) )
17- . map ( i => ( { type : CHALLENGE_PRIZE_TYPE . USD , value : validateValue ( amount , VALIDATION_VALUE_TYPE . INTEGER , '$' ) } ) )
18- onUpdateOthers ( { field : 'prizeSets' , value : [ ...challenge . prizeSets . filter ( p => p . type !== type ) , + number && checkpointPrize ] . filter ( p => p ) } )
32+ checkpointPrize . prizes = _ . range ( validateValue ( number , VALIDATION_VALUE_TYPE . INTEGER ) )
33+ . map ( i => ( { type : CHALLENGE_PRIZE_TYPE . USD , value : + validateValue ( amount , VALIDATION_VALUE_TYPE . INTEGER ) } ) )
34+ onUpdateOthers ( { field : 'prizeSets' , value : [ ...prizeSets . filter ( p => p . type !== type ) , + number && checkpointPrize ] . filter ( p => p ) } )
1935 }
36+
2037 return (
21- < div className = { styles . row } >
22- < div className = { cn ( styles . field , styles . col1 ) } >
23- < label htmlFor = 'checkpointPrizes' > Checkpoint Prizes :</ label >
38+ < >
39+ < div className = { cn ( styles . row ) } >
40+ < div className = { cn ( styles . field , styles . col1 ) } >
41+ < label htmlFor = { `checkpointPrizes` } className = { styles . checkpointLabel } > Checkpoint Prizes :</ label >
42+ </ div >
43+ {
44+ readOnly ? (
45+ < div className = { cn ( styles . field , styles . col2 ) } >
46+ ${ amount } for each submission up to { number } submissions
47+ </ div >
48+ ) : (
49+ < div className = { cn ( styles . field , styles . col2 ) } >
50+ < div >
51+ < div className = { styles . checkpointPrizeInputContainer } >
52+ < div className = { styles . checkpointPrizeAmountContainer } >
53+ < FontAwesomeIcon className = { styles . dollarIcon } icon = { faDollarSign } />
54+ </ div >
55+ < input id = 'checkpointPrize' name = 'checkpointPrize' type = 'text' placeholder = '' value = { amount } maxLength = '7' required onChange = { ( e ) => onChange ( number , e . target . value ) } />
56+ </ div >
57+ </ div >
58+ < div >
59+ for each submission up to
60+ </ div >
61+ < div className = { styles . checkpointSelect } >
62+ < Select
63+ name = 'submissions'
64+ options = { _ . range ( 1 , MAX_CHECKPOINT_PRIZE_COUNT + 1 ) . map ( ( v ) => ( { label : v , value : v } ) ) }
65+ value = { { label : number , value : number } }
66+ isClearable = { false }
67+ onChange = { e => onChange ( e . value , amount ) }
68+ isDisabled = { false }
69+ />
70+ </ div >
71+ </ div >
72+ )
73+ }
2474 </ div >
25- < div className = { cn ( styles . field , styles . col2 ) } >
26- < input id = 'checkNumber' name = 'checkNumber' type = 'text' placeholder = 'Number of checkpoint prizes' value = { number } maxLength = '200' onChange = { e => onChange ( e . target . value , amount ) } />
27- < input id = 'checkAmount' name = 'checkAmount' type = 'text' placeholder = 'Amount per prizes' value = { amount } maxLength = '200' onChange = { e => onChange ( number , e . target . value ) } />
28- </ div >
29- </ div >
75+ </ >
3076 )
3177}
3278
79+ CheckpointPrizesField . defaultProps = {
80+ readOnly : false
81+ }
82+
3383CheckpointPrizesField . propTypes = {
3484 challenge : PropTypes . shape ( ) . isRequired ,
35- onUpdateOthers : PropTypes . func . isRequired
85+ onUpdateOthers : PropTypes . func ,
86+ readOnly : PropTypes . bool
3687}
3788
3889export default CheckpointPrizesField
0 commit comments