Fix(challenges): load legacy code from localStorage
This commit is contained in:
@ -4,32 +4,46 @@ import types from '../../common/app/routes/challenges/redux/types';
|
|||||||
import {
|
import {
|
||||||
savedCodeFound
|
savedCodeFound
|
||||||
} from '../../common/app/routes/challenges/redux/actions';
|
} from '../../common/app/routes/challenges/redux/actions';
|
||||||
|
import {
|
||||||
|
updateContents
|
||||||
|
} from '../../common/utils/polyvinyl';
|
||||||
|
|
||||||
const legecyPrefixes = [
|
const legacyPrefixes = [
|
||||||
'Bonfire: ',
|
'Bonfire: ',
|
||||||
'Waypoint: ',
|
'Waypoint: ',
|
||||||
'Zipline: ',
|
'Zipline: ',
|
||||||
'Basejump: ',
|
'Basejump: ',
|
||||||
'Checkpoint: '
|
'Checkpoint: '
|
||||||
];
|
];
|
||||||
|
const legacyPostfix = 'Val';
|
||||||
|
|
||||||
function getCode(id, legacy) {
|
function getCode(id) {
|
||||||
if (store.has(id)) {
|
if (store.has(id)) {
|
||||||
return store.get(id);
|
return store.get(id);
|
||||||
}
|
}
|
||||||
if (store.has(legacy)) {
|
return null;
|
||||||
const code = '' + store.get(legacy);
|
}
|
||||||
store.remove(legacy);
|
|
||||||
|
function getLegacyCode(legacy) {
|
||||||
|
const key = legacy + legacyPostfix;
|
||||||
|
let code = null;
|
||||||
|
if (store.has(key)) {
|
||||||
|
code = '' + store.get(key);
|
||||||
|
store.remove(key);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
return legecyPrefixes.reduce((code, prefix) => {
|
return legacyPrefixes.reduce((code, prefix) => {
|
||||||
if (code) {
|
if (code) {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
return store.get(prefix + legacy + 'Val');
|
return store.get(prefix + key);
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function legacyToFile(code, files, key) {
|
||||||
|
return { [key]: updateContents(code, files[key]) };
|
||||||
|
}
|
||||||
|
|
||||||
export default function codeStorageSaga(actions$, getState) {
|
export default function codeStorageSaga(actions$, getState) {
|
||||||
return actions$
|
return actions$
|
||||||
.filter(({ type }) => (
|
.filter(({ type }) => (
|
||||||
@ -37,15 +51,26 @@ export default function codeStorageSaga(actions$, getState) {
|
|||||||
type === types.loadCode
|
type === types.loadCode
|
||||||
))
|
))
|
||||||
.map(({ type }) => {
|
.map(({ type }) => {
|
||||||
const { id = '', files = {}, legacyKey = '' } = getState().challengesApp;
|
const {
|
||||||
|
challengesApp: {
|
||||||
|
id = '',
|
||||||
|
files = {},
|
||||||
|
legacyKey = '',
|
||||||
|
key
|
||||||
|
}
|
||||||
|
} = getState();
|
||||||
if (type === types.saveCode) {
|
if (type === types.saveCode) {
|
||||||
store.set(id, files);
|
store.set(id, files);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const codeFound = getCode(id, legacyKey);
|
const codeFound = getCode(id);
|
||||||
if (codeFound) {
|
if (codeFound) {
|
||||||
return savedCodeFound(codeFound);
|
return savedCodeFound(codeFound);
|
||||||
}
|
}
|
||||||
|
const legacyCode = getLegacyCode(legacyKey);
|
||||||
|
if (legacyCode) {
|
||||||
|
return savedCodeFound(legacyToFile(legacyCode, files, key));
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user