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 { Observable } from 'rx';
import store from 'store'; import store from 'store';
import { removeCodeUri, getCodeUri } from '../utils/code-uri';
import { ofType } from '../../common/utils/get-actions-of-type'; import { ofType } from '../../common/utils/get-actions-of-type';
import { updateContents } from '../../common/utils/polyvinyl'; import { updateContents } from '../../common/utils/polyvinyl';
import combineSagas from '../../common/utils/combine-sagas'; 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 types from '../../common/app/routes/challenges/redux/types';
import { import {
savedCodeFound, savedCodeFound,
updateMain updateMain,
lockUntrustedCode
} from '../../common/app/routes/challenges/redux/actions'; } from '../../common/app/routes/challenges/redux/actions';
const legacyPrefixes = [ 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$ return actions$
::ofType(types.loadCode) ::ofType(types.loadCode)
.flatMap(() => { .flatMap(() => {
@ -71,6 +73,21 @@ export function loadCodeSaga(actions$, getState) {
key key
} }
} = getState(); } = 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); const codeFound = getCode(id);
if (codeFound) { if (codeFound) {

View File

@ -49,7 +49,7 @@ export function getLegacySolutionFromQuery(query = '', decode) {
)(query, 'solution'); )(query, 'solution');
} }
export function getCodeUri({ location, decodeURIComponent }) { export function getCodeUri(location, decodeURIComponent) {
let query; let query;
if ( if (
location.search && location.search &&
@ -62,3 +62,18 @@ export function getCodeUri({ location, decodeURIComponent }) {
return getLegacySolutionFromQuery(query, 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 resetUi = createAction(types.resetUi);
export const updateHint = createAction(types.updateHint); export const updateHint = createAction(types.updateHint);
export const lockUntrustedCode = createAction(types.lockUntrustedCode);
export const fetchChallenges = createAction(types.fetchChallenges); export const fetchChallenges = createAction(types.fetchChallenges);
export const fetchChallengesCompleted = createAction( export const fetchChallengesCompleted = createAction(

View File

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

View File

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