2016-05-09 13:42:39 -07:00
|
|
|
import { handleActions } from 'redux-actions';
|
2016-05-11 18:38:08 -07:00
|
|
|
import { createPoly } from '../../../../utils/polyvinyl';
|
2016-05-09 13:42:39 -07:00
|
|
|
|
|
|
|
import types from './types';
|
2016-06-07 20:41:42 -07:00
|
|
|
import { bonfire, html, js } from '../../../utils/challengeTypes';
|
2016-05-20 12:42:26 -07:00
|
|
|
import {
|
|
|
|
arrayToString,
|
|
|
|
buildSeed,
|
|
|
|
createTests,
|
|
|
|
getPreFile,
|
|
|
|
getFileKey
|
|
|
|
} from '../utils';
|
2016-05-09 13:42:39 -07:00
|
|
|
|
2016-06-10 10:45:29 -07:00
|
|
|
const initialUiState = {
|
2016-06-14 18:47:43 -07:00
|
|
|
hintIndex: 0,
|
2016-06-13 12:26:30 -07:00
|
|
|
// step index tracing
|
2016-06-10 10:45:29 -07:00
|
|
|
currentIndex: 0,
|
|
|
|
previousIndex: -1,
|
2016-06-13 12:26:30 -07:00
|
|
|
// step action
|
2016-06-10 10:45:29 -07:00
|
|
|
isActionCompleted: false,
|
2016-06-17 17:24:34 -07:00
|
|
|
isLightBoxOpen: false,
|
2016-06-13 12:26:30 -07:00
|
|
|
// project is ready to submit
|
|
|
|
isSubmitting: false,
|
2016-06-10 14:01:13 -07:00
|
|
|
output: `/**
|
|
|
|
* Any console.log()
|
|
|
|
* statements will appear in
|
|
|
|
* here console.
|
2016-06-13 12:26:30 -07:00
|
|
|
*/`,
|
|
|
|
// video
|
|
|
|
// 1 indexed
|
|
|
|
currentQuestion: 1,
|
|
|
|
// [ xPosition, yPosition ]
|
|
|
|
mouse: [ 0, 0 ],
|
|
|
|
// change in mouse position since pressed
|
|
|
|
// [ xDelta, yDelta ]
|
|
|
|
delta: [ 0, 0 ],
|
|
|
|
isPressed: false,
|
|
|
|
isCorrect: false,
|
|
|
|
shouldShakeQuestion: false,
|
|
|
|
shouldShowQuestions: false
|
2016-06-10 10:45:29 -07:00
|
|
|
};
|
2016-05-09 13:42:39 -07:00
|
|
|
const initialState = {
|
2016-05-27 22:07:10 -07:00
|
|
|
id: '',
|
2016-05-09 13:42:39 -07:00
|
|
|
challenge: '',
|
2016-06-10 10:45:29 -07:00
|
|
|
// old code storage key
|
2016-05-27 22:07:10 -07:00
|
|
|
legacyKey: '',
|
2016-06-10 10:45:29 -07:00
|
|
|
files: {},
|
2016-06-08 18:48:30 -07:00
|
|
|
// map
|
2016-06-22 14:55:35 -07:00
|
|
|
mapUi: {},
|
2016-05-10 19:28:40 -07:00
|
|
|
filter: '',
|
2016-06-01 15:52:08 -07:00
|
|
|
superBlocks: [],
|
2016-06-08 18:48:30 -07:00
|
|
|
// misc
|
2016-06-10 10:45:29 -07:00
|
|
|
toast: 0,
|
|
|
|
...initialUiState
|
2016-05-09 13:42:39 -07:00
|
|
|
};
|
|
|
|
|
2016-05-11 18:38:08 -07:00
|
|
|
const mainReducer = handleActions(
|
2016-05-09 13:42:39 -07:00
|
|
|
{
|
2016-05-10 21:25:12 -07:00
|
|
|
[types.fetchChallengeCompleted]: (state, { payload = '' }) => ({
|
2016-05-09 13:42:39 -07:00
|
|
|
...state,
|
|
|
|
challenge: payload
|
|
|
|
}),
|
2016-05-10 21:25:12 -07:00
|
|
|
[types.updateCurrentChallenge]: (state, { payload: challenge }) => ({
|
2016-05-09 13:42:39 -07:00
|
|
|
...state,
|
2016-05-27 22:07:10 -07:00
|
|
|
id: challenge.id,
|
|
|
|
// used mainly to find code storage
|
|
|
|
legacyKey: challenge.name,
|
2016-05-11 23:16:03 -07:00
|
|
|
challenge: challenge.dashedName,
|
2016-05-20 12:42:26 -07:00
|
|
|
key: getFileKey(challenge),
|
2016-06-14 18:47:43 -07:00
|
|
|
tests: createTests(challenge),
|
|
|
|
numOfHints: Array.isArray(challenge.hints) ? challenge.hints.length : 0
|
2016-05-10 19:28:40 -07:00
|
|
|
}),
|
2016-05-25 18:28:20 -07:00
|
|
|
[types.updateTests]: (state, { payload: tests }) => ({
|
|
|
|
...state,
|
|
|
|
tests
|
|
|
|
}),
|
2016-06-14 18:47:43 -07:00
|
|
|
[types.updateHint]: state => ({
|
|
|
|
...state,
|
|
|
|
hintIndex: state.hintIndex + 1 >= state.numOfHints ?
|
|
|
|
0 :
|
|
|
|
state.hintIndex + 1
|
|
|
|
}),
|
2016-05-27 22:21:52 -07:00
|
|
|
[types.executeChallenge]: state => ({
|
|
|
|
...state,
|
|
|
|
tests: state.tests.map(test => ({ ...test, err: false, pass: false }))
|
|
|
|
}),
|
2016-06-01 15:52:08 -07:00
|
|
|
[types.showChallengeComplete]: (state, { payload: toast }) => ({
|
|
|
|
...state,
|
|
|
|
toast
|
|
|
|
}),
|
2016-06-07 20:41:42 -07:00
|
|
|
[types.showProjectSubmit]: state => ({
|
|
|
|
...state,
|
|
|
|
isSubmitting: true
|
|
|
|
}),
|
2016-06-10 10:45:29 -07:00
|
|
|
[types.resetUi]: (state) => ({
|
|
|
|
...state,
|
|
|
|
...initialUiState
|
|
|
|
}),
|
2016-05-10 19:28:40 -07:00
|
|
|
|
|
|
|
// map
|
|
|
|
[types.updateFilter]: (state, { payload = ''}) => ({
|
|
|
|
...state,
|
|
|
|
filter: payload
|
|
|
|
}),
|
|
|
|
[types.clearFilter]: (state) => ({
|
|
|
|
...state,
|
|
|
|
filter: ''
|
|
|
|
}),
|
|
|
|
[types.fetchChallengesCompleted]: (state, { payload = [] }) => ({
|
|
|
|
...state,
|
|
|
|
superBlocks: payload
|
|
|
|
}),
|
|
|
|
|
|
|
|
// step
|
|
|
|
[types.goToStep]: (state, { payload: step = 0 }) => ({
|
|
|
|
...state,
|
2016-06-08 18:48:30 -07:00
|
|
|
currentIndex: step,
|
|
|
|
previousIndex: state.currentIndex,
|
|
|
|
isActionCompleted: false
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.completeAction]: state => ({
|
|
|
|
...state,
|
|
|
|
isActionCompleted: true
|
2016-05-27 17:11:25 -07:00
|
|
|
}),
|
2016-06-17 17:24:34 -07:00
|
|
|
[types.openLightBoxImage]: state => ({
|
|
|
|
...state,
|
|
|
|
isLightBoxOpen: true
|
|
|
|
}),
|
|
|
|
[types.closeLightBoxImage]: state => ({
|
|
|
|
...state,
|
|
|
|
isLightBoxOpen: false
|
|
|
|
}),
|
2016-06-08 18:48:30 -07:00
|
|
|
|
|
|
|
// classic/modern
|
2016-05-27 17:11:25 -07:00
|
|
|
[types.initOutput]: (state, { payload: output }) => ({
|
|
|
|
...state,
|
|
|
|
output
|
|
|
|
}),
|
|
|
|
[types.updateOutput]: (state, { payload: output }) => ({
|
|
|
|
...state,
|
|
|
|
output: (state.output || '') + output
|
2016-06-13 12:26:30 -07:00
|
|
|
}),
|
|
|
|
// video
|
|
|
|
[types.toggleQuestionView]: state => ({
|
|
|
|
...state,
|
|
|
|
shouldShowQuestions: !state.shouldShowQuestions,
|
|
|
|
currentQuestion: 1
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.grabQuestion]: (state, { payload: { delta, mouse } }) => ({
|
|
|
|
...state,
|
|
|
|
isPressed: true,
|
|
|
|
delta,
|
|
|
|
mouse
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.releaseQuestion]: state => ({
|
|
|
|
...state,
|
|
|
|
isPressed: false,
|
|
|
|
mouse: [ 0, 0 ]
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.moveQuestion]: (state, { payload: mouse }) => ({ ...state, mouse }),
|
|
|
|
[types.startShake]: state => ({ ...state, shouldShakeQuestion: true }),
|
|
|
|
[types.endShake]: state => ({ ...state, shouldShakeQuestion: false }),
|
|
|
|
|
|
|
|
[types.primeNextQuestion]: (state, { payload: userAnswer }) => ({
|
|
|
|
...state,
|
|
|
|
currentQuestion: state.currentQuestion + 1,
|
|
|
|
mouse: [ userAnswer ? 1000 : -1000, 0],
|
|
|
|
isPressed: false
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.goToNextQuestion]: state => ({
|
|
|
|
...state,
|
|
|
|
mouse: [ 0, 0 ]
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.videoCompleted]: (state, { payload: userAnswer } ) => ({
|
|
|
|
...state,
|
|
|
|
isCorrect: true,
|
|
|
|
isPressed: false,
|
|
|
|
delta: [ 0, 0 ],
|
|
|
|
mouse: [ userAnswer ? 1000 : -1000, 0]
|
2016-05-09 13:42:39 -07:00
|
|
|
})
|
|
|
|
},
|
|
|
|
initialState
|
|
|
|
);
|
2016-05-11 18:38:08 -07:00
|
|
|
|
|
|
|
const filesReducer = handleActions(
|
|
|
|
{
|
|
|
|
[types.updateFile]: (state, { payload: file }) => ({
|
|
|
|
...state,
|
2016-05-20 12:42:26 -07:00
|
|
|
[file.key]: file
|
2016-05-11 18:38:08 -07:00
|
|
|
}),
|
|
|
|
[types.updateFiles]: (state, { payload: files }) => {
|
|
|
|
return files
|
|
|
|
.reduce((files, file) => {
|
2016-05-20 12:42:26 -07:00
|
|
|
files[file.key] = file;
|
2016-05-11 18:38:08 -07:00
|
|
|
return files;
|
|
|
|
}, { ...state });
|
|
|
|
},
|
2016-05-27 22:07:10 -07:00
|
|
|
[types.savedCodeFound]: (state, { payload: files }) => ({
|
|
|
|
...files
|
|
|
|
}),
|
2016-05-11 18:38:08 -07:00
|
|
|
[types.updateCurrentChallenge]: (state, { payload: challenge }) => {
|
2016-05-11 23:16:03 -07:00
|
|
|
if (challenge.type === 'mod') {
|
|
|
|
return challenge.files;
|
|
|
|
}
|
|
|
|
if (
|
2016-06-07 20:41:42 -07:00
|
|
|
challenge.challengeType !== html &&
|
|
|
|
challenge.challengeType !== js &&
|
|
|
|
challenge.challengeType !== bonfire
|
2016-05-11 23:16:03 -07:00
|
|
|
) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-06-10 10:45:29 -07:00
|
|
|
// classic challenge to modern format
|
2016-05-20 12:42:26 -07:00
|
|
|
const preFile = getPreFile(challenge);
|
2016-05-11 18:38:08 -07:00
|
|
|
return {
|
|
|
|
...state,
|
2016-05-20 12:42:26 -07:00
|
|
|
[preFile.key]: createPoly({
|
|
|
|
...preFile,
|
2016-05-14 16:46:20 -07:00
|
|
|
contents: buildSeed(challenge),
|
|
|
|
head: arrayToString(challenge.head),
|
|
|
|
tail: arrayToString(challenge.tail)
|
2016-05-11 23:16:03 -07:00
|
|
|
})
|
2016-05-11 18:38:08 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2016-06-21 16:28:13 -07:00
|
|
|
// {
|
|
|
|
// // show
|
|
|
|
// [(super)BlockName]: { open: Boolean }
|
|
|
|
// // do not show
|
|
|
|
// [(super)BlockName]: null
|
|
|
|
// }
|
|
|
|
const mapReducer = handleActions(
|
|
|
|
{
|
|
|
|
[types.initMap]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
...payload
|
|
|
|
}),
|
|
|
|
[types.toggleThisPanel]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
[payload]: !state[payload]
|
|
|
|
})
|
|
|
|
},
|
2016-06-22 14:55:35 -07:00
|
|
|
initialState.mapUi
|
2016-06-21 16:28:13 -07:00
|
|
|
);
|
|
|
|
|
2016-05-11 18:38:08 -07:00
|
|
|
export default function challengeReducers(state, action) {
|
|
|
|
const newState = mainReducer(state, action);
|
|
|
|
const files = filesReducer(state && state.files || {}, action);
|
2016-06-21 16:28:13 -07:00
|
|
|
if (newState.files !== files) {
|
|
|
|
return { ...newState, files };
|
|
|
|
}
|
|
|
|
// map actions only effect this reducer;
|
2016-06-22 14:55:35 -07:00
|
|
|
const mapUi = mapReducer(state && state.mapUi || {}, action);
|
|
|
|
if (newState.mapUi !== mapUi) {
|
|
|
|
return { ...newState, mapUi };
|
2016-06-21 16:28:13 -07:00
|
|
|
}
|
|
|
|
return newState;
|
2016-05-11 18:38:08 -07:00
|
|
|
}
|