Browse Source

Avoid rerendering

develop
Ezerous 6 years ago
parent
commit
3c28d45300
  1. 2
      app/src/config/ipfsOptions.js
  2. 4
      app/src/index.js
  3. 4
      app/src/redux/reducers/userReducer.js
  4. 2
      app/src/redux/sagas/orbitSaga.js
  5. 28
      app/src/redux/sagas/userSaga.js
  6. 2
      app/src/redux/store.js

2
app/src/config/ipfsOptions.js

@ -8,7 +8,7 @@ const ipfsOptions = {
Swarm: [ Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star', '/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) // 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'
] ]
} }
} }

4
app/src/index.js

@ -2,6 +2,7 @@ import React from 'react';
import { render } from 'react-dom'; import { render } from 'react-dom';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { ConnectedRouter } from 'connected-react-router' import { ConnectedRouter } from 'connected-react-router'
import { Drizzle } from 'drizzle';
import store, {history} from './redux/store'; import store, {history} from './redux/store';
import routes from './router/routes' import routes from './router/routes'
@ -9,9 +10,12 @@ import { initIPFS } from './orbit'
import * as serviceWorker from './utils/serviceWorker'; import * as serviceWorker from './utils/serviceWorker';
import './assets/css/index.css'; import './assets/css/index.css';
import drizzleOptions from "./config/drizzleOptions";
initIPFS(); initIPFS();
new Drizzle(drizzleOptions, store);
render( render(
<Provider store={store}> <Provider store={store}>
<ConnectedRouter history={history}> <ConnectedRouter history={history}>

4
app/src/redux/reducers/userReducer.js

@ -7,13 +7,13 @@ const initialState = {
const userReducer = (state = initialState, action) => { const userReducer = (state = initialState, action) => {
switch (action.type) { switch (action.type) {
case 'USER_HAS_SIGNED_UP': case 'USER_DATA_UPDATED_(AUTHENTICATED)':
return { return {
username: action.username, username: action.username,
address: action.address, address: action.address,
hasSignedUp: true hasSignedUp: true
}; };
case 'USER_IS_GUEST': case 'USER_DATA_UPDATED_(GUEST)':
return { return {
username: "", username: "",
address: action.address, address: action.address,

2
app/src/redux/sagas/orbitSaga.js

@ -35,7 +35,7 @@ function* getOrbitDBInfo() {
function* orbitSaga() { function* orbitSaga() {
yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); yield take("DRIZZLE_UTILS_SAGA_INITIALIZED");
yield take('IPFS_INITIALIZED'); yield take('IPFS_INITIALIZED');
yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); yield takeLatest("ACCOUNT_CHANGED", getOrbitDBInfo); //TODO: takeEvery (?)
} }
export default orbitSaga; export default orbitSaga;

28
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'; import { contract, getCurrentAccount } from './drizzleUtilsSaga';
@ -12,21 +12,26 @@ function* updateUserData() {
} }
const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]); const txObj1 = yield call(contract.methods["hasUserSignedUp"], ...[account]);
try { try {
const userState = yield call(getUserState);
const callResult = yield call(txObj1.call, {address:account}); const callResult = yield call(txObj1.call, {address:account});
if(callResult) { if(callResult) {
const txObj2 = yield call(contract.methods["getUsername"], ...[account]); const txObj2 = yield call(contract.methods["getUsername"], ...[account]);
const username = yield call(txObj2.call, {address:account}); const username = yield call(txObj2.call, {address:account});
const dispatchArgs = { if(account!==userState.address || username!==userState.username){
address: account, const dispatchArgs = {
username: username address: account,
}; username: username
yield put({type: 'USER_HAS_SIGNED_UP', ...dispatchArgs}); };
yield put({type: 'USER_DATA_UPDATED_(AUTHENTICATED)', ...dispatchArgs});
}
} }
else{ else{
const dispatchArgs = { if(account!==userState.address){
address: account const dispatchArgs = {
}; address: account
yield put({type: 'USER_IS_GUEST', ...dispatchArgs}); };
yield put({type: 'USER_DATA_UPDATED_(GUEST)', ...dispatchArgs});
}
} }
} }
catch (error) { catch (error) {
@ -35,6 +40,9 @@ function* updateUserData() {
} }
} }
function* getUserState(){
return yield select((state) => state.user);
}
function* userSaga() { function* userSaga() {
yield take("DRIZZLE_UTILS_SAGA_INITIALIZED"); yield take("DRIZZLE_UTILS_SAGA_INITIALIZED");

2
app/src/redux/store.js

@ -27,8 +27,6 @@ const store = createStore(
composedEnhancers composedEnhancers
); );
new Drizzle(drizzleOptions, store);
sagaMiddleware.run(rootSaga); sagaMiddleware.run(rootSaga);
export default store; export default store;
Loading…
Cancel
Save