2016-05-27 22:07:10 -07:00
|
|
|
import store from 'store';
|
|
|
|
|
|
|
|
import types from '../../common/app/routes/challenges/redux/types';
|
|
|
|
import {
|
|
|
|
savedCodeFound
|
|
|
|
} from '../../common/app/routes/challenges/redux/actions';
|
2016-08-13 14:58:16 -07:00
|
|
|
import {
|
|
|
|
updateContents
|
|
|
|
} from '../../common/utils/polyvinyl';
|
2016-05-27 22:07:10 -07:00
|
|
|
|
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-05-27 22:07:10 -07:00
|
|
|
export default function codeStorageSaga(actions$, getState) {
|
|
|
|
return actions$
|
|
|
|
.filter(({ type }) => (
|
|
|
|
type === types.saveCode ||
|
|
|
|
type === types.loadCode
|
|
|
|
))
|
|
|
|
.map(({ type }) => {
|
2016-08-13 14:58:16 -07:00
|
|
|
const {
|
|
|
|
challengesApp: {
|
|
|
|
id = '',
|
|
|
|
files = {},
|
|
|
|
legacyKey = '',
|
|
|
|
key
|
|
|
|
}
|
|
|
|
} = getState();
|
2016-05-27 22:07:10 -07:00
|
|
|
if (type === types.saveCode) {
|
|
|
|
store.set(id, files);
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-13 14:58:16 -07:00
|
|
|
const codeFound = getCode(id);
|
2016-05-27 22:07:10 -07:00
|
|
|
if (codeFound) {
|
|
|
|
return savedCodeFound(codeFound);
|
|
|
|
}
|
2016-08-13 14:58:16 -07:00
|
|
|
const legacyCode = getLegacyCode(legacyKey);
|
|
|
|
if (legacyCode) {
|
|
|
|
return savedCodeFound(legacyToFile(legacyCode, files, key));
|
|
|
|
}
|
2016-05-27 22:07:10 -07:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|