Skip to content
Open

ht 5 #29

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/routes/PersonPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react'
import {connect} from 'react-redux'
import {addPerson} from '../../ducks/people'
import {addPerson, cancelPeopleSync} from '../../ducks/people'
import NewPersonForm from '../people/NewPersonForm'
import PeopleList from '../people/PeopleList'

Expand All @@ -9,6 +9,10 @@ class PersonPage extends Component {

};

componentWillUnmount() {
this.props.cancelPeopleSync();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

задача стояла делать это на изменениие роута, а то, что там есть какой-то компонент - это частный случай. Тогда и запускать синхронизацию стоит здесь

}

render() {
return (
<div>
Expand All @@ -20,4 +24,4 @@ class PersonPage extends Component {
}
}

export default connect(null, {addPerson})(PersonPage)
export default connect(null, {addPerson, cancelPeopleSync})(PersonPage)
12 changes: 6 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import firebase from 'firebase'
export const appName = 'advreact-1610'

firebase.initializeApp({
apiKey: "AIzaSyB31xpTtp4Jln_hb2kAbE4PGf6Mi8EgLyA",
authDomain: `${appName}.firebaseapp.com`,
databaseURL: `https://${appName}.firebaseio.com`,
projectId: appName,
storageBucket: "",
messagingSenderId: "397157634637"
apiKey: "AIzaSyAVvjbBMLMWUEmCRHUv2eNEkgfS-pEqcMY",
authDomain: "adv-react-ad9a1.firebaseapp.com",
databaseURL: "https://adv-react-ad9a1.firebaseio.com",
projectId: "adv-react-ad9a1",
storageBucket: "adv-react-ad9a1.appspot.com",
messagingSenderId: "598425030570"
})
32 changes: 30 additions & 2 deletions src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {eventChannel} from 'redux-saga'
import {call, put, all, take, spawn} from 'redux-saga/effects'
import {replace} from 'react-router-redux'

/**
Expand Down Expand Up @@ -69,6 +70,7 @@ export function signIn(email, password) {
}
}

/*
firebase.auth().onAuthStateChanged(user => {
if (!user) return

Expand All @@ -77,11 +79,22 @@ firebase.auth().onAuthStateChanged(user => {
payload: { user }
})
})
*/

/**
* Sagas
**/

export const createAuthSocket = () => eventChannel(emit => {
firebase.auth().onAuthStateChanged(user => {
if (!user) {
return
}
emit({ user })
})
return () => {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот это не очень хорошее решение. Вернии настоящую функцию для отписки

})

export function * signUpSaga() {
const auth = firebase.auth()

Expand Down Expand Up @@ -126,15 +139,30 @@ export const signInSaga = function * () {
}
}

export function * authSyncSaga() {
const chan = yield call(createAuthSocket)

while (true) {
const { user } = yield take(chan)

yield put({
type: SIGN_IN_SUCCESS,
payload: { user }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а если нет юзера?

})
}
}

export function * watchStatusChangeSaga() {
while (true) {
yield take(SIGN_IN_SUCCESS)

yield (put(replace('/admin')))
yield (put(replace('/people')))
}
}

export function * saga() {
yield spawn(authSyncSaga)

yield all([
signUpSaga(),
signInSaga(),
Expand Down
35 changes: 28 additions & 7 deletions src/ducks/people.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const FETCH_ALL_SUCCESS = `${prefix}/FETCH_ALL_SUCCESS`

export const ADD_EVENT_REQUEST = `${prefix}/ADD_EVENT_REQUEST`
export const ADD_EVENT_SUCCESS = `${prefix}/ADD_EVENT_SUCCESS`
export const CANCEL_PEOPLE_SYNC = `${prefix}/CANCEL_PEOPLE_SYNC`

/**
* Reducer
Expand Down Expand Up @@ -92,6 +93,12 @@ export function addEventToPerson(eventId, personId) {
}
}

export function cancelPeopleSync() {
return {
type: CANCEL_PEOPLE_SYNC,
}
}

/**
*Sagas
* */
Expand Down Expand Up @@ -183,18 +190,32 @@ 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()
}
}
}

export function * cancelPeopleSyncSaga(peopleSyncTask) {
while (true) {
yield take(CANCEL_PEOPLE_SYNC)
yield cancel(peopleSyncTask)
}
}

export function * saga() {
yield spawn(realtimePeopleSyncSaga)
const realtimeTask = yield spawn(realtimePeopleSyncSaga)
yield spawn(cancelPeopleSyncSaga, realtimeTask)

yield all([
takeEvery(ADD_PERSON_REQUEST, addPersonSaga),
Expand Down