@@ -8,7 +8,7 @@ import PT from 'prop-types'
8
8
import moment from 'moment'
9
9
import _ from 'lodash'
10
10
import cn from 'classnames'
11
- import { getTCMemberURL } from '../../../config/constants'
11
+ import { getTCMemberURL , CHALLENGE_STATUS } from '../../../config/constants'
12
12
import ReactSVG from 'react-svg'
13
13
import { getRatingLevel , sortList } from '../../../util/tc'
14
14
import { getCurrentPhase } from '../../../util/phase'
@@ -81,7 +81,13 @@ export default class Resources extends React.Component {
81
81
}
82
82
83
83
componentDidUpdate ( prevProps ) {
84
- const { resources, resourcesSort, submissions, challenge } = this . props
84
+ const {
85
+ resources,
86
+ resourcesSort,
87
+ submissions,
88
+ challenge,
89
+ loggedInUserResource
90
+ } = this . props
85
91
if (
86
92
! _ . isEqual ( prevProps . resources , resources ) ||
87
93
! _ . isEqual ( prevProps . resourcesSort , resourcesSort )
@@ -91,7 +97,8 @@ export default class Resources extends React.Component {
91
97
if (
92
98
! _ . isEqual ( prevProps . submissions , submissions ) ||
93
99
! _ . isEqual ( prevProps . challenge , challenge ) ||
94
- ! _ . isEqual ( prevProps . resources , resources )
100
+ ! _ . isEqual ( prevProps . resources , resources ) ||
101
+ ! _ . isEqual ( prevProps . loggedInUserResource , loggedInUserResource )
95
102
) {
96
103
this . updateExceptionHandlesDelete ( )
97
104
}
@@ -143,30 +150,50 @@ export default class Resources extends React.Component {
143
150
144
151
/**
145
152
* Update exception handles delete
146
- * Don't allow deletion of submitters who submitted, or creator of challenge
147
153
*/
148
154
updateExceptionHandlesDelete ( ) {
149
- const { submissions, challenge, resources } = this . props
155
+ const {
156
+ submissions,
157
+ challenge,
158
+ resources,
159
+ loggedInUserResource
160
+ } = this . props
150
161
const currentPhase = getCurrentPhase ( challenge ) . toLowerCase ( )
151
162
const isCurrentPhasesNotSubmissionOrRegistration = _ . every ( [ 'submission' , 'registration' ] , ( phase ) => currentPhase . indexOf ( phase ) < 0 )
152
163
const exceptionHandlesDeleteList = { }
153
- // do not allow to delete owner of challenge
164
+ // The creator of the challenge can't be deleted
154
165
exceptionHandlesDeleteList [ challenge . createdBy ] = true
155
166
_ . forEach ( submissions , ( s ) => {
156
- // do not allow to delete member that submit submission
167
+ // do not allow to delete submitters who submitted
157
168
exceptionHandlesDeleteList [ s . createdBy ] = true
158
169
} )
159
170
160
171
const exceptionResourceIdDeleteList = { }
161
172
_ . forEach ( resources , ( resourceItem ) => {
173
+ if ( exceptionHandlesDeleteList [ resourceItem . memberHandle ] ) {
174
+ exceptionResourceIdDeleteList [ resourceItem . id ] = true
175
+ }
162
176
if (
163
- (
164
- // If the current phase is not submission or registration
165
- // then we will disable removing reviewers and copilots.
166
- _ . some ( [ 'reviewer' , 'copilot' ] , ( role ) => `${ resourceItem . role } ` . toLowerCase ( ) . indexOf ( role ) >= 0 ) &&
167
- isCurrentPhasesNotSubmissionOrRegistration
168
- ) ||
169
- exceptionHandlesDeleteList [ resourceItem . memberHandle ]
177
+ // if the challenge is in New or Draft status
178
+ // we will allow removing reviewers and copilots
179
+ _ . some ( [
180
+ CHALLENGE_STATUS . DRAFT ,
181
+ CHALLENGE_STATUS . NEW
182
+ ] , ( status ) => challenge . status . toUpperCase ( ) === status )
183
+ ) {
184
+ if (
185
+ // Copilots can't delete themselves from the challenge
186
+ loggedInUserResource &&
187
+ _ . some ( loggedInUserResource . roles , ( role ) => `${ role } ` . toLowerCase ( ) . indexOf ( 'copilot' ) >= 0 ) &&
188
+ loggedInUserResource . memberHandle === resourceItem . memberHandle
189
+ ) {
190
+ exceptionResourceIdDeleteList [ resourceItem . id ] = true
191
+ }
192
+ } else if (
193
+ // If the current phase is not submission or registration
194
+ // then we will disable removing reviewers and copilots.
195
+ _ . some ( [ 'reviewer' , 'copilot' ] , ( role ) => `${ resourceItem . role } ` . toLowerCase ( ) . indexOf ( role ) >= 0 ) &&
196
+ isCurrentPhasesNotSubmissionOrRegistration
170
197
) {
171
198
exceptionResourceIdDeleteList [ resourceItem . id ] = true
172
199
}
@@ -430,7 +457,8 @@ Resources.defaultProps = {
430
457
results : [ ] ,
431
458
checkpointResults : { } ,
432
459
resourcesSort : { } ,
433
- submissions : [ ]
460
+ submissions : [ ] ,
461
+ loggedInUserResource : null
434
462
}
435
463
436
464
Resources . propTypes = {
@@ -458,5 +486,6 @@ Resources.propTypes = {
458
486
sort : PT . string
459
487
} ) ,
460
488
canEditResource : PT . bool . isRequired ,
461
- deleteResource : PT . func . isRequired
489
+ deleteResource : PT . func . isRequired ,
490
+ loggedInUserResource : PT . any
462
491
}
0 commit comments