Skip to content

Commit ccc4f13

Browse files
authored
<fix> issue 102: submit without registration (#197)
1 parent c5d930a commit ccc4f13

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/actions/challenge.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ function getChallengeDone(challengeId) {
454454
return challengeService.getChallenge(challengeId);
455455
}
456456

457+
/**
458+
* @static
459+
* @desc Check if a user has registered a challenge
460+
* @param {String} challengeId Challenge ID.
461+
* @param {String} userId User Id.
462+
* @return {Action}
463+
*/
464+
async function getIsRegistered(challengeId, userId) {
465+
const registrants = await challengeService.getChallengeRegistrants(challengeId);
466+
const isRegistered = _.some(registrants, (r) => `${r.memberId}` === `${userId}`);
467+
return { isRegistered };
468+
}
469+
470+
457471
export default createActions({
458472
CHALLENGE: {
459473
DROP_CHECKPOINTS: dropCheckpoints,
@@ -483,5 +497,6 @@ export default createActions({
483497
GET_SUBMISSION_INFORMATION_DONE: getSubmissionInformationDone,
484498
GET_CHALLENGE_INIT: _.noop,
485499
GET_CHALLENGE_DONE: getChallengeDone,
500+
GET_IS_REGISTERED: getIsRegistered,
486501
},
487502
});

src/containers/Submission/index.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const Submission = ({
2525
getCommunityList,
2626
isLoadingChallenge,
2727
isChallengeLoaded,
28-
2928
track,
3029
agreed,
3130
filePickers,
@@ -38,6 +37,7 @@ const Submission = ({
3837
submitDone,
3938
uploadProgress,
4039

40+
getIsRegistered,
4141
getChallenge,
4242
submit,
4343
resetForm,
@@ -92,6 +92,11 @@ const Submission = ({
9292
);
9393
}
9494

95+
const handleSubmit = async (data) => {
96+
const registered = await getIsRegistered(challengeId, userId);
97+
if (registered) submit(data);
98+
};
99+
95100
return (
96101
<Submit
97102
challengeId={challengeId}
@@ -119,7 +124,7 @@ const Submission = ({
119124
setFilePickerUploadProgress={setFilePickerUploadProgress}
120125
setFilePickerDragged={setFilePickerDragged}
121126
setSubmissionFilestackData={setSubmissionFilestackData}
122-
submit={submit}
127+
submit={handleSubmit}
123128
/>
124129
);
125130
};
@@ -155,6 +160,7 @@ Submission.propTypes = {
155160
uploadProgress: PT.number,
156161

157162
getChallenge: PT.func,
163+
getIsRegistered: PT.func,
158164
submit: PT.func,
159165
resetForm: PT.func,
160166
setAgreed: PT.func,
@@ -209,6 +215,10 @@ const mapDispatchToProps = (dispatch) => {
209215
setAuth: () => {
210216
dispatch(actions.auth.setAuthDone());
211217
},
218+
getIsRegistered: async (challengeId, userId) => {
219+
const action = await dispatch(actions.challenge.getIsRegistered(challengeId, userId));
220+
return action?.payload?.isRegistered;
221+
},
212222
getChallenge: (challengeId) => {
213223
dispatch(actions.challenge.getChallengeInit(challengeId));
214224
dispatch(actions.challenge.getChallengeDone(challengeId));

src/reducers/challenge.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,30 @@ function onGetChallengeDone(state, { error, payload }) {
472472
};
473473
}
474474

475+
/**
476+
* Update isRegistered to before challenge submit
477+
* @param {Object} state Old state.
478+
* @param {Object} actions Action error/payload.
479+
* @param {Object} action Action.
480+
*/
481+
function onGetIsRegistered(state, { error, payload }) {
482+
if (error) {
483+
logger.error("Failed to get the user's registration status!", payload);
484+
fireErrorMessage(
485+
"ERROR: Failed to submit",
486+
"Please, try again a bit later"
487+
);
488+
return state;
489+
}
490+
return {
491+
...state,
492+
challenge: {
493+
...state.challenge,
494+
isRegistered: payload.isRegistered
495+
}
496+
};
497+
}
498+
475499
/**
476500
* Creates a new Challenge reducer with the specified initial state.
477501
* @param {Object} initialState Optional. Initial state.
@@ -520,6 +544,7 @@ function create(initialState) {
520544
[a.getSubmissionInformationDone]: onGetSubmissionInformationDone,
521545
[a.getChallengeInit]: onGetChallengeInit,
522546
[a.getChallengeDone]: onGetChallengeDone,
547+
[a.getIsRegistered]: onGetIsRegistered,
523548
},
524549
_.defaults(initialState, {
525550
details: null,

src/services/challenge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@ async function getChallenge(challengeId) {
113113

114114
export default {
115115
getChallenge,
116+
getChallengeRegistrants
116117
};

0 commit comments

Comments
 (0)