Chore(challenges): separate code storage sagas
This commit is contained in:
@ -1,15 +1,16 @@
|
|||||||
import { Observable } from 'rx';
|
import { Observable } from 'rx';
|
||||||
import store from 'store';
|
import store from 'store';
|
||||||
|
|
||||||
|
import { ofType } from '../../common/utils/get-actions-of-type';
|
||||||
|
import { updateContents } from '../../common/utils/polyvinyl';
|
||||||
|
import combineSagas from '../../common/utils/combine-sagas';
|
||||||
|
|
||||||
import { makeToast } from '../../common/app/toasts/redux/actions';
|
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
|
||||||
} from '../../common/app/routes/challenges/redux/actions';
|
} from '../../common/app/routes/challenges/redux/actions';
|
||||||
import {
|
|
||||||
updateContents
|
|
||||||
} from '../../common/utils/polyvinyl';
|
|
||||||
|
|
||||||
const legacyPrefixes = [
|
const legacyPrefixes = [
|
||||||
'Bonfire: ',
|
'Bonfire: ',
|
||||||
@ -47,13 +48,20 @@ function legacyToFile(code, files, key) {
|
|||||||
return { [key]: updateContents(code, files[key]) };
|
return { [key]: updateContents(code, files[key]) };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function codeStorageSaga(actions$, getState) {
|
export function saveCodeSaga(actions, getState) {
|
||||||
|
return actions
|
||||||
|
::ofType(types.saveCode)
|
||||||
|
.map(() => {
|
||||||
|
const { challengesApp: { id = '', files = {} } } = getState();
|
||||||
|
store.set(id, files);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadCodeSaga(actions$, getState) {
|
||||||
return actions$
|
return actions$
|
||||||
.filter(({ type }) => (
|
::ofType(types.loadCode)
|
||||||
type === types.saveCode ||
|
.flatMap(() => {
|
||||||
type === types.loadCode
|
|
||||||
))
|
|
||||||
.flatMap(({ type }) => {
|
|
||||||
let finalFiles;
|
let finalFiles;
|
||||||
const {
|
const {
|
||||||
challengesApp: {
|
challengesApp: {
|
||||||
@ -63,10 +71,6 @@ export default function codeStorageSaga(actions$, getState) {
|
|||||||
key
|
key
|
||||||
}
|
}
|
||||||
} = getState();
|
} = getState();
|
||||||
if (type === types.saveCode) {
|
|
||||||
store.set(id, files);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const codeFound = getCode(id);
|
const codeFound = getCode(id);
|
||||||
if (codeFound) {
|
if (codeFound) {
|
||||||
@ -90,3 +94,5 @@ export default function codeStorageSaga(actions$, getState) {
|
|||||||
return Observable.empty();
|
return Observable.empty();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default combineSagas(saveCodeSaga, loadCodeSaga);
|
||||||
|
@ -13,7 +13,7 @@ import {
|
|||||||
} from './selectors';
|
} from './selectors';
|
||||||
import { updateCurrentChallenge } from '../routes/challenges/redux/actions';
|
import { updateCurrentChallenge } from '../routes/challenges/redux/actions';
|
||||||
import getActionsOfType from '../../utils/get-actions-of-type';
|
import getActionsOfType from '../../utils/get-actions-of-type';
|
||||||
import combineSagas from '../utils/combine-sagas';
|
import combineSagas from '../../utils/combine-sagas';
|
||||||
import { postJSON$ } from '../../utils/ajax-stream';
|
import { postJSON$ } from '../../utils/ajax-stream';
|
||||||
|
|
||||||
const log = debug('fcc:app/redux/load-current-challenge-saga');
|
const log = debug('fcc:app/redux/load-current-challenge-saga');
|
||||||
|
@ -2,7 +2,6 @@ import { Observable } from 'rx';
|
|||||||
import { push } from 'react-router-redux';
|
import { push } from 'react-router-redux';
|
||||||
|
|
||||||
import { types } from './actions';
|
import { types } from './actions';
|
||||||
import combineSagas from '../../../utils/combine-sagas';
|
|
||||||
import { makeToast } from '../../../toasts/redux/actions';
|
import { makeToast } from '../../../toasts/redux/actions';
|
||||||
import { fetchChallenges } from '../../challenges/redux/actions';
|
import { fetchChallenges } from '../../challenges/redux/actions';
|
||||||
import {
|
import {
|
||||||
@ -14,6 +13,7 @@ import {
|
|||||||
import { userSelector } from '../../../redux/selectors';
|
import { userSelector } from '../../../redux/selectors';
|
||||||
import { postJSON$ } from '../../../../utils/ajax-stream';
|
import { postJSON$ } from '../../../../utils/ajax-stream';
|
||||||
import langs from '../../../../utils/supported-languages';
|
import langs from '../../../../utils/supported-languages';
|
||||||
|
import combineSagas from '../../../../utils/combine-sagas';
|
||||||
|
|
||||||
const urlMap = {
|
const urlMap = {
|
||||||
isLocked: 'lockdown',
|
isLocked: 'lockdown',
|
||||||
|
@ -1,3 +1,20 @@
|
|||||||
|
// redux-observable compatible operator
|
||||||
|
export function ofType(...keys) {
|
||||||
|
return this.filter(({ type }) => {
|
||||||
|
const len = keys.length;
|
||||||
|
if (len === 1) {
|
||||||
|
return type === keys[0];
|
||||||
|
} else {
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
if (keys[i] === type) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default function getActionsOfType(actions, ...types) {
|
export default function getActionsOfType(actions, ...types) {
|
||||||
const length = types.length;
|
const length = types.length;
|
||||||
return actions
|
return actions
|
||||||
|
Reference in New Issue
Block a user