fix: add redux wiring for legacy certs

This commit is contained in:
Ahmad Abdolsaheb
2019-03-26 17:54:18 +03:00
committed by Mrugesh Mohapatra
parent e8931ae1cb
commit c2ffd6471b
3 changed files with 48 additions and 19 deletions

View File

@ -299,14 +299,14 @@ class CertificationSettings extends Component {
const isCertClaimed = this.getUserIsCertMap()[certName]; const isCertClaimed = this.getUserIsCertMap()[certName];
const initialObject = {}; const initialObject = {};
let filledforms = 0; let filledforms = 0;
legacyProjectMap[certName].forEach(element => { legacyProjectMap[certName].forEach(project => {
let completedProject = find(completedChallenges, function(challenge) { let completedProject = find(completedChallenges, function(challenge) {
return challenge['id'] === element['id']; return challenge['id'] === project['id'];
}); });
if (!completedProject) { if (!completedProject) {
initialObject[element.title] = ''; initialObject[project.title] = '';
} else { } else {
initialObject[element.title] = completedProject.solution; initialObject[project.title] = completedProject.solution;
filledforms++; filledforms++;
} }
}); });

View File

@ -291,7 +291,12 @@ export const reducer = handleActions(
error: payload 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; const { appUsername } = state;
return { return {
...state, ...state,
@ -301,7 +306,27 @@ export const reducer = handleActions(
[appUsername]: { [appUsername]: {
...state.user[appUsername], ...state.user[appUsername],
completedChallenges: uniqBy( 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' 'id'
) )
} }

View File

@ -1,13 +1,11 @@
import { takeEvery, select, call, put } from 'redux-saga/effects'; import { takeEvery, select, call, put } from 'redux-saga/effects';
import { putUpdateLegacyCertificate } from '../../utils/ajax'; import { putUpdateLegacyCertificate } from '../../utils/ajax';
import { completedChallengesSelector } from '../'; import { completedChallengesSelector, submitComplete } from '../';
import { legacyProjectMap } from '../../resources/certProjectMap'; import { legacyProjectMap } from '../../resources/certProjectMap';
import { createFlashMessage } from '../../components/Flash/redux'; import { createFlashMessage } from '../../components/Flash/redux';
import { import standardErrorMessage from '../../utils/reallyWeirdErrorMessage';
updateLegacyCertificateComplete, import { updateLegacyCertificateError } from './';
updateLegacyCertificateError
} from './';
const completedChallenges = state => completedChallengesSelector(state); const completedChallenges = state => completedChallengesSelector(state);
@ -29,6 +27,7 @@ function* updateLegacyCertificateSaga({ payload }) {
break; break;
} }
} }
// make an object with keys as challenge ids and values as solutions // make an object with keys as challenge ids and values as solutions
let idsToSolutions = {}; let idsToSolutions = {};
for (let i of Object.keys(payload)) { 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 // find how many challnegs have been updated and how many are new
let completed = yield select(completedChallenges); let completed = yield select(completedChallenges);
let newSubmissions = 0;
let challengesToUpdate = {}; let challengesToUpdate = {};
let newChalleneFound = true; let newChalleneFound = true;
for (let j of Object.keys(idsToSolutions)) { for (let j of Object.keys(idsToSolutions)) {
@ -57,27 +56,32 @@ function* updateLegacyCertificateSaga({ payload }) {
} }
if (newChalleneFound && idsToSolutions[j] !== '') { if (newChalleneFound && idsToSolutions[j] !== '') {
challengesToUpdate[j] = idsToSolutions[j]; challengesToUpdate[j] = idsToSolutions[j];
newSubmissions++;
} }
newChalleneFound = true; 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 = { const body = {
projects: { projects: {
[legacyCert]: challengesToUpdate [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 { try {
const response = yield call(putUpdateLegacyCertificate, body); const { data: response } = yield call(putUpdateLegacyCertificate, body);
yield put( yield put(submitComplete({ challArray: reduxShape }));
updateLegacyCertificateComplete({ updatedChallenges: challengesToUpdate })
);
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));
} catch (e) { } catch (e) {
yield put(updateLegacyCertificateError(e)); yield put(updateLegacyCertificateError(e));
yield put(createFlashMessage(standardErrorMessage));
} }
} }