Feature(code-uri): Load and remove code uri on loadCode

This commit is contained in:
Berkeley Martinez
2016-08-15 12:10:09 -07:00
parent 3f3aab3ff7
commit c919ce5dc8
5 changed files with 46 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import { Observable } from 'rx';
import store from 'store';
import { removeCodeUri, getCodeUri } from '../utils/code-uri';
import { ofType } from '../../common/utils/get-actions-of-type';
import { updateContents } from '../../common/utils/polyvinyl';
import combineSagas from '../../common/utils/combine-sagas';
@ -9,7 +10,8 @@ import { makeToast } from '../../common/app/toasts/redux/actions';
import types from '../../common/app/routes/challenges/redux/types';
import {
savedCodeFound,
updateMain
updateMain,
lockUntrustedCode
} from '../../common/app/routes/challenges/redux/actions';
const legacyPrefixes = [
@ -58,7 +60,7 @@ export function saveCodeSaga(actions, getState) {
});
}
export function loadCodeSaga(actions$, getState) {
export function loadCodeSaga(actions$, getState, { window, location }) {
return actions$
::ofType(types.loadCode)
.flatMap(() => {
@ -71,6 +73,21 @@ export function loadCodeSaga(actions$, getState) {
key
}
} = getState();
const codeUriFound = getCodeUri(
location,
window.decodeURIComponent
);
if (codeUriFound) {
finalFiles = legacyToFile(codeUriFound, files, key);
removeCodeUri(location, window.history);
return Observable.of(
lockUntrustedCode(),
makeToast({
message: 'I found code in the URI. Loading now'
}),
savedCodeFound(finalFiles)
);
}
const codeFound = getCode(id);
if (codeFound) {

View File

@ -49,7 +49,7 @@ export function getLegacySolutionFromQuery(query = '', decode) {
)(query, 'solution');
}
export function getCodeUri({ location, decodeURIComponent }) {
export function getCodeUri(location, decodeURIComponent) {
let query;
if (
location.search &&
@ -62,3 +62,18 @@ export function getCodeUri({ location, decodeURIComponent }) {
return getLegacySolutionFromQuery(query, decodeURIComponent);
}
export function removeCodeUri(location, history) {
if (
typeof location.search.split !== 'function' ||
typeof history.replaceState !== 'function'
) {
return false;
}
history.replaceState(
history.state,
null,
location.search.split('?')[0]
);
return true;
}

View File

@ -22,6 +22,7 @@ export const fetchChallengeCompleted = createAction(
);
export const resetUi = createAction(types.resetUi);
export const updateHint = createAction(types.updateHint);
export const lockUntrustedCode = createAction(types.lockUntrustedCode);
export const fetchChallenges = createAction(types.fetchChallenges);
export const fetchChallengesCompleted = createAction(

View File

@ -45,6 +45,7 @@ const initialUiState = {
shouldShowQuestions: false
};
const initialState = {
isCodeLocked: false,
id: '',
challenge: '',
helpChatRoom: 'Help',
@ -88,6 +89,14 @@ const mainReducer = handleActions(
0 :
state.hintIndex + 1
}),
[types.lockUntrustedCode]: state => ({
...state,
isCodeLocked: true
}),
[types.unlockCode]: state => ({
...state,
isCodeLocked: false
}),
[types.executeChallenge]: state => ({
...state,
tests: state.tests.map(test => ({ ...test, err: false, pass: false }))

View File

@ -17,6 +17,7 @@ export default createTypes([
'replaceChallenge',
'resetUi',
'updateHint',
'lockUntrustedCode',
// map
'updateFilter',