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
];

View File

@ -8,14 +8,24 @@ import Editor from './Editor.jsx';
import SidePanel from './Side-Panel.jsx';
import Preview from './Preview.jsx';
import { challengeSelector } from '../../redux/selectors';
import { executeChallenge, updateMain, updateFile } from '../../redux/actions';
import {
executeChallenge,
updateMain,
updateFile,
loadCode
} from '../../redux/actions';
const mapStateToProps = createSelector(
challengeSelector,
state => state.challengesApp.tests,
state => state.challengesApp.files,
state => state.challengesApp.key,
({ challenge, showPreview, mode }, tests, files = {}, key = '') => ({
(
{ showPreview, mode },
tests,
files = {},
key = ''
) => ({
content: files[key] && files[key].contents || '',
file: files[key],
showPreview,
@ -24,7 +34,12 @@ const mapStateToProps = createSelector(
})
);
const bindableActions = { executeChallenge, updateFile, updateMain };
const bindableActions = {
executeChallenge,
updateFile,
updateMain,
loadCode
};
export class Challenge extends PureComponent {
static displayName = 'Challenge';
@ -35,12 +50,15 @@ export class Challenge extends PureComponent {
mode: PropTypes.string,
updateFile: PropTypes.func,
executeChallenge: PropTypes.func,
updateMain: PropTypes.func
updateMain: PropTypes.func,
loadCode: PropTypes.func
};
componentDidMount() {
this.props.loadCode();
this.props.updateMain();
}
renderPreview(showPreview) {
if (!showPreview) {
return null;

View File

@ -56,3 +56,8 @@ export const updateTests = createAction(types.updateTests);
export const initOutput = createAction(types.initOutput, loggerToStr);
export const updateOutput = createAction(types.updateOutput, loggerToStr);
// code storage
export const saveCode = createAction(types.saveCode);
export const loadCode = createAction(types.loadCode);
export const savedCodeFound = createAction(types.savedCodeFound);

View File

@ -12,7 +12,9 @@ import {
} from '../utils';
const initialState = {
id: '',
challenge: '',
legacyKey: '',
currentStep: 0,
previousStep: -1,
filter: '',
@ -29,6 +31,9 @@ const mainReducer = handleActions(
[types.updateCurrentChallenge]: (state, { payload: challenge }) => ({
...state,
refresh: true,
id: challenge.id,
// used mainly to find code storage
legacyKey: challenge.name,
challenge: challenge.dashedName,
key: getFileKey(challenge),
tests: createTests(challenge)
@ -86,6 +91,9 @@ const filesReducer = handleActions(
return files;
}, { ...state });
},
[types.savedCodeFound]: (state, { payload: files }) => ({
...files
}),
[types.updateCurrentChallenge]: (state, { payload: challenge }) => {
if (challenge.type === 'mod') {
return challenge.files;

View File

@ -28,5 +28,10 @@ export default createTypes([
'frameTests',
'updateOutput',
'initOutput',
'updateTests'
'updateTests',
// code storage
'saveCode',
'loadCode',
'savedCodeFound'
], 'challenges');