1- import React from 'react'
2- import PropTypes from 'prop-types'
3- import { PrimaryButton } from '../../Buttons'
4- import styles from './Copilot-Field.module.scss'
51import cn from 'classnames'
62import _ from 'lodash'
3+ import PropTypes from 'prop-types'
4+ import React from 'react'
5+
6+ import { PrimaryButton } from '../../Buttons'
77import CopilotCard from '../../CopilotCard'
88
9- const CopilotField = ( { copilots, challenge, onUpdateOthers, readOnly, assignYourselfCopilot } ) => {
9+ import styles from './Copilot-Field.module.scss'
10+
11+ const CopilotField = ( { copilots, challenge, onUpdateOthers, readOnly, assignYourselfCopilot, loggedInUser } ) => {
1012 let errMessage = 'Please set a copilot'
1113 const handleProperty = copilots . handle ? 'handle' : 'memberHandle'
1214 const selectedCopilot = _ . find ( copilots , { [ handleProperty ] : challenge . copilot } )
1315 const selectedCopilotHandle = selectedCopilot ? selectedCopilot [ handleProperty ] : undefined
1416 const copilotFee = _ . find ( challenge . prizeSets , p => p . type === 'copilot' , [ ] )
1517 const selfService = challenge . selfService
18+ const copilotIsSelf = loggedInUser && selectedCopilotHandle === loggedInUser . handle
19+ const assignButtonText = `${ selectedCopilot && copilotIsSelf ? 'Una' : 'A' } ssign Yourself`
20+ const showAssignButton = loggedInUser && ( ! selectedCopilotHandle || copilotIsSelf )
1621
1722 if ( readOnly ) {
1823 return (
@@ -22,11 +27,13 @@ const CopilotField = ({ copilots, challenge, onUpdateOthers, readOnly, assignYou
2227 </ div >
2328 { ( selectedCopilot || selfService ) && ( < div className = { cn ( styles . field , styles . col2 ) } >
2429 { ( selectedCopilot && < CopilotCard copilot = { selectedCopilot } selectedCopilot = { challenge . copilot } key = { selectedCopilotHandle } /> ) }
25- { ( selfService && ! selectedCopilot && < PrimaryButton
26- text = 'Assign Yourself'
27- type = 'info'
28- onClick = { assignYourselfCopilot }
29- /> ) }
30+ { ( ( selfService && showAssignButton ) && < div >
31+ < PrimaryButton
32+ text = { assignButtonText }
33+ type = 'info'
34+ onClick = { assignYourselfCopilot }
35+ />
36+ </ div > ) }
3037 </ div > ) }
3138 </ div >
3239 )
@@ -67,7 +74,8 @@ CopilotField.propTypes = {
6774 challenge : PropTypes . shape ( ) . isRequired ,
6875 onUpdateOthers : PropTypes . func ,
6976 readOnly : PropTypes . bool ,
70- assignYourselfCopilot : PropTypes . func . isRequired
77+ assignYourselfCopilot : PropTypes . func . isRequired ,
78+ loggedInUser : PropTypes . object
7179}
7280
7381export default CopilotField
0 commit comments