import { call, fork, put, select, take, takeEvery } from 'redux-saga/effects';

import {
  types,
  syncTodos,
} from '../reducer/todos.actions';

import rsf from '../rsf';

function* syncTodosSaga() {
  const channel = yield call(rsf.channel, 'todos');

  while(true) {
    const todos = yield take(channel);
    yield put(syncTodos(
      Object.keys(todos).map(key => ({
        ...todos[key],
        id: key,
      }))
    ));
  }
}

function* saveNewTodo() {
  const uid = yield select(state => state.login.user.uid);
  const newTodo = yield select(state => state.todos.new);

  yield call(rsf.create, 'todos', {
    creator: uid,
    done: false,
    label: newTodo,
  });
}

function* setTodoStatus(action) {
  yield call(rsf.patch, `todos/${action.todoId}`, {
    done: action.done,
  });
}

export default function* rootSaga() {
  yield fork(syncTodosSaga);
  yield [
    takeEvery(types.TODOS.NEW.SAVE, saveNewTodo),
    takeEvery(types.TODOS.SET_STATUS, setTodoStatus),
  ];
}

@@ -467,18 +467,19 @@
  const u
-id
+ser
  = yield
@@ -515,12 +515,8 @@
 user
-.uid
 );%0A 
@@ -623,18 +623,37 @@
 eator: u
-id
+ser ? user.uid : null
 ,%0A    do

Allow todo creation by non logged-in users.