diff --git a/src/actions/challenge.js b/src/actions/challenge.js index 5256ad9..84272bd 100644 --- a/src/actions/challenge.js +++ b/src/actions/challenge.js @@ -454,6 +454,20 @@ function getChallengeDone(challengeId) { return challengeService.getChallenge(challengeId); } +/** + * @static + * @desc Check if a user has registered a challenge + * @param {String} challengeId Challenge ID. + * @param {String} userId User Id. + * @return {Action} + */ +async function getIsRegistered(challengeId, userId) { + const registrants = await challengeService.getChallengeRegistrants(challengeId); + const isRegistered = _.some(registrants, (r) => `${r.memberId}` === `${userId}`); + return { isRegistered }; +} + + export default createActions({ CHALLENGE: { DROP_CHECKPOINTS: dropCheckpoints, @@ -483,5 +497,6 @@ export default createActions({ GET_SUBMISSION_INFORMATION_DONE: getSubmissionInformationDone, GET_CHALLENGE_INIT: _.noop, GET_CHALLENGE_DONE: getChallengeDone, + GET_IS_REGISTERED: getIsRegistered, }, }); diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index 9c1e1ea..6653af9 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -25,7 +25,6 @@ const Submission = ({ getCommunityList, isLoadingChallenge, isChallengeLoaded, - track, agreed, filePickers, @@ -38,6 +37,7 @@ const Submission = ({ submitDone, uploadProgress, + getIsRegistered, getChallenge, submit, resetForm, @@ -92,6 +92,11 @@ const Submission = ({ ); } + const handleSubmit = async (data) => { + const registered = await getIsRegistered(challengeId, userId); + if (registered) submit(data); + }; + return ( ); }; @@ -155,6 +160,7 @@ Submission.propTypes = { uploadProgress: PT.number, getChallenge: PT.func, + getIsRegistered: PT.func, submit: PT.func, resetForm: PT.func, setAgreed: PT.func, @@ -209,6 +215,10 @@ const mapDispatchToProps = (dispatch) => { setAuth: () => { dispatch(actions.auth.setAuthDone()); }, + getIsRegistered: async (challengeId, userId) => { + const action = await dispatch(actions.challenge.getIsRegistered(challengeId, userId)); + return action?.payload?.isRegistered; + }, getChallenge: (challengeId) => { dispatch(actions.challenge.getChallengeInit(challengeId)); dispatch(actions.challenge.getChallengeDone(challengeId)); diff --git a/src/reducers/challenge.js b/src/reducers/challenge.js index 346db3e..5e213f8 100644 --- a/src/reducers/challenge.js +++ b/src/reducers/challenge.js @@ -472,6 +472,30 @@ function onGetChallengeDone(state, { error, payload }) { }; } +/** + * Update isRegistered to before challenge submit + * @param {Object} state Old state. + * @param {Object} actions Action error/payload. + * @param {Object} action Action. + */ +function onGetIsRegistered(state, { error, payload }) { + if (error) { + logger.error("Failed to get the user's registration status!", payload); + fireErrorMessage( + "ERROR: Failed to submit", + "Please, try again a bit later" + ); + return state; + } + return { + ...state, + challenge: { + ...state.challenge, + isRegistered: payload.isRegistered + } + }; +} + /** * Creates a new Challenge reducer with the specified initial state. * @param {Object} initialState Optional. Initial state. @@ -520,6 +544,7 @@ function create(initialState) { [a.getSubmissionInformationDone]: onGetSubmissionInformationDone, [a.getChallengeInit]: onGetChallengeInit, [a.getChallengeDone]: onGetChallengeDone, + [a.getIsRegistered]: onGetIsRegistered, }, _.defaults(initialState, { details: null, diff --git a/src/services/challenge.js b/src/services/challenge.js index 38e4c6b..adde73c 100644 --- a/src/services/challenge.js +++ b/src/services/challenge.js @@ -113,4 +113,5 @@ async function getChallenge(challengeId) { export default { getChallenge, + getChallengeRegistrants };