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: [
'/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'
]
}
}

4
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(
<Provider store={store}>
<ConnectedRouter history={history}>

4
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,

2
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;

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';
@ -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");

2
app/src/redux/store.js

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