Merge pull request #10982 from BerkeleyTrue/fix/code-storage-fresh-head-tail

Fix(code-storage): Make sure head/tail is always fresh
This commit is contained in:
Quincy Larson
2016-09-30 12:45:26 -07:00
committed by GitHub
3 changed files with 36 additions and 10 deletions

View File

@ -14,6 +14,9 @@ import {
updateMain, updateMain,
lockUntrustedCode lockUntrustedCode
} from '../../common/app/routes/challenges/redux/actions'; } from '../../common/app/routes/challenges/redux/actions';
import {
challengeSelector
} from '../../common/app/routes/challenges/redux/selectors';
const legacyPrefixes = [ const legacyPrefixes = [
'Bonfire: ', 'Bonfire: ',
@ -72,13 +75,14 @@ export function saveCodeSaga(actions, getState) {
}); });
} }
export function loadCodeSaga(actions$, getState, { window, location }) { export function loadCodeSaga(actions, getState, { window, location }) {
return actions$ return actions
::ofType(types.loadCode) ::ofType(types.loadCode)
.flatMap(() => { .flatMap(() => {
let finalFiles; let finalFiles;
const state = getState(); const state = getState();
const { user } = userSelector(state); const { user } = userSelector(state);
const { challenge } = challengeSelector(state);
const { const {
challengesApp: { challengesApp: {
id = '', id = '',
@ -99,7 +103,7 @@ export function loadCodeSaga(actions$, getState, { window, location }) {
makeToast({ makeToast({
message: 'I found code in the URI. Loading now.' message: 'I found code in the URI. Loading now.'
}), }),
savedCodeFound(finalFiles) savedCodeFound(finalFiles, challenge)
); );
} }
@ -118,7 +122,7 @@ export function loadCodeSaga(actions$, getState, { window, location }) {
makeToast({ makeToast({
message: 'I found some saved work. Loading now.' message: 'I found some saved work. Loading now.'
}), }),
savedCodeFound(finalFiles), savedCodeFound(finalFiles, challenge),
updateMain() updateMain()
); );
} }
@ -135,7 +139,7 @@ export function loadCodeSaga(actions$, getState, { window, location }) {
makeToast({ makeToast({
message: 'I found a previous solved solution. Loading now.' message: 'I found a previous solved solution. Loading now.'
}), }),
savedCodeFound(finalFiles), savedCodeFound(finalFiles, challenge),
updateMain() updateMain()
); );
} }

View File

@ -91,7 +91,10 @@ export const moveToNextChallenge = createAction(types.moveToNextChallenge);
// code storage // code storage
export const saveCode = createAction(types.saveCode); export const saveCode = createAction(types.saveCode);
export const loadCode = createAction(types.loadCode); export const loadCode = createAction(types.loadCode);
export const savedCodeFound = createAction(types.savedCodeFound); export const savedCodeFound = createAction(
types.savedCodeFound,
(files, challenge) => ({ files, challenge })
);
export const clearSavedCode = createAction(types.clearSavedCode); export const clearSavedCode = createAction(types.clearSavedCode);

View File

@ -221,9 +221,29 @@ const filesReducer = handleActions(
return files; return files;
}, { ...state }); }, { ...state });
}, },
[types.savedCodeFound]: (state, { payload: files }) => ({ [types.savedCodeFound]: (state, { payload: { files, challenge } }) => {
...files if (challenge.type === 'mod') {
}), // this may need to change to update head/tail
return challenge.files;
}
if (
challenge.challengeType !== html &&
challenge.challengeType !== js &&
challenge.challengeType !== bonfire
) {
return {};
}
// classic challenge to modern format
const preFile = getPreFile(challenge);
return {
[preFile.key]: createPoly({
...files[preFile.key],
// make sure head/tail are always fresh
head: arrayToString(challenge.head),
tail: arrayToString(challenge.tail)
})
};
},
[types.updateCurrentChallenge]: (state, { payload: challenge = {} }) => { [types.updateCurrentChallenge]: (state, { payload: challenge = {} }) => {
if (challenge.type === 'mod') { if (challenge.type === 'mod') {
return challenge.files; return challenge.files;
@ -238,7 +258,6 @@ const filesReducer = handleActions(
// classic challenge to modern format // classic challenge to modern format
const preFile = getPreFile(challenge); const preFile = getPreFile(challenge);
return { return {
...state,
[preFile.key]: createPoly({ [preFile.key]: createPoly({
...preFile, ...preFile,
contents: buildSeed(challenge), contents: buildSeed(challenge),