From c2ffd6471b91047206150548d72b227c307d0f03 Mon Sep 17 00:00:00 2001 From: Ahmad Abdolsaheb Date: Tue, 26 Mar 2019 17:54:18 +0300 Subject: [PATCH] fix: add redux wiring for legacy certs --- .../src/components/settings/Certification.js | 8 ++--- client/src/redux/index.js | 29 ++++++++++++++++-- .../update-legacy-certificate-saga.js | 30 +++++++++++-------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/client/src/components/settings/Certification.js b/client/src/components/settings/Certification.js index a3f0e34c0e..fec233fa40 100644 --- a/client/src/components/settings/Certification.js +++ b/client/src/components/settings/Certification.js @@ -299,14 +299,14 @@ class CertificationSettings extends Component { const isCertClaimed = this.getUserIsCertMap()[certName]; const initialObject = {}; let filledforms = 0; - legacyProjectMap[certName].forEach(element => { + legacyProjectMap[certName].forEach(project => { let completedProject = find(completedChallenges, function(challenge) { - return challenge['id'] === element['id']; + return challenge['id'] === project['id']; }); if (!completedProject) { - initialObject[element.title] = ''; + initialObject[project.title] = ''; } else { - initialObject[element.title] = completedProject.solution; + initialObject[project.title] = completedProject.solution; filledforms++; } }); diff --git a/client/src/redux/index.js b/client/src/redux/index.js index 17a89c0c66..5ffd32836f 100644 --- a/client/src/redux/index.js +++ b/client/src/redux/index.js @@ -291,7 +291,12 @@ export const reducer = handleActions( error: payload } }), - [types.submitComplete]: (state, { payload: { id } }) => { + [types.submitComplete]: (state, { payload: { id, challArray } }) => { + let submitedchallneges = [{ id }]; + if (challArray) { + submitedchallneges = challArray; + } + console.log(...submitedchallneges); const { appUsername } = state; return { ...state, @@ -301,7 +306,27 @@ export const reducer = handleActions( [appUsername]: { ...state.user[appUsername], completedChallenges: uniqBy( - [...state.user[appUsername].completedChallenges, { id }], + [ + ...submitedchallneges, + ...state.user[appUsername].completedChallenges + ], + 'id' + ) + } + } + }; + }, + [settingsTypes.updateLegacyCertificateComplete]: (state, { payload }) => { + const { appUsername } = state; + return { + ...state, + completionCount: state.completionCount + 1, + user: { + ...state.user, + [appUsername]: { + ...state.user[appUsername], + completedChallenges: uniqBy( + [...state.user[appUsername].completedChallenges, payload], 'id' ) } diff --git a/client/src/redux/settings/update-legacy-certificate-saga.js b/client/src/redux/settings/update-legacy-certificate-saga.js index 3fa41c75c8..690c67eb6c 100644 --- a/client/src/redux/settings/update-legacy-certificate-saga.js +++ b/client/src/redux/settings/update-legacy-certificate-saga.js @@ -1,13 +1,11 @@ import { takeEvery, select, call, put } from 'redux-saga/effects'; import { putUpdateLegacyCertificate } from '../../utils/ajax'; -import { completedChallengesSelector } from '../'; +import { completedChallengesSelector, submitComplete } from '../'; import { legacyProjectMap } from '../../resources/certProjectMap'; import { createFlashMessage } from '../../components/Flash/redux'; -import { - updateLegacyCertificateComplete, - updateLegacyCertificateError -} from './'; +import standardErrorMessage from '../../utils/reallyWeirdErrorMessage'; +import { updateLegacyCertificateError } from './'; const completedChallenges = state => completedChallengesSelector(state); @@ -29,6 +27,7 @@ function* updateLegacyCertificateSaga({ payload }) { break; } } + // make an object with keys as challenge ids and values as solutions let idsToSolutions = {}; for (let i of Object.keys(payload)) { @@ -40,9 +39,9 @@ function* updateLegacyCertificateSaga({ payload }) { } } } + // find how many challnegs have been updated and how many are new let completed = yield select(completedChallenges); - let newSubmissions = 0; let challengesToUpdate = {}; let newChalleneFound = true; for (let j of Object.keys(idsToSolutions)) { @@ -57,27 +56,32 @@ function* updateLegacyCertificateSaga({ payload }) { } if (newChalleneFound && idsToSolutions[j] !== '') { challengesToUpdate[j] = idsToSolutions[j]; - newSubmissions++; } newChalleneFound = true; } - console.log(newSubmissions); - // shape the body of the http calls so it is consumable by api + // shape the body of the http call so it is consumable by api const body = { projects: { [legacyCert]: challengesToUpdate } }; + // shape to update completed challenges + let reduxShape = []; + for (let obj in challengesToUpdate) { + if (challengesToUpdate.hasOwnProperty(obj)) { + reduxShape.push({ id: obj, solution: challengesToUpdate[obj] }); + } + } + try { - const response = yield call(putUpdateLegacyCertificate, body); - yield put( - updateLegacyCertificateComplete({ updatedChallenges: challengesToUpdate }) - ); + const { data: response } = yield call(putUpdateLegacyCertificate, body); + yield put(submitComplete({ challArray: reduxShape })); yield put(createFlashMessage(response)); } catch (e) { yield put(updateLegacyCertificateError(e)); + yield put(createFlashMessage(standardErrorMessage)); } }