Add code storage on code execution

This commit is contained in:
Berkeley Martinez
2016-05-27 22:07:10 -07:00
parent 8bf27f9834
commit 7c691b5532
7 changed files with 98 additions and 8 deletions

View File

@@ -0,0 +1,51 @@
import store from 'store';
import types from '../../common/app/routes/challenges/redux/types';
import {
savedCodeFound
} from '../../common/app/routes/challenges/redux/actions';
const legecyPrefixes = [
'Bonfire: ',
'Waypoint: ',
'Zipline: ',
'Basejump: ',
'Checkpoint: '
];
function getCode(id, legacy) {
if (store.has(id)) {
return store.get(id);
}
if (store.has(legacy)) {
const code = '' + store.get(legacy);
store.remove(legacy);
return code;
}
return legecyPrefixes.reduce((code, prefix) => {
if (code) {
return code;
}
return store.get(prefix + legacy + 'Val');
}, null);
}
export default function codeStorageSaga(actions$, getState) {
return actions$
.filter(({ type }) => (
type === types.saveCode ||
type === types.loadCode
))
.map(({ type }) => {
const { id = '', files = {}, legacyKey = '' } = getState().challengesApp;
if (type === types.saveCode) {
store.set(id, files);
return null;
}
const codeFound = getCode(id, legacyKey);
if (codeFound) {
return savedCodeFound(codeFound);
}
return null;
});
}

View File

@@ -7,7 +7,8 @@ import types from '../../common/app/routes/challenges/redux/types';
import {
frameMain,
frameTests,
initOutput
initOutput,
saveCode
} from '../../common/app/routes/challenges/redux/actions';
import { setExt, updateContents } from '../../common/utils/polyvinyl';
@@ -116,7 +117,7 @@ export default function executeChallengeSaga(action$, getState) {
frameMain(payload)
];
if (type === types.executeChallenge) {
actions.push(frameTests(payload));
actions.push(saveCode(), frameTests(payload));
}
return Observable.from(actions, null, null, Scheduler.default);
})

View File

@@ -5,6 +5,7 @@ import hardGoToSaga from './hard-go-to-saga';
import windowSaga from './window-saga';
import executeChallengeSaga from './execute-challenge-saga';
import frameSaga from './frame-saga';
import codeStorageSaga from './code-storage-saga';
export default [
errSaga,
@@ -13,5 +14,6 @@ export default [
hardGoToSaga,
windowSaga,
executeChallengeSaga,
frameSaga
frameSaga,
codeStorageSaga
];