From 3c28d453000127e7dea837ac2f96c0bb0e921c57 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 22 Feb 2019 13:03:04 +0200 Subject: [PATCH] Avoid rerendering --- app/src/config/ipfsOptions.js | 2 +- app/src/index.js | 4 ++++ app/src/redux/reducers/userReducer.js | 4 ++-- app/src/redux/sagas/orbitSaga.js | 2 +- app/src/redux/sagas/userSaga.js | 28 +++++++++++++++++---------- app/src/redux/store.js | 2 -- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/src/config/ipfsOptions.js b/app/src/config/ipfsOptions.js index 4ff1cf5..3cdac53 100644 --- a/app/src/config/ipfsOptions.js +++ b/app/src/config/ipfsOptions.js @@ -8,7 +8,7 @@ const ipfsOptions = { Swarm: [ '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star', // Use local signal server (https://github.com/libp2p/js-libp2p-websocket-star-rendezvous) - '/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star' + //'/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star' ] } } diff --git a/app/src/index.js b/app/src/index.js index b909cf3..45ee126 100644 --- a/app/src/index.js +++ b/app/src/index.js @@ -2,6 +2,7 @@ import React from 'react'; import { render } from 'react-dom'; import { Provider } from 'react-redux'; import { ConnectedRouter } from 'connected-react-router' +import { Drizzle } from 'drizzle'; import store, {history} from './redux/store'; import routes from './router/routes' @@ -9,9 +10,12 @@ import { initIPFS } from './orbit' import * as serviceWorker from './utils/serviceWorker'; import './assets/css/index.css'; +import drizzleOptions from "./config/drizzleOptions"; initIPFS(); +new Drizzle(drizzleOptions, store); + render( diff --git a/app/src/redux/reducers/userReducer.js b/app/src/redux/reducers/userReducer.js index 0381ae3..81e23a5 100644 --- a/app/src/redux/reducers/userReducer.js +++ b/app/src/redux/reducers/userReducer.js @@ -7,13 +7,13 @@ const initialState = { const userReducer = (state = initialState, action) => { switch (action.type) { - case 'USER_HAS_SIGNED_UP': + case 'USER_DATA_UPDATED_(AUTHENTICATED)': return { username: action.username, address: action.address, hasSignedUp: true }; - case 'USER_IS_GUEST': + case 'USER_DATA_UPDATED_(GUEST)': return { username: "", address: action.address, diff --git a/app/src/redux/sagas/orbitSaga.js b/app/src/redux/sagas/orbitSaga.js index 9eabacf..5f5e1ef 100644 --- a/app/src/redux/sagas/orbitSaga.js +++ b/app/src/redux/sagas/orbitSaga.js @@ -35,7 +35,7 @@ function* getOrbitDBInfo() { function* orbitSaga() { yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); yield take('IPFS_INITIALIZED'); - yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); + yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); //TODO: takeEvery (?) } export default orbitSaga; diff --git a/app/src/redux/sagas/userSaga.js b/app/src/redux/sagas/userSaga.js index 168eb82..f1c7e83 100644 --- a/app/src/redux/sagas/userSaga.js +++ b/app/src/redux/sagas/userSaga.js @@ -1,4 +1,4 @@ -import { call, put, take, takeEvery } from 'redux-saga/effects' +import {call, put, select, take, takeEvery} from 'redux-saga/effects' import { contract, getCurrentAccount } from './drizzleUtilsSaga'; @@ -12,21 +12,26 @@ function* updateUserData() { } const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]); try { + const userState = yield call(getUserState); const callResult = yield call(txObj1.call, {address:account}); if(callResult) { const txObj2 = yield call(contract.methods["getUsername"], ...[account]); const username = yield call(txObj2.call, {address:account}); - const dispatchArgs = { - address: account, - username: username - }; - yield put({type: 'USER_HAS_SIGNED_UP', ...dispatchArgs}); + if(account!==userState.address || username!==userState.username){ + const dispatchArgs = { + address: account, + username: username + }; + yield put({type: 'USER_DATA_UPDATED_(AUTHENTICATED)', ...dispatchArgs}); + } } else{ - const dispatchArgs = { - address: account - }; - yield put({type: 'USER_IS_GUEST', ...dispatchArgs}); + if(account!==userState.address){ + const dispatchArgs = { + address: account + }; + yield put({type: 'USER_DATA_UPDATED_(GUEST)', ...dispatchArgs}); + } } } catch (error) { @@ -35,6 +40,9 @@ function* updateUserData() { } } +function* getUserState(){ + return yield select((state) => state.user); +} function* userSaga() { yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); diff --git a/app/src/redux/store.js b/app/src/redux/store.js index 8ad1ff1..a64bb7d 100644 --- a/app/src/redux/store.js +++ b/app/src/redux/store.js @@ -27,8 +27,6 @@ const store = createStore( composedEnhancers ); -new Drizzle(drizzleOptions, store); - sagaMiddleware.run(rootSaga); export default store; \ No newline at end of file