diff --git a/src/components/people/PeopleList.js b/src/components/people/PeopleList.js index 17e6fad..cf880b8 100644 --- a/src/components/people/PeopleList.js +++ b/src/components/people/PeopleList.js @@ -17,7 +17,7 @@ class PeopleList extends Component { return ( - {connectDragSource(

{person.firstName} {person.lastName}

)} -

{person.email}

- + + { + interpolated => ( +
+ {connectDragSource(

{person.firstName} {person.lastName}

)} +

{person.email}

+
+ ) + } +
) } } diff --git a/src/config.js b/src/config.js index 2782a0b..161087f 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 = 'react-course-f262b' firebase.initializeApp({ - apiKey: "AIzaSyB31xpTtp4Jln_hb2kAbE4PGf6Mi8EgLyA", authDomain: `${appName}.firebaseapp.com`, databaseURL: `https://${appName}.firebaseio.com`, projectId: appName, - storageBucket: "", - messagingSenderId: "397157634637" + apiKey: "AIzaSyAt-smU_1tqNdkj9pmSpVLxqaOjhDwf4dk", + storageBucket: "react-course-f262b.appspot.com", + messagingSenderId: "447165438476" }) \ No newline at end of file diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 09ebeda..b84f7f9 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -2,8 +2,9 @@ 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 {replace} from 'react-router-redux' +import {eventChannel} from 'redux-saga' /** * Constants @@ -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 **/ @@ -134,7 +126,35 @@ export function * watchStatusChangeSaga() { } } +const createAuthSocket = () => eventChannel(emit => { + const auth = firebase.auth(); + const callback = user => emit({ user }) + + auth.onAuthStateChanged(callback) + + // empty function for unsubscribe + return () => {}; +}) + +export const realtimeAuthSaga = function * () { + const chan = yield call(createAuthSocket) + + while (true) { + const { user } = yield take(chan) + + if (user) { + yield put({ + type: SIGN_IN_SUCCESS, + payload: { user } + }) + } + } +} + + export function * saga() { + yield spawn(realtimeAuthSaga) + yield all([ signUpSaga(), signInSaga(), diff --git a/src/ducks/people.js b/src/ducks/people.js index 509323e..2c85cb5 100644 --- a/src/ducks/people.js +++ b/src/ducks/people.js @@ -6,6 +6,7 @@ import {delay, eventChannel} from 'redux-saga' import firebase from 'firebase' import {createSelector} from 'reselect' import {fbToEntities} from './utils' +import {LOCATION_CHANGE} from 'react-router-redux'; /** * Constants @@ -160,15 +161,16 @@ export const syncPeopleWithShortPollingSaga = function * () { } export const cancelableSyncSaga = function * () { - const res = yield race({ - sync: syncPeopleWithShortPollingSaga(), - timeout: delay(6000) - }) -/* - const task = yield fork(syncPeopleWithShortPollingSaga) - yield delay(6000) - yield cancel(task) -*/ + let task = yield fork(realtimePeopleSyncSaga) + + while (true) { + const {payload: {pathname}} = yield take(LOCATION_CHANGE); + if (pathname !== '/people' && pathname !== '/admin') { + yield cancel(task); + } else { + task = yield fork(realtimePeopleSyncSaga) + } + } } const createPeopleSocket = () => eventChannel(emit => { @@ -183,18 +185,25 @@ const createPeopleSocket = () => eventChannel(emit => { export const realtimePeopleSyncSaga = function * () { const chan = yield call(createPeopleSocket) - while (true) { - const { data } = yield take(chan) + try { + while (true) { + const { data } = yield take(chan) - yield put({ - type: FETCH_ALL_SUCCESS, - payload: data.val() - }) + yield put({ + type: FETCH_ALL_SUCCESS, + payload: data.val() + }) + } + } finally { + if (yield cancelled()) { + chan.close(); + console.log('People channel closed'); + } } } export function * saga() { - yield spawn(realtimePeopleSyncSaga) + yield spawn(cancelableSyncSaga) yield all([ takeEvery(ADD_PERSON_REQUEST, addPersonSaga),