diff --git a/client/sagas/code-storage-saga.js b/client/sagas/code-storage-saga.js index cc7904ba67..2ff705e7b4 100644 --- a/client/sagas/code-storage-saga.js +++ b/client/sagas/code-storage-saga.js @@ -14,6 +14,9 @@ import { updateMain, lockUntrustedCode } from '../../common/app/routes/challenges/redux/actions'; +import { + challengeSelector +} from '../../common/app/routes/challenges/redux/selectors'; const legacyPrefixes = [ 'Bonfire: ', @@ -72,13 +75,14 @@ export function saveCodeSaga(actions, getState) { }); } -export function loadCodeSaga(actions$, getState, { window, location }) { - return actions$ +export function loadCodeSaga(actions, getState, { window, location }) { + return actions ::ofType(types.loadCode) .flatMap(() => { let finalFiles; const state = getState(); const { user } = userSelector(state); + const { challenge } = challengeSelector(state); const { challengesApp: { id = '', @@ -99,7 +103,7 @@ export function loadCodeSaga(actions$, getState, { window, location }) { makeToast({ 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({ message: 'I found some saved work. Loading now.' }), - savedCodeFound(finalFiles), + savedCodeFound(finalFiles, challenge), updateMain() ); } @@ -135,7 +139,7 @@ export function loadCodeSaga(actions$, getState, { window, location }) { makeToast({ message: 'I found a previous solved solution. Loading now.' }), - savedCodeFound(finalFiles), + savedCodeFound(finalFiles, challenge), updateMain() ); } diff --git a/common/app/routes/challenges/redux/actions.js b/common/app/routes/challenges/redux/actions.js index b2f8e65243..d4bcb0a421 100644 --- a/common/app/routes/challenges/redux/actions.js +++ b/common/app/routes/challenges/redux/actions.js @@ -91,7 +91,10 @@ export const moveToNextChallenge = createAction(types.moveToNextChallenge); // code storage export const saveCode = createAction(types.saveCode); 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); diff --git a/common/app/routes/challenges/redux/reducer.js b/common/app/routes/challenges/redux/reducer.js index 8e0bb258d5..ed9bd3f821 100644 --- a/common/app/routes/challenges/redux/reducer.js +++ b/common/app/routes/challenges/redux/reducer.js @@ -221,9 +221,29 @@ const filesReducer = handleActions( return files; }, { ...state }); }, - [types.savedCodeFound]: (state, { payload: files }) => ({ - ...files - }), + [types.savedCodeFound]: (state, { payload: { files, challenge } }) => { + 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 = {} }) => { if (challenge.type === 'mod') { return challenge.files; @@ -238,7 +258,6 @@ const filesReducer = handleActions( // classic challenge to modern format const preFile = getPreFile(challenge); return { - ...state, [preFile.key]: createPoly({ ...preFile, contents: buildSeed(challenge),