Skip to content

Commit f763b68

Browse files
author
vikasrohit
authored
Merge pull request #937 from topcoder-platform/develop
Fixing Close Task action
2 parents 4c60f19 + 1915927 commit f763b68

File tree

5 files changed

+51
-61
lines changed

5 files changed

+51
-61
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ npm-debug.log*
2424
yarn-debug.log*
2525
yarn-error.log*
2626

27-
*.env
27+
*.env
28+
29+
*.vscode

src/components/ChallengeEditor/ChallengeView/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Loader from '../../Loader'
2020
import PhaseInput from '../../PhaseInput'
2121
import LegacyLinks from '../../LegacyLinks'
2222
import AssignedMemberField from '../AssignedMember-Field'
23+
import { getResourceRoleByName } from '../../../util/tc'
2324

2425
const ChallengeView = ({
2526
projectDetail,
@@ -36,13 +37,9 @@ const ChallengeView = ({
3637

3738
const [openAdvanceSettings, setOpenAdvanceSettings] = useState(false)
3839

39-
const getResourceRoleByName = (name) => {
40-
const { resourceRoles } = metadata
41-
return resourceRoles ? resourceRoles.find(role => role.name === name) : null
42-
}
43-
4440
const getResourceFromProps = (name) => {
45-
const role = getResourceRoleByName(name)
41+
const { resourceRoles } = metadata
42+
const role = getResourceRoleByName(resourceRoles, name)
4643
return challengeResources && role && challengeResources.find(resource => resource.roleId === role.id)
4744
}
4845

src/components/ChallengeEditor/index.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import PhaseInput from '../PhaseInput'
4545
import LegacyLinks from '../LegacyLinks'
4646
import AssignedMemberField from './AssignedMember-Field'
4747
import Tooltip from '../Tooltip'
48+
import { getResourceRoleByName } from '../../util/tc'
4849

4950
const theme = {
5051
container: styles.modalContainer
@@ -115,6 +116,7 @@ class ChallengeEditor extends Component {
115116
this.getTemplatePhases = this.getTemplatePhases.bind(this)
116117
this.getAvailableTimelineTemplates = this.getAvailableTimelineTemplates.bind(this)
117118
this.autoUpdateChallengeThrottled = _.throttle(this.autoUpdateChallenge.bind(this), 3000) // 3s
119+
this.updateResource = this.updateResource.bind(this)
118120
}
119121

120122
componentDidMount () {
@@ -195,22 +197,25 @@ class ChallengeEditor extends Component {
195197
* Close task when user confirm it
196198
*/
197199
onCloseTask () {
198-
const { challenge: oldChallenge, assignedMemberDetails } = this.state
199-
200-
// set assigned user as the only one winner
201-
const newChallenge = {
202-
...oldChallenge,
203-
winners: [{
204-
userId: assignedMemberDetails.userId,
205-
handle: assignedMemberDetails.handle,
206-
placement: 1
207-
}]
208-
}
200+
// before marking challenge as complete, save all the changes user might have made
201+
this.updateAllChallengeInfo(this.state.challenge.status, () => {
202+
const { challenge: oldChallenge, assignedMemberDetails } = this.state
203+
204+
// set assigned user as the only one winner
205+
const newChallenge = {
206+
...oldChallenge,
207+
winners: [{
208+
userId: assignedMemberDetails.userId,
209+
handle: assignedMemberDetails.handle,
210+
placement: 1
211+
}]
212+
}
209213

210-
this.setState({
211-
challenge: newChallenge
212-
}, () => {
213-
this.updateAllChallengeInfo('Completed')
214+
this.setState({
215+
challenge: newChallenge
216+
}, () => {
217+
this.updateAllChallengeInfo('Completed')
218+
})
214219
})
215220
}
216221

@@ -959,13 +964,9 @@ class ChallengeEditor extends Component {
959964
this.updateAllChallengeInfo(this.state.challenge.status)
960965
}
961966

962-
getResourceRoleByName (name) {
963-
const { resourceRoles } = this.props.metadata
964-
return resourceRoles ? resourceRoles.find(role => role.name === name) : null
965-
}
966-
967967
async updateResource (challengeId, name, value, prevValue) {
968-
const resourceRole = this.getResourceRoleByName(name)
968+
const { resourceRoles } = this.props.metadata
969+
const resourceRole = getResourceRoleByName(resourceRoles, name)
969970
const roleId = resourceRole.id
970971
await this.props.replaceResourceInRole(challengeId, roleId, value, prevValue)
971972
}
@@ -986,8 +987,8 @@ class ChallengeEditor extends Component {
986987
}
987988

988989
getResourceFromProps (name) {
989-
const { challengeResources } = this.props
990-
const role = this.getResourceRoleByName(name)
990+
const { challengeResources, metadata: { resourceRoles } } = this.props
991+
const role = getResourceRoleByName(resourceRoles, name)
991992
return challengeResources && role && challengeResources.find(resource => resource.roleId === role.id)
992993
}
993994

src/containers/ChallengeEditor/index.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ import {
2727
createChallenge,
2828
replaceResourceInRole
2929
} from '../../actions/challenges'
30-
import {
31-
loadMemberDetails
32-
} from '../../actions/members'
3330

3431
import { connect } from 'react-redux'
3532
import { SUBMITTER_ROLE_UUID } from '../../config/constants'
@@ -80,36 +77,13 @@ class ChallengeEditor extends Component {
8077
}
8178

8279
componentWillReceiveProps (nextProps) {
83-
const { match, challengeDetails } = this.props
84-
const { match: newMatch, loadChallengeDetails, loadResources, challengeDetails: nextChallengeDetails } = nextProps
80+
const { match } = this.props
81+
const { match: newMatch, loadChallengeDetails, loadResources } = nextProps
8582
const projectId = _.get(newMatch.params, 'projectId', null)
8683
const challengeId = _.get(newMatch.params, 'challengeId', null)
8784
if (_.get(match.params, 'projectId', null) !== projectId || _.get(match.params, 'challengeId', null) !== challengeId) {
8885
this.fetchChallengeDetails(newMatch, loadChallengeDetails, loadResources)
8986
}
90-
91-
// this section is called only one time as soon challenge details are loaded
92-
if (
93-
_.get(challengeDetails, 'id') !== _.get(nextChallengeDetails, 'id') &&
94-
challengeId === _.get(nextChallengeDetails, 'id')
95-
) {
96-
this.loadAssignedMemberDetails(nextProps)
97-
}
98-
}
99-
100-
/**
101-
* Load assign member details if challenge has a member assigned
102-
* @param {Object} nextProps the latest props
103-
*/
104-
loadAssignedMemberDetails (nextProps) {
105-
// cannot use `loadMemberDetails` form the `nextProps` because linter complains about unused prop
106-
const { loadMemberDetails } = this.props
107-
const { challengeDetails } = nextProps
108-
const assignedMemberId = _.get(challengeDetails, 'task.memberId')
109-
110-
if (assignedMemberId) {
111-
loadMemberDetails(assignedMemberId)
112-
}
11387
}
11488

11589
async fetchChallengeDetails (newMatch, loadChallengeDetails, loadResources) {
@@ -280,7 +254,6 @@ ChallengeEditor.propTypes = {
280254
loggedInUser: PropTypes.object,
281255
removeAttachment: PropTypes.func,
282256
failedToLoad: PropTypes.bool,
283-
loadMemberDetails: PropTypes.func,
284257
updateChallengeDetails: PropTypes.func.isRequired,
285258
partiallyUpdateChallengeDetails: PropTypes.func.isRequired,
286259
createChallenge: PropTypes.func.isRequired,
@@ -317,7 +290,6 @@ const mapDispatchToProps = {
317290
// loadChallengeTerms,
318291
loadResources,
319292
loadResourceRoles,
320-
loadMemberDetails,
321293
updateChallengeDetails,
322294
partiallyUpdateChallengeDetails,
323295
createChallenge,

src/util/tc.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Topcoder related utilities
33
*/
4-
import { MARATHON_MATCH_SUBTRACKS, CHALLENGE_TRACKS, ALLOWED_USER_ROLES, ADMIN_ROLES } from '../config/constants'
4+
import { MARATHON_MATCH_SUBTRACKS, CHALLENGE_TRACKS, ALLOWED_USER_ROLES, ADMIN_ROLES, SUBMITTER_ROLE_UUID } from '../config/constants'
55
import _ from 'lodash'
66
import { decodeToken } from 'tc-auth-lib'
77

@@ -59,3 +59,21 @@ export const checkAdmin = (token) => {
5959
const roles = _.get(decodeToken(token), 'roles')
6060
return roles.some(val => ADMIN_ROLES.indexOf(val.toLowerCase()) > -1)
6161
}
62+
63+
/**
64+
* Get resource role by name
65+
*
66+
* @param {Object[]} resourceRoles list of resource roles
67+
* @param {String} name resource role name
68+
*
69+
* @returns {Object} resource role or `null`
70+
*/
71+
export const getResourceRoleByName = (resourceRoles, name) => {
72+
// there are multiple junk resource roles with 'Submitter' name,
73+
// so we use `id` from config to find the correct one
74+
if (name === 'Submitter') {
75+
return _.find(resourceRoles, { id: SUBMITTER_ROLE_UUID }) || null
76+
} else {
77+
return _.find(resourceRoles, { name }) || null
78+
}
79+
}

0 commit comments

Comments
 (0)