diff --git a/src/config.js b/src/config.js index 2782a0b..8f59c54 100644 --- a/src/config.js +++ b/src/config.js @@ -1,12 +1,12 @@ import firebase from 'firebase' -export const appName = 'advreact-1610' +export const appName = 'advreact-1610-kys' firebase.initializeApp({ - apiKey: "AIzaSyB31xpTtp4Jln_hb2kAbE4PGf6Mi8EgLyA", + apiKey: "AIzaSyCuzZg-9QjuEEIHNSmJEL4VaAb3QtKb4xQ", authDomain: `${appName}.firebaseapp.com`, databaseURL: `https://${appName}.firebaseio.com`, projectId: appName, storageBucket: "", - messagingSenderId: "397157634637" + messagingSenderId: "437473203897" }) \ No newline at end of file diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 09ebeda..049d7a9 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -2,7 +2,8 @@ import {appName} from '../config' import {Record} from 'immutable' import firebase from 'firebase' import {createSelector} from 'reselect' -import {call, put, all, take} from 'redux-saga/effects' +import {call, put, all, take, spawn} from 'redux-saga/effects' +import {eventChannel} from 'redux-saga' import {replace} from 'react-router-redux' /** @@ -69,15 +70,6 @@ export function signIn(email, password) { } } -firebase.auth().onAuthStateChanged(user => { - if (!user) return - - window.store.dispatch({ - type: SIGN_IN_SUCCESS, - payload: { user } - }) -}) - /** * Sagas **/ @@ -126,11 +118,32 @@ export const signInSaga = function * () { } } +const createAuthSocket = () => eventChannel(emit => { + const callback = user => emit({ user }) + + let unsubscribe = firebase.auth().onAuthStateChanged(callback) + + return unsubscribe +}) + export function * watchStatusChangeSaga() { + + const chan = yield call(createAuthSocket) + while (true) { - yield take(SIGN_IN_SUCCESS) - yield (put(replace('/admin'))) + const { user } = yield take(chan) + + if(user){ + + yield put({ + type: SIGN_IN_SUCCESS, + payload: { user } + }) + + yield (put(replace('/people'))) + + } } } diff --git a/src/ducks/people.js b/src/ducks/people.js index 509323e..87ec6d4 100644 --- a/src/ducks/people.js +++ b/src/ducks/people.js @@ -177,24 +177,61 @@ const createPeopleSocket = () => eventChannel(emit => { ref.on('value', callback) - return () => ref.off('value', callback) + return () => {console.log('YA-HA_HA');ref.off('value', callback)} }) +export const cancelableRealtimePeopleSyncSaga = function * () { + let task + while (true) { + const {payload} = yield take('@@router/LOCATION_CHANGE') + console.log('---payload cancelableRealtimePeopleSyncSaga: ', payload); + + if (payload && payload.pathname.includes('people')) { + task = yield fork(realtimePeopleSyncSaga) + + } else if (task) { + yield cancel(task) + } + } +} + export const realtimePeopleSyncSaga = function * () { const chan = yield call(createPeopleSocket) - while (true) { - const { data } = yield take(chan) + try{ + while (true) { + - yield put({ - type: FETCH_ALL_SUCCESS, - payload: data.val() - }) + /* Вопрос: + * Не понимаю, почему следующие две строчки срабатывают только один раз + * если yield spawn(realtimePeopleSyncSaga) + * */ + const {payload} = yield take('@@router/LOCATION_CHANGE') + console.log('---payload: ', payload); + + + + const { data } = yield take(chan) + + yield put({ + type: FETCH_ALL_SUCCESS, + payload: data.val() + }) + } + } finally { + if (yield cancelled()) { + console.log('---start to cancel'); + yield call([chan, chan.close]) + console.log('---finish to cancel'); + + } } + } export function * saga() { - yield spawn(realtimePeopleSyncSaga) + // yield spawn(realtimePeopleSyncSaga) + yield spawn(cancelableRealtimePeopleSyncSaga) yield all([ takeEvery(ADD_PERSON_REQUEST, addPersonSaga),