2018-04-06 14:51:52 +01:00
|
|
|
import { createAction, handleActions } from 'redux-actions';
|
2018-09-21 17:30:16 +03:00
|
|
|
import { reducer as reduxFormReducer } from 'redux-form';
|
|
|
|
|
2018-09-27 14:19:03 +03:00
|
|
|
import { createTypes } from '../../../../utils/stateManagement';
|
2018-11-07 18:15:27 +00:00
|
|
|
import { createAsyncTypes } from '../../../utils/createTypes';
|
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
import { createPoly } from '../utils/polyvinyl';
|
|
|
|
import challengeModalEpic from './challenge-modal-epic';
|
|
|
|
import completionEpic from './completion-epic';
|
2018-04-11 15:10:48 +01:00
|
|
|
import codeLockEpic from './code-lock-epic';
|
2018-05-08 00:29:45 +01:00
|
|
|
import createQuestionEpic from './create-question-epic';
|
2018-05-09 12:40:09 +01:00
|
|
|
import codeStorageEpic from './code-storage-epic';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
2018-11-07 18:15:27 +00:00
|
|
|
import { createIdToNameMapSaga } from './id-to-name-map-saga';
|
2018-11-26 02:17:38 +03:00
|
|
|
import { createExecuteChallengeSaga } from './execute-challenge-saga';
|
2019-02-06 14:30:54 +03:00
|
|
|
import { createCurrentChallengeSaga } from './current-challenge-saga';
|
2018-11-07 18:15:27 +00:00
|
|
|
|
2018-11-29 12:12:15 +00:00
|
|
|
export const ns = 'challenge';
|
2018-04-17 11:33:04 +01:00
|
|
|
export const backendNS = 'backendChallenge';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
challengeFiles: {},
|
2018-11-07 18:15:27 +00:00
|
|
|
challengeIdToNameMap: {},
|
2018-04-06 14:51:52 +01:00
|
|
|
challengeMeta: {
|
|
|
|
id: '',
|
|
|
|
nextChallengePath: '/'
|
|
|
|
},
|
|
|
|
challengeTests: [],
|
|
|
|
consoleOut: '',
|
2018-05-09 12:40:09 +01:00
|
|
|
isCodeLocked: false,
|
2018-04-06 14:51:52 +01:00
|
|
|
isJSEnabled: true,
|
|
|
|
modal: {
|
|
|
|
completion: false,
|
2018-05-09 12:40:09 +01:00
|
|
|
help: false,
|
2018-08-06 09:25:50 -04:00
|
|
|
video: false,
|
2018-05-09 12:40:09 +01:00
|
|
|
reset: false
|
2018-04-06 14:51:52 +01:00
|
|
|
},
|
2018-09-27 14:19:03 +03:00
|
|
|
projectFormValues: {},
|
2018-04-06 14:51:52 +01:00
|
|
|
successMessage: 'Happy Coding!'
|
|
|
|
};
|
|
|
|
|
|
|
|
export const types = createTypes(
|
|
|
|
[
|
|
|
|
'createFiles',
|
2018-05-08 00:29:45 +01:00
|
|
|
'createQuestion',
|
2018-04-06 14:51:52 +01:00
|
|
|
'initTests',
|
|
|
|
'initConsole',
|
2018-07-10 14:59:31 +02:00
|
|
|
'initLogs',
|
2018-04-06 14:51:52 +01:00
|
|
|
'updateConsole',
|
|
|
|
'updateChallengeMeta',
|
|
|
|
'updateFile',
|
|
|
|
'updateJSEnabled',
|
2018-05-24 19:45:38 +01:00
|
|
|
'updateProjectFormValues',
|
2018-04-06 14:51:52 +01:00
|
|
|
'updateSuccessMessage',
|
|
|
|
'updateTests',
|
2018-07-10 14:59:31 +02:00
|
|
|
'updateLogs',
|
|
|
|
|
|
|
|
'logsToConsole',
|
2018-04-11 15:10:48 +01:00
|
|
|
|
2018-05-09 12:40:09 +01:00
|
|
|
'lockCode',
|
2018-04-11 15:10:48 +01:00
|
|
|
'unlockCode',
|
2018-04-06 14:51:52 +01:00
|
|
|
'disableJSOnError',
|
2018-05-09 12:40:09 +01:00
|
|
|
'storedCodeFound',
|
|
|
|
'noStoredCodeFound',
|
2018-04-06 14:51:52 +01:00
|
|
|
|
|
|
|
'closeModal',
|
|
|
|
'openModal',
|
|
|
|
|
2018-12-07 14:08:32 +02:00
|
|
|
'previewMounted',
|
2018-05-09 12:40:09 +01:00
|
|
|
'challengeMounted',
|
2018-04-06 14:51:52 +01:00
|
|
|
'checkChallenge',
|
|
|
|
'executeChallenge',
|
2018-05-08 00:29:45 +01:00
|
|
|
'resetChallenge',
|
2018-05-09 12:40:09 +01:00
|
|
|
'submitChallenge',
|
2018-11-07 18:15:27 +00:00
|
|
|
|
2018-12-07 14:08:32 +02:00
|
|
|
'moveToTab',
|
|
|
|
|
2018-11-07 18:15:27 +00:00
|
|
|
...createAsyncTypes('fetchIdToNameMap')
|
2018-04-06 14:51:52 +01:00
|
|
|
],
|
|
|
|
ns
|
|
|
|
);
|
|
|
|
|
2018-11-07 18:15:27 +00:00
|
|
|
export const epics = [
|
|
|
|
challengeModalEpic,
|
|
|
|
codeLockEpic,
|
|
|
|
completionEpic,
|
|
|
|
createQuestionEpic,
|
2019-02-06 14:30:54 +03:00
|
|
|
codeStorageEpic
|
2018-11-07 18:15:27 +00:00
|
|
|
];
|
|
|
|
|
2018-11-26 02:17:38 +03:00
|
|
|
export const sagas = [
|
|
|
|
...createIdToNameMapSaga(types),
|
2019-02-06 14:30:54 +03:00
|
|
|
...createExecuteChallengeSaga(types),
|
|
|
|
...createCurrentChallengeSaga(types)
|
2018-11-26 02:17:38 +03:00
|
|
|
];
|
2018-11-07 18:15:27 +00:00
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
export const createFiles = createAction(types.createFiles, challengeFiles =>
|
|
|
|
Object.keys(challengeFiles)
|
|
|
|
.filter(key => challengeFiles[key])
|
|
|
|
.map(key => challengeFiles[key])
|
|
|
|
.reduce(
|
|
|
|
(challengeFiles, file) => ({
|
|
|
|
...challengeFiles,
|
|
|
|
[file.key]: {
|
|
|
|
...createPoly(file),
|
|
|
|
seed: file.contents.slice(0)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
);
|
2018-11-07 18:15:27 +00:00
|
|
|
|
|
|
|
export const fetchIdToNameMap = createAction(types.fetchIdToNameMap);
|
|
|
|
export const fetchIdToNameMapComplete = createAction(
|
|
|
|
types.fetchIdToNameMapComplete
|
|
|
|
);
|
|
|
|
export const fetchIdToNameMapError = createAction(types.fetchIdToNameMapError);
|
|
|
|
|
2018-05-08 00:29:45 +01:00
|
|
|
export const createQuestion = createAction(types.createQuestion);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const initTests = createAction(types.initTests);
|
|
|
|
export const updateTests = createAction(types.updateTests);
|
|
|
|
|
|
|
|
export const initConsole = createAction(types.initConsole);
|
2018-07-10 14:59:31 +02:00
|
|
|
export const initLogs = createAction(types.initLogs);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const updateChallengeMeta = createAction(types.updateChallengeMeta);
|
|
|
|
export const updateFile = createAction(types.updateFile);
|
|
|
|
export const updateConsole = createAction(types.updateConsole);
|
2018-07-10 14:59:31 +02:00
|
|
|
export const updateLogs = createAction(types.updateLogs);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const updateJSEnabled = createAction(types.updateJSEnabled);
|
2018-05-24 19:45:38 +01:00
|
|
|
export const updateProjectFormValues = createAction(
|
|
|
|
types.updateProjectFormValues
|
|
|
|
);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const updateSuccessMessage = createAction(types.updateSuccessMessage);
|
|
|
|
|
2018-07-10 14:59:31 +02:00
|
|
|
export const logsToConsole = createAction(types.logsToConsole);
|
|
|
|
|
2018-05-09 12:40:09 +01:00
|
|
|
export const lockCode = createAction(types.lockCode);
|
2018-04-11 15:10:48 +01:00
|
|
|
export const unlockCode = createAction(types.unlockCode);
|
2018-07-04 16:38:42 +03:00
|
|
|
export const disableJSOnError = createAction(types.disableJSOnError);
|
2018-05-09 12:40:09 +01:00
|
|
|
export const storedCodeFound = createAction(types.storedCodeFound);
|
|
|
|
export const noStoredCodeFound = createAction(types.noStoredCodeFound);
|
2018-04-06 14:51:52 +01:00
|
|
|
|
|
|
|
export const closeModal = createAction(types.closeModal);
|
|
|
|
export const openModal = createAction(types.openModal);
|
|
|
|
|
2018-12-11 23:05:15 +02:00
|
|
|
export const previewMounted = createAction(types.previewMounted);
|
2018-05-09 12:40:09 +01:00
|
|
|
export const challengeMounted = createAction(types.challengeMounted);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const checkChallenge = createAction(types.checkChallenge);
|
|
|
|
export const executeChallenge = createAction(types.executeChallenge);
|
2018-05-08 00:29:45 +01:00
|
|
|
export const resetChallenge = createAction(types.resetChallenge);
|
2018-04-06 14:51:52 +01:00
|
|
|
export const submitChallenge = createAction(types.submitChallenge);
|
|
|
|
|
2018-12-07 14:08:32 +02:00
|
|
|
export const moveToTab = createAction(types.moveToTab);
|
|
|
|
|
|
|
|
export const currentTabSelector = state => state[ns].currentTab;
|
2018-04-06 14:51:52 +01:00
|
|
|
export const challengeFilesSelector = state => state[ns].challengeFiles;
|
2018-11-07 18:15:27 +00:00
|
|
|
export const challengeIdToNameMapSelector = state =>
|
|
|
|
state[ns].challengeIdToNameMap;
|
2018-04-06 14:51:52 +01:00
|
|
|
export const challengeMetaSelector = state => state[ns].challengeMeta;
|
|
|
|
export const challengeTestsSelector = state => state[ns].challengeTests;
|
|
|
|
export const consoleOutputSelector = state => state[ns].consoleOut;
|
2018-05-09 12:40:09 +01:00
|
|
|
export const isCodeLockedSelector = state => state[ns].isCodeLocked;
|
2018-04-06 14:51:52 +01:00
|
|
|
export const isCompletionModalOpenSelector = state =>
|
2018-04-11 14:51:47 +01:00
|
|
|
state[ns].modal.completion;
|
2018-05-08 00:29:45 +01:00
|
|
|
export const isHelpModalOpenSelector = state => state[ns].modal.help;
|
2018-08-06 09:25:50 -04:00
|
|
|
export const isVideoModalOpenSelector = state => state[ns].modal.video;
|
2018-05-09 12:40:09 +01:00
|
|
|
export const isResetModalOpenSelector = state => state[ns].modal.reset;
|
2018-04-11 14:43:23 +01:00
|
|
|
export const isJSEnabledSelector = state => state[ns].isJSEnabled;
|
2018-04-06 14:51:52 +01:00
|
|
|
export const successMessageSelector = state => state[ns].successMessage;
|
|
|
|
|
2018-05-24 19:45:38 +01:00
|
|
|
export const backendFormValuesSelector = state => state.form[backendNS];
|
2018-09-27 14:19:03 +03:00
|
|
|
export const projectFormValuesSelector = state =>
|
|
|
|
state[ns].projectFormValues || {};
|
2018-05-24 19:45:38 +01:00
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
export const reducer = handleActions(
|
|
|
|
{
|
2018-11-07 18:15:27 +00:00
|
|
|
[types.fetchIdToNameMapComplete]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeIdToNameMap: payload
|
|
|
|
}),
|
2018-04-06 14:51:52 +01:00
|
|
|
[types.createFiles]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeFiles: payload
|
|
|
|
}),
|
2018-05-08 00:29:45 +01:00
|
|
|
[types.updateFile]: (state, { payload: { key, editorValue } }) => ({
|
|
|
|
...state,
|
|
|
|
challengeFiles: {
|
|
|
|
...state.challengeFiles,
|
|
|
|
[key]: {
|
|
|
|
...state.challengeFiles[key],
|
|
|
|
contents: editorValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2018-05-09 12:40:09 +01:00
|
|
|
[types.storedCodeFound]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeFiles: payload
|
|
|
|
}),
|
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
[types.initTests]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeTests: payload
|
|
|
|
}),
|
|
|
|
[types.updateTests]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeTests: payload
|
|
|
|
}),
|
2018-05-09 12:40:09 +01:00
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
[types.initConsole]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
consoleOut: payload
|
|
|
|
}),
|
|
|
|
[types.updateConsole]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
consoleOut: state.consoleOut + '\n' + payload
|
|
|
|
}),
|
2018-07-10 14:59:31 +02:00
|
|
|
[types.initLogs]: state => ({
|
|
|
|
...state,
|
|
|
|
logsOut: []
|
|
|
|
}),
|
|
|
|
[types.updateLogs]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
logsOut: [...state.logsOut, payload]
|
|
|
|
}),
|
|
|
|
[types.logsToConsole]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
consoleOut:
|
|
|
|
state.consoleOut +
|
|
|
|
(state.logsOut.length
|
|
|
|
? '\n' + payload + '\n' + state.logsOut.join('\n')
|
|
|
|
: '')
|
|
|
|
}),
|
2018-05-08 00:29:45 +01:00
|
|
|
[types.updateChallengeMeta]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
challengeMeta: { ...payload }
|
|
|
|
}),
|
|
|
|
|
|
|
|
[types.resetChallenge]: state => ({
|
2018-04-06 14:51:52 +01:00
|
|
|
...state,
|
2018-12-07 14:08:32 +02:00
|
|
|
currentTab: 2,
|
2018-04-06 14:51:52 +01:00
|
|
|
challengeFiles: {
|
2018-05-08 00:29:45 +01:00
|
|
|
...Object.keys(state.challengeFiles)
|
|
|
|
.map(key => state.challengeFiles[key])
|
|
|
|
.reduce(
|
|
|
|
(files, file) => ({
|
|
|
|
...files,
|
|
|
|
[file.key]: {
|
|
|
|
...file,
|
|
|
|
contents: file.seed.slice()
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
challengeTests: state.challengeTests.map(({ text, testString }) => ({
|
|
|
|
text,
|
|
|
|
testString
|
|
|
|
})),
|
|
|
|
consoleOut: ''
|
2018-04-06 14:51:52 +01:00
|
|
|
}),
|
2018-05-24 19:45:38 +01:00
|
|
|
[types.updateProjectFormValues]: (state, { payload }) => ({
|
|
|
|
...state,
|
2018-09-27 14:19:03 +03:00
|
|
|
projectFormValues: payload
|
2018-05-24 19:45:38 +01:00
|
|
|
}),
|
2018-05-09 12:40:09 +01:00
|
|
|
|
|
|
|
[types.lockCode]: state => ({
|
|
|
|
...state,
|
|
|
|
isCodeLocked: true
|
|
|
|
}),
|
2018-04-11 15:10:48 +01:00
|
|
|
[types.unlockCode]: state => ({
|
|
|
|
...state,
|
2018-05-09 12:40:09 +01:00
|
|
|
isJSEnabled: true,
|
|
|
|
isCodeLocked: false
|
2018-04-11 15:10:48 +01:00
|
|
|
}),
|
2018-07-04 16:38:42 +03:00
|
|
|
[types.disableJSOnError]: (state, { payload }) => ({
|
2018-04-06 14:51:52 +01:00
|
|
|
...state,
|
2018-09-30 11:37:19 +01:00
|
|
|
consoleOut: state.consoleOut + ' \n' + payload,
|
2018-04-06 14:51:52 +01:00
|
|
|
isJSEnabled: false
|
|
|
|
}),
|
2018-05-09 12:40:09 +01:00
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
[types.updateSuccessMessage]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
successMessage: payload
|
|
|
|
}),
|
|
|
|
[types.closeModal]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
modal: {
|
|
|
|
...state.modal,
|
|
|
|
[payload]: false
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[types.openModal]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
modal: {
|
|
|
|
...state.modal,
|
|
|
|
[payload]: true
|
|
|
|
}
|
2018-12-07 14:08:32 +02:00
|
|
|
}),
|
|
|
|
[types.moveToTab]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
currentTab: payload
|
|
|
|
}),
|
2019-02-06 14:30:54 +03:00
|
|
|
[types.executeChallenge]: state => ({
|
2018-12-07 14:08:32 +02:00
|
|
|
...state,
|
|
|
|
currentTab: 3
|
2018-04-06 14:51:52 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
initialState
|
|
|
|
);
|
2018-09-21 17:30:16 +03:00
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
const resetProjectFormValues = handleActions(
|
|
|
|
{
|
2018-09-21 17:30:16 +03:00
|
|
|
[types.updateProjectFormValues]: (state, { payload: { solution } }) => {
|
|
|
|
if (!solution) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
solution: {},
|
|
|
|
githubLink: {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const formReducer = reduxFormReducer.plugin({
|
|
|
|
'frond-end-form': resetProjectFormValues,
|
|
|
|
'back-end-form': resetProjectFormValues
|
|
|
|
});
|