diff --git a/common/app/routes/challenges/redux/actions.js b/common/app/routes/challenges/redux/actions.js index bf1a8793a0..89936fd3e1 100644 --- a/common/app/routes/challenges/redux/actions.js +++ b/common/app/routes/challenges/redux/actions.js @@ -32,3 +32,7 @@ export const updateFilter = createAction( ); export const clearFilter = createAction(types.clearFilter); + +// files +export const updateFile = createAction(types.updateFile); +export const updateFiles = createAction(types.updateFiles); diff --git a/common/app/routes/challenges/redux/reducer.js b/common/app/routes/challenges/redux/reducer.js index 837c35e0a2..ddb5dd07c2 100644 --- a/common/app/routes/challenges/redux/reducer.js +++ b/common/app/routes/challenges/redux/reducer.js @@ -1,4 +1,5 @@ import { handleActions } from 'redux-actions'; +import { createPoly } from '../../../../utils/polyvinyl'; import types from './types'; @@ -7,20 +8,20 @@ const initialState = { currentStep: 0, previousStep: -1, filter: '', - content: null, + files: {}, superBlocks: [] }; function arrayToNewLineString(seedData = []) { seedData = Array.isArray(seedData) ? seedData : [seedData]; - return seedData.reduce((seed, line) => '' + seed + line + '\n', '\n'); + return seedData.reduce((seed, line) => '' + seed + line + '\n', ''); } function buildSeed({ challengeSeed = [] } = {}) { return arrayToNewLineString(challengeSeed); } -export default handleActions( +const mainReducer = handleActions( { [types.fetchChallengeCompleted]: (state, { payload = '' }) => ({ ...state, @@ -28,8 +29,7 @@ export default handleActions( }), [types.updateCurrentChallenge]: (state, { payload: challenge }) => ({ ...state, - challenge: challenge.dashedName, - content: buildSeed(challenge) + challenge: challenge.dashedName }), // map @@ -56,3 +56,33 @@ export default handleActions( }, initialState ); + +const filesReducer = handleActions( + { + [types.updateFile]: (state, { payload: file }) => ({ + ...state, + [file.path]: file + }), + [types.updateFiles]: (state, { payload: files }) => { + return files + .reduce((files, file) => { + files[file.path] = file; + return files; + }, { ...state }); + }, + [types.updateCurrentChallenge]: (state, { payload: challenge }) => { + const path = challenge.dashedName + challenge.type; + return { + ...state, + [path]: createPoly({ path, contents: buildSeed(challenge) }) + }; + } + }, + {} +); + +export default function challengeReducers(state, action) { + const newState = mainReducer(state, action); + const files = filesReducer(state && state.files || {}, action); + return newState.files !== files ? { ...newState, files } : newState; +} diff --git a/common/app/routes/challenges/redux/types.js b/common/app/routes/challenges/redux/types.js index f0fa935c83..817103c3a9 100644 --- a/common/app/routes/challenges/redux/types.js +++ b/common/app/routes/challenges/redux/types.js @@ -13,5 +13,9 @@ export default createTypes([ // map 'updateFilter', - 'clearFilter' + 'clearFilter', + + // files + 'updateFile', + 'updateFiles' ], 'challenges');