From 70dc2f75fa491ea4d9a08544358d5866e80e0b65 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Wed, 11 May 2016 23:16:03 -0700 Subject: [PATCH] fix file storing for single file challenges --- .../challenges/components/Challenge.jsx | 7 +++-- common/app/routes/challenges/redux/reducer.js | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/common/app/routes/challenges/components/Challenge.jsx b/common/app/routes/challenges/components/Challenge.jsx index 331d7b426e..35196a4d70 100644 --- a/common/app/routes/challenges/components/Challenge.jsx +++ b/common/app/routes/challenges/components/Challenge.jsx @@ -11,9 +11,10 @@ import { challengeSelector } from '../redux/selectors'; const mapStateToProps = createSelector( challengeSelector, - state => state.challengesApp.content, - ({ challenge, showPreview, mode }, content) => ({ - content, + state => state.challengesApp.files, + state => state.challengesApp.path, + ({ challenge, showPreview, mode }, files, path) => ({ + content: files[path] && files[path].contents, challenge, showPreview, mode diff --git a/common/app/routes/challenges/redux/reducer.js b/common/app/routes/challenges/redux/reducer.js index ddb5dd07c2..068de2aedf 100644 --- a/common/app/routes/challenges/redux/reducer.js +++ b/common/app/routes/challenges/redux/reducer.js @@ -2,6 +2,7 @@ import { handleActions } from 'redux-actions'; import { createPoly } from '../../../../utils/polyvinyl'; import types from './types'; +import { HTML, JS } from '../../../utils/challengeTypes'; const initialState = { challenge: '', @@ -21,6 +22,15 @@ function buildSeed({ challengeSeed = [] } = {}) { return arrayToNewLineString(challengeSeed); } +const pathsMap = { + [HTML]: 'main.html', + [JS]: 'main.js' +}; + +function getPath({ challengeType }) { + return pathsMap[challengeType] || 'main'; +} + const mainReducer = handleActions( { [types.fetchChallengeCompleted]: (state, { payload = '' }) => ({ @@ -29,7 +39,8 @@ const mainReducer = handleActions( }), [types.updateCurrentChallenge]: (state, { payload: challenge }) => ({ ...state, - challenge: challenge.dashedName + challenge: challenge.dashedName, + path: getPath(challenge) }), // map @@ -71,10 +82,22 @@ const filesReducer = handleActions( }, { ...state }); }, [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 { ...state, - [path]: createPoly({ path, contents: buildSeed(challenge) }) + [path]: createPoly({ + path, + contents: buildSeed(challenge) + }) }; } },