2016-08-13 17:53:03 -07:00
|
|
|
import { Observable } from 'rx';
|
2016-05-27 22:07:10 -07:00
|
|
|
import store from 'store';
|
|
|
|
|
2016-08-15 12:10:09 -07:00
|
|
|
import { removeCodeUri, getCodeUri } from '../utils/code-uri';
|
2016-08-13 18:09:32 -07:00
|
|
|
import { ofType } from '../../common/utils/get-actions-of-type';
|
|
|
|
import { updateContents } from '../../common/utils/polyvinyl';
|
|
|
|
import combineSagas from '../../common/utils/combine-sagas';
|
|
|
|
|
2016-08-13 17:53:03 -07:00
|
|
|
import { makeToast } from '../../common/app/toasts/redux/actions';
|
2016-05-27 22:07:10 -07:00
|
|
|
import types from '../../common/app/routes/challenges/redux/types';
|
|
|
|
import {
|
2016-08-13 17:53:03 -07:00
|
|
|
savedCodeFound,
|
2016-08-15 12:10:09 -07:00
|
|
|
updateMain,
|
|
|
|
lockUntrustedCode
|
2016-05-27 22:07:10 -07:00
|
|
|
} from '../../common/app/routes/challenges/redux/actions';
|
|
|
|
|
2016-08-13 14:58:16 -07:00
|
|
|
const legacyPrefixes = [
|
2016-05-27 22:07:10 -07:00
|
|
|
'Bonfire: ',
|
|
|
|
'Waypoint: ',
|
|
|
|
'Zipline: ',
|
|
|
|
'Basejump: ',
|
|
|
|
'Checkpoint: '
|
|
|
|
];
|
2016-08-13 14:58:16 -07:00
|
|
|
const legacyPostfix = 'Val';
|
2016-05-27 22:07:10 -07:00
|
|
|
|
2016-08-13 14:58:16 -07:00
|
|
|
function getCode(id) {
|
2016-05-27 22:07:10 -07:00
|
|
|
if (store.has(id)) {
|
|
|
|
return store.get(id);
|
|
|
|
}
|
2016-08-13 14:58:16 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLegacyCode(legacy) {
|
|
|
|
const key = legacy + legacyPostfix;
|
|
|
|
let code = null;
|
|
|
|
if (store.has(key)) {
|
|
|
|
code = '' + store.get(key);
|
|
|
|
store.remove(key);
|
2016-05-27 22:07:10 -07:00
|
|
|
return code;
|
|
|
|
}
|
2016-08-13 14:58:16 -07:00
|
|
|
return legacyPrefixes.reduce((code, prefix) => {
|
2016-05-27 22:07:10 -07:00
|
|
|
if (code) {
|
|
|
|
return code;
|
|
|
|
}
|
2016-08-13 14:58:16 -07:00
|
|
|
return store.get(prefix + key);
|
2016-05-27 22:07:10 -07:00
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
|
2016-08-13 14:58:16 -07:00
|
|
|
function legacyToFile(code, files, key) {
|
|
|
|
return { [key]: updateContents(code, files[key]) };
|
|
|
|
}
|
|
|
|
|
2016-08-13 18:09:32 -07:00
|
|
|
export function saveCodeSaga(actions, getState) {
|
|
|
|
return actions
|
|
|
|
::ofType(types.saveCode)
|
|
|
|
.map(() => {
|
|
|
|
const { challengesApp: { id = '', files = {} } } = getState();
|
|
|
|
store.set(id, files);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:10:09 -07:00
|
|
|
export function loadCodeSaga(actions$, getState, { window, location }) {
|
2016-05-27 22:07:10 -07:00
|
|
|
return actions$
|
2016-08-13 18:09:32 -07:00
|
|
|
::ofType(types.loadCode)
|
|
|
|
.flatMap(() => {
|
2016-08-13 17:53:03 -07:00
|
|
|
let finalFiles;
|
2016-08-13 14:58:16 -07:00
|
|
|
const {
|
|
|
|
challengesApp: {
|
|
|
|
id = '',
|
|
|
|
files = {},
|
|
|
|
legacyKey = '',
|
|
|
|
key
|
|
|
|
}
|
|
|
|
} = getState();
|
2016-08-15 12:10:09 -07:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
2016-08-13 17:53:03 -07:00
|
|
|
|
2016-08-13 14:58:16 -07:00
|
|
|
const codeFound = getCode(id);
|
2016-05-27 22:07:10 -07:00
|
|
|
if (codeFound) {
|
2016-08-13 17:53:03 -07:00
|
|
|
finalFiles = codeFound;
|
|
|
|
} else {
|
|
|
|
const legacyCode = getLegacyCode(legacyKey);
|
|
|
|
if (legacyCode) {
|
|
|
|
finalFiles = legacyToFile(legacyCode, files, key);
|
|
|
|
}
|
2016-05-27 22:07:10 -07:00
|
|
|
}
|
2016-08-13 17:53:03 -07:00
|
|
|
|
|
|
|
if (finalFiles) {
|
|
|
|
return Observable.of(
|
|
|
|
makeToast({
|
|
|
|
message: 'I found some saved work. Loading now'
|
|
|
|
}),
|
|
|
|
savedCodeFound(finalFiles),
|
|
|
|
updateMain()
|
|
|
|
);
|
2016-08-13 14:58:16 -07:00
|
|
|
}
|
2016-08-13 17:53:03 -07:00
|
|
|
return Observable.empty();
|
2016-05-27 22:07:10 -07:00
|
|
|
});
|
|
|
|
}
|
2016-08-13 18:09:32 -07:00
|
|
|
|
|
|
|
export default combineSagas(saveCodeSaga, loadCodeSaga);
|