@@ -29,7 +29,7 @@ import {
29
29
} from '../../actions/challenges'
30
30
31
31
import { connect } from 'react-redux'
32
- import { SUBMITTER_ROLE_UUID } from '../../config/constants'
32
+ import { SUBMITTER_ROLE_UUID , MESSAGE } from '../../config/constants'
33
33
import { patchChallenge } from '../../services/challenges'
34
34
import ConfirmationModal from '../../components/Modal/ConfirmationModal'
35
35
import AlertModal from '../../components/Modal/AlertModal'
@@ -42,13 +42,23 @@ class ChallengeEditor extends Component {
42
42
constructor ( props ) {
43
43
super ( props )
44
44
const mountedWithCreatePage = props . match . path . endsWith ( '/new' )
45
- this . state = { mountedWithCreatePage, isLaunching : false , showSuccessModal : false , showLaunchModal : false }
45
+ this . state = {
46
+ challengeDetails : props . challengeDetails ,
47
+ mountedWithCreatePage,
48
+ isLaunching : false ,
49
+ showSuccessModal : false ,
50
+ showLaunchModal : false
51
+ }
46
52
47
53
this . onLaunchChallenge = this . onLaunchChallenge . bind ( this )
48
54
this . activateChallenge = this . activateChallenge . bind ( this )
49
55
this . closeLaunchModal = this . closeLaunchModal . bind ( this )
56
+ this . closeCloseTaskModal = this . closeCloseTaskModal . bind ( this )
50
57
this . closeSuccessModal = this . closeSuccessModal . bind ( this )
58
+ this . onCloseTask = this . onCloseTask . bind ( this )
59
+ this . closeTask = this . closeTask . bind ( this )
51
60
}
61
+
52
62
componentDidMount ( ) {
53
63
const {
54
64
match,
@@ -95,6 +105,8 @@ class ChallengeEditor extends Component {
95
105
const challengeId = _ . get ( newMatch . params , 'challengeId' , null )
96
106
if ( _ . get ( match . params , 'projectId' , null ) !== projectId || _ . get ( match . params , 'challengeId' , null ) !== challengeId ) {
97
107
this . fetchChallengeDetails ( newMatch , loadChallengeDetails , loadResources )
108
+ } else {
109
+ this . setState ( { challengeDetails : nextProps . challengeDetails } )
98
110
}
99
111
}
100
112
@@ -124,10 +136,18 @@ class ChallengeEditor extends Component {
124
136
this . setState ( { showLaunchModal : true } )
125
137
}
126
138
139
+ onCloseTask ( ) {
140
+ this . setState ( { showCloseTaskModal : true } )
141
+ }
142
+
127
143
closeLaunchModal ( ) {
128
144
this . setState ( { showLaunchModal : false } )
129
145
}
130
146
147
+ closeCloseTaskModal ( ) {
148
+ this . setState ( { showCloseTaskModal : false } )
149
+ }
150
+
131
151
closeSuccessModal ( ) {
132
152
this . setState ( { showSuccessModal : false } )
133
153
}
@@ -137,20 +157,63 @@ class ChallengeEditor extends Component {
137
157
const { challengeDetails } = this . props
138
158
try {
139
159
this . setState ( { isLaunching : true } )
140
- await patchChallenge ( challengeDetails . id , { status : 'Active' } )
141
- this . setState ( { isLaunching : false , showLaunchModal : false , showSuccessModal : true } )
160
+ const response = await patchChallenge ( challengeDetails . id , { status : 'Active' } )
161
+ this . setState ( {
162
+ isLaunching : false ,
163
+ showLaunchModal : false ,
164
+ showSuccessModal : true ,
165
+ suceessMessage : MESSAGE . CHALLENGE_LAUNCH_SUCCESS ,
166
+ challengeDetails : { ...challengeDetails , status : response . status }
167
+ } )
142
168
} catch ( e ) {
143
169
const error = _ . get ( e , 'response.data.message' , 'Unable to activate the challenge' )
144
170
this . setState ( { isLaunching : false , showLaunchModal : false , launchError : error } )
145
171
}
146
172
}
147
173
174
+ /**
175
+ * Close task when user confirm it
176
+ */
177
+ async closeTask ( ) {
178
+ const { challengeResources } = this . props
179
+ const { challengeDetails } = this . state
180
+ const submitters = challengeResources && challengeResources . filter ( cr => cr . roleId === SUBMITTER_ROLE_UUID )
181
+ var assignedMemberDetails = null
182
+ if ( submitters && submitters . length === 1 ) {
183
+ assignedMemberDetails = {
184
+ userId : submitters [ 0 ] . memberId ,
185
+ handle : submitters [ 0 ] . memberHandle
186
+ }
187
+ }
188
+
189
+ // set assigned user as the only one winner
190
+ const winners = [ {
191
+ userId : assignedMemberDetails . userId ,
192
+ handle : assignedMemberDetails . handle ,
193
+ placement : 1
194
+ } ]
195
+ try {
196
+ this . setState ( { isLaunching : true } )
197
+ const response = await patchChallenge ( challengeDetails . id , { winners, status : 'Completed' } )
198
+ this . setState ( {
199
+ isLaunching : false ,
200
+ showCloseTaskModal : false ,
201
+ showSuccessModal : true ,
202
+ suceessMessage : MESSAGE . TASK_CLOSE_SUCCESS ,
203
+ challengeDetails : { ...challengeDetails , status : response . status }
204
+ } )
205
+ } catch ( e ) {
206
+ const error = _ . get ( e , 'response.data.message' , 'Unable to close the task' )
207
+ this . setState ( { isLaunching : false , showCloseTaskModal : false , launchError : error } )
208
+ }
209
+ }
210
+
148
211
render ( ) {
149
212
const {
150
213
match,
151
214
isLoading,
152
215
isProjectLoading,
153
- challengeDetails,
216
+ // challengeDetails,
154
217
challengeResources,
155
218
metadata,
156
219
createAttachment,
@@ -165,7 +228,15 @@ class ChallengeEditor extends Component {
165
228
replaceResourceInRole
166
229
// members
167
230
} = this . props
168
- const { mountedWithCreatePage, isLaunching, showLaunchModal, showSuccessModal } = this . state
231
+ const {
232
+ mountedWithCreatePage,
233
+ isLaunching,
234
+ showLaunchModal,
235
+ showCloseTaskModal,
236
+ showSuccessModal,
237
+ suceessMessage,
238
+ challengeDetails
239
+ } = this . state
169
240
if ( isProjectLoading || isLoading ) return < Loader />
170
241
const challengeId = _ . get ( match . params , 'challengeId' , null )
171
242
if ( challengeId && ( ! challengeDetails || ! challengeDetails . id ) ) {
@@ -191,15 +262,25 @@ class ChallengeEditor extends Component {
191
262
onCancel = { this . closeLaunchModal }
192
263
onConfirm = { this . activateChallenge }
193
264
/>
265
+ const closeTaskModal = < ConfirmationModal
266
+ title = 'Confirm Close Task'
267
+ message = { `Do you want to close task "${ challengeDetails . name } "?` }
268
+ theme = { theme }
269
+ isProcessing = { isLaunching }
270
+ errorMessage = { this . state . launchError }
271
+ onCancel = { this . closeCloseTaskModal }
272
+ onConfirm = { this . closeTask }
273
+ />
194
274
const successModal = < AlertModal
195
275
title = 'Success'
196
- message = 'Challenge is activated successfully'
276
+ message = { suceessMessage }
197
277
theme = { theme }
198
278
closeText = 'Ok'
199
279
onClose = { this . closeSuccessModal }
200
280
/>
201
281
return < div >
202
282
{ showLaunchModal && activateModal }
283
+ { showCloseTaskModal && closeTaskModal }
203
284
{ showSuccessModal && successModal }
204
285
< Route
205
286
exact
@@ -269,6 +350,7 @@ class ChallengeEditor extends Component {
269
350
assignedMemberDetails = { assignedMemberDetails }
270
351
enableEdit = { enableEdit }
271
352
onLaunchChallenge = { this . onLaunchChallenge }
353
+ onCloseTask = { this . onCloseTask }
272
354
/>
273
355
) )
274
356
} />
0 commit comments