fix file storing for single file challenges

This commit is contained in:
Berkeley Martinez
2016-05-11 23:16:03 -07:00
parent e173463cdb
commit 70dc2f75fa
2 changed files with 30 additions and 6 deletions

View File

@ -11,9 +11,10 @@ import { challengeSelector } from '../redux/selectors';
const mapStateToProps = createSelector( const mapStateToProps = createSelector(
challengeSelector, challengeSelector,
state => state.challengesApp.content, state => state.challengesApp.files,
({ challenge, showPreview, mode }, content) => ({ state => state.challengesApp.path,
content, ({ challenge, showPreview, mode }, files, path) => ({
content: files[path] && files[path].contents,
challenge, challenge,
showPreview, showPreview,
mode mode

View File

@ -2,6 +2,7 @@ import { handleActions } from 'redux-actions';
import { createPoly } from '../../../../utils/polyvinyl'; import { createPoly } from '../../../../utils/polyvinyl';
import types from './types'; import types from './types';
import { HTML, JS } from '../../../utils/challengeTypes';
const initialState = { const initialState = {
challenge: '', challenge: '',
@ -21,6 +22,15 @@ function buildSeed({ challengeSeed = [] } = {}) {
return arrayToNewLineString(challengeSeed); return arrayToNewLineString(challengeSeed);
} }
const pathsMap = {
[HTML]: 'main.html',
[JS]: 'main.js'
};
function getPath({ challengeType }) {
return pathsMap[challengeType] || 'main';
}
const mainReducer = handleActions( const mainReducer = handleActions(
{ {
[types.fetchChallengeCompleted]: (state, { payload = '' }) => ({ [types.fetchChallengeCompleted]: (state, { payload = '' }) => ({
@ -29,7 +39,8 @@ const mainReducer = handleActions(
}), }),
[types.updateCurrentChallenge]: (state, { payload: challenge }) => ({ [types.updateCurrentChallenge]: (state, { payload: challenge }) => ({
...state, ...state,
challenge: challenge.dashedName challenge: challenge.dashedName,
path: getPath(challenge)
}), }),
// map // map
@ -71,10 +82,22 @@ const filesReducer = handleActions(
}, { ...state }); }, { ...state });
}, },
[types.updateCurrentChallenge]: (state, { payload: challenge }) => { [types.updateCurrentChallenge]: (state, { payload: challenge }) => {
const path = challenge.dashedName + challenge.type; if (challenge.type === 'mod') {
return challenge.files;
}
if (
challenge.challengeType !== HTML &&
challenge.challengeType !== JS
) {
return {};
}
const path = getPath(challenge);
return { return {
...state, ...state,
[path]: createPoly({ path, contents: buildSeed(challenge) }) [path]: createPoly({
path,
contents: buildSeed(challenge)
})
}; };
} }
}, },