From 9be996744f6dd37f286502e8c336e0f4074cf6a6 Mon Sep 17 00:00:00 2001 From: flaming-cl Date: Wed, 24 Nov 2021 10:57:59 +0800 Subject: [PATCH 1/6] issue 102: submit without registration --- src/containers/Submission/index.jsx | 49 +++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index 9c1e1ea..7af1910 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -1,5 +1,6 @@ -import React, { useEffect, useLayoutEffect, useRef } from "react"; +import React, { useEffect, useLayoutEffect, useRef, useState } from "react"; import PT from "prop-types"; +import _ from "lodash"; import { connect } from "react-redux"; import { navigate } from "@reach/router"; import { PrimaryButton } from "components/Buttons"; @@ -25,7 +26,7 @@ const Submission = ({ getCommunityList, isLoadingChallenge, isChallengeLoaded, - + tokens, track, agreed, filePickers, @@ -39,6 +40,7 @@ const Submission = ({ uploadProgress, getChallenge, + getChallengeDetails, submit, resetForm, setAgreed, @@ -49,6 +51,13 @@ const Submission = ({ setSubmissionFilestackData, setAuth, }) => { + + const [registered, setRegistered] = useState(isRegistered); + + useEffect(() => { + setRegistered(isRegistered); + }, [isRegistered]); + const propsRef = useRef(); propsRef.current = { id, @@ -82,16 +91,31 @@ const Submission = ({ return null; } - if (!isRegistered) { + if (!registered) { return ( - - - Go to Challenge Details - - + + + Go to Challenge Details + + ); } + const checkIfRegistered = async () => { + const challengeDetail = await getChallengeDetails(tokens, challengeId); + const registrants = challengeDetail?.payload?.registrants; + return _.some(registrants, (r) => `${r.memberId}` === `${userId}`); + } + + const handleSubmit = async (data) => { + const registered = await checkIfRegistered(); + if (registered) { + submit(data); + } else { + setRegistered(registered); + } + } + return ( ); }; @@ -164,12 +188,14 @@ Submission.propTypes = { setFilePickerDragged: PT.func, setSubmissionFilestackData: PT.func, setAuth: PT.func, + registered: PT.bool, }; const mapStateToProps = (state, ownProps) => { const challenge = state?.challenge?.challenge; return { + tokens: state.auth, id: ownProps.challengeId, challengeId: challenge?.id, challengeName: challenge?.name, @@ -213,6 +239,11 @@ const mapDispatchToProps = (dispatch) => { dispatch(actions.challenge.getChallengeInit(challengeId)); dispatch(actions.challenge.getChallengeDone(challengeId)); }, + getChallengeDetails: (tokens, challengeId) => { + const a = actions.challenge; + const { tokenV3, tokenV2 } = tokens; + return dispatch(a.getFullDetailsDone(challengeId, tokenV3, tokenV2)); + }, submit: (data) => { dispatch(actions.submission.submit.submitInit()); dispatch(actions.submission.submit.submitDone(data, onProgress)); From 9f7f3a5076270157ae3e11ecdf4f1efd0535fb0f Mon Sep 17 00:00:00 2001 From: flaming-cl Date: Wed, 24 Nov 2021 16:42:55 +0800 Subject: [PATCH 2/6] issue 102: submit without registration - code updated --- src/containers/Submission/index.jsx | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index 7af1910..788a338 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -52,11 +52,7 @@ const Submission = ({ setAuth, }) => { - const [registered, setRegistered] = useState(isRegistered); - - useEffect(() => { - setRegistered(isRegistered); - }, [isRegistered]); + const [registrationCanceled, setCanceled] = useState(false); const propsRef = useRef(); propsRef.current = { @@ -91,7 +87,7 @@ const Submission = ({ return null; } - if (!registered) { + if (!isRegistered || registrationCanceled) { return ( @@ -101,18 +97,18 @@ const Submission = ({ ); } - const checkIfRegistered = async () => { + const ifStillRegistered = async () => { const challengeDetail = await getChallengeDetails(tokens, challengeId); - const registrants = challengeDetail?.payload?.registrants; + const registrants = challengeDetail?.payload?.registrants || []; return _.some(registrants, (r) => `${r.memberId}` === `${userId}`); } const handleSubmit = async (data) => { - const registered = await checkIfRegistered(); - if (registered) { + const stillRegistered = await ifStillRegistered(); + if (stillRegistered) { submit(data); } else { - setRegistered(registered); + setCanceled(true); } } @@ -188,7 +184,7 @@ Submission.propTypes = { setFilePickerDragged: PT.func, setSubmissionFilestackData: PT.func, setAuth: PT.func, - registered: PT.bool, + registrationCanceled: PT.bool, }; const mapStateToProps = (state, ownProps) => { From 0856318b5770907c3f7197d4847e879ca6b77d4e Mon Sep 17 00:00:00 2001 From: flaming-cl Date: Fri, 26 Nov 2021 16:18:12 +0800 Subject: [PATCH 3/6] issue 102: use get challenge API --- src/containers/Submission/index.jsx | 37 ++++++++++------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index 788a338..6dee369 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -1,6 +1,5 @@ -import React, { useEffect, useLayoutEffect, useRef, useState } from "react"; +import React, { useEffect, useLayoutEffect, useRef } from "react"; import PT from "prop-types"; -import _ from "lodash"; import { connect } from "react-redux"; import { navigate } from "@reach/router"; import { PrimaryButton } from "components/Buttons"; @@ -26,7 +25,6 @@ const Submission = ({ getCommunityList, isLoadingChallenge, isChallengeLoaded, - tokens, track, agreed, filePickers, @@ -52,8 +50,6 @@ const Submission = ({ setAuth, }) => { - const [registrationCanceled, setCanceled] = useState(false); - const propsRef = useRef(); propsRef.current = { id, @@ -87,7 +83,7 @@ const Submission = ({ return null; } - if (!isRegistered || registrationCanceled) { + if (!isRegistered) { return ( @@ -98,18 +94,14 @@ const Submission = ({ } const ifStillRegistered = async () => { - const challengeDetail = await getChallengeDetails(tokens, challengeId); - const registrants = challengeDetail?.payload?.registrants || []; - return _.some(registrants, (r) => `${r.memberId}` === `${userId}`); + const onCheck = true; + const challenge = await getChallenge(challengeId, onCheck); + return challenge?.payload?.isRegistered; } const handleSubmit = async (data) => { - const stillRegistered = await ifStillRegistered(); - if (stillRegistered) { - submit(data); - } else { - setCanceled(true); - } + const registered = await ifStillRegistered(); + if (registered) submit(data); } return ( @@ -184,14 +176,12 @@ Submission.propTypes = { setFilePickerDragged: PT.func, setSubmissionFilestackData: PT.func, setAuth: PT.func, - registrationCanceled: PT.bool, }; const mapStateToProps = (state, ownProps) => { const challenge = state?.challenge?.challenge; return { - tokens: state.auth, id: ownProps.challengeId, challengeId: challenge?.id, challengeName: challenge?.name, @@ -231,14 +221,11 @@ const mapDispatchToProps = (dispatch) => { setAuth: () => { dispatch(actions.auth.setAuthDone()); }, - getChallenge: (challengeId) => { - dispatch(actions.challenge.getChallengeInit(challengeId)); - dispatch(actions.challenge.getChallengeDone(challengeId)); - }, - getChallengeDetails: (tokens, challengeId) => { - const a = actions.challenge; - const { tokenV3, tokenV2 } = tokens; - return dispatch(a.getFullDetailsDone(challengeId, tokenV3, tokenV2)); + getChallenge: (challengeId, onRegistrationCheck) => { + if (!onRegistrationCheck) { + dispatch(actions.challenge.getChallengeInit(challengeId)); + } + return dispatch(actions.challenge.getChallengeDone(challengeId)); }, submit: (data) => { dispatch(actions.submission.submit.submitInit()); From b2434b0e8730bd1aeb2c4fe2d840e535b858ebdb Mon Sep 17 00:00:00 2001 From: flaming-cl Date: Fri, 26 Nov 2021 16:24:26 +0800 Subject: [PATCH 4/6] issue 102: use get challenge API - code updated --- src/containers/Submission/index.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index 6dee369..f0d4a7b 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -38,7 +38,6 @@ const Submission = ({ uploadProgress, getChallenge, - getChallengeDetails, submit, resetForm, setAgreed, From 79e7fc81146ceeb571261e23defbb0729bd7e015 Mon Sep 17 00:00:00 2001 From: flaming-cl Date: Fri, 26 Nov 2021 19:22:38 +0800 Subject: [PATCH 5/6] issue_102: updated code with eslint --- src/containers/Submission/index.jsx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/containers/Submission/index.jsx b/src/containers/Submission/index.jsx index f0d4a7b..032f71d 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -48,7 +48,6 @@ const Submission = ({ setSubmissionFilestackData, setAuth, }) => { - const propsRef = useRef(); propsRef.current = { id, @@ -84,11 +83,11 @@ const Submission = ({ if (!isRegistered) { return ( - - - Go to Challenge Details - - + + + Go to Challenge Details + + ); } @@ -96,12 +95,12 @@ const Submission = ({ const onCheck = true; const challenge = await getChallenge(challengeId, onCheck); return challenge?.payload?.isRegistered; - } + }; const handleSubmit = async (data) => { const registered = await ifStillRegistered(); if (registered) submit(data); - } + }; return ( Date: Sat, 27 Nov 2021 10:02:40 +0800 Subject: [PATCH 6/6] issue_102: only fetch registration info --- src/actions/challenge.js | 15 +++++++++++++++ src/containers/Submission/index.jsx | 22 ++++++++++------------ src/reducers/challenge.js | 25 +++++++++++++++++++++++++ src/services/challenge.js | 1 + 4 files changed, 51 insertions(+), 12 deletions(-) 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 032f71d..6653af9 100644 --- a/src/containers/Submission/index.jsx +++ b/src/containers/Submission/index.jsx @@ -37,6 +37,7 @@ const Submission = ({ submitDone, uploadProgress, + getIsRegistered, getChallenge, submit, resetForm, @@ -91,14 +92,8 @@ const Submission = ({ ); } - const ifStillRegistered = async () => { - const onCheck = true; - const challenge = await getChallenge(challengeId, onCheck); - return challenge?.payload?.isRegistered; - }; - const handleSubmit = async (data) => { - const registered = await ifStillRegistered(); + const registered = await getIsRegistered(challengeId, userId); if (registered) submit(data); }; @@ -165,6 +160,7 @@ Submission.propTypes = { uploadProgress: PT.number, getChallenge: PT.func, + getIsRegistered: PT.func, submit: PT.func, resetForm: PT.func, setAgreed: PT.func, @@ -219,11 +215,13 @@ const mapDispatchToProps = (dispatch) => { setAuth: () => { dispatch(actions.auth.setAuthDone()); }, - getChallenge: (challengeId, onRegistrationCheck) => { - if (!onRegistrationCheck) { - dispatch(actions.challenge.getChallengeInit(challengeId)); - } - return dispatch(actions.challenge.getChallengeDone(challengeId)); + 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)); }, submit: (data) => { dispatch(actions.submission.submit.submitInit()); 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 };