Browse Source

userSaga init

develop
Ezerous 7 years ago
parent
commit
48332a4510
  1. 3
      package.json
  2. 1
      src/containers/UsernameFormContainer.js
  3. 8
      src/drizzleOptions.js
  4. 3
      src/layouts/home/Home.js
  5. 6
      src/reducer.js
  6. 8
      src/rootSaga.js
  7. 26
      src/userReducer.js
  8. 74
      src/userSaga.js

3
package.json

@ -19,9 +19,8 @@
"react-router": "^3.2.1",
"react-router-redux": "^4.0.8",
"redux": "^3.7.2",
"redux-auth-wrapper": "1.1.0",
"redux-saga": "0.16.0",
"web3":"^1.0.0-beta.34"
"web3": "^1.0.0-beta.34"
},
"scripts": {
"start": "react-scripts start",

1
src/containers/UsernameFormContainer.js

@ -45,7 +45,6 @@ class UsernameFormContainer extends Component {
}
}
console.dir(initialState);
this.state = initialState;
}

8
src/drizzleOptions.js

@ -11,8 +11,12 @@ const drizzleOptions = {
Forum
],
events: {
Forum: ['UserSignedUp']
}
Forum: ['UserSignedUp', 'UsernameUpdated']
},
polls: {
accounts: 3000,
blocks: 3000
},
};
export default drizzleOptions

3
src/layouts/home/Home.js

@ -1,5 +1,5 @@
import React, { Component } from 'react'
import { AccountData, ContractData } from 'drizzle-react-components'
import { ContractData } from 'drizzle-react-components'
import UsernameFormContainer from '../../containers/UsernameFormContainer'
class Home extends Component {
@ -13,7 +13,6 @@ class Home extends Component {
</div>
<div className="pure-u-1-1">
<h2>Account</h2>
<AccountData accountIndex="0" units="ether" precision="3" />
<p><strong>Username</strong>: <ContractData contract="Forum" method="getUsername" methodArgs={[this.props.accounts[0]]}/></p>
<UsernameFormContainer/>
<br/><br/>

6
src/reducer.js

@ -1,10 +1,12 @@
import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import { drizzleReducers } from 'drizzle'
import userReducer from "./userReducer";
const reducer = combineReducers({
routing: routerReducer,
...drizzleReducers
routing: routerReducer,
user: userReducer,
...drizzleReducers
});
export default reducer

8
src/rootSaga.js

@ -1,8 +1,10 @@
import { all, fork } from 'redux-saga/effects'
import { drizzleSagas } from 'drizzle'
import userSaga from "./userSaga";
export default function* root() {
yield all(
drizzleSagas.map(saga => fork(saga))
)
let sagas = [...drizzleSagas,userSaga];
yield all(
sagas.map(saga => fork(saga))
)
}

26
src/userReducer.js

@ -0,0 +1,26 @@
const initialState = {
username: "Guest",
address: "0x0",
hasSignedUp: false
};
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'USER_HAS_SIGNED_UP':
return {
username: action.username,
address: action.address,
hasSignedUp: true
};
case 'USER_IS_GUEST':
return {
username: "Guest",
address: action.address,
hasSignedUp: false
};
default:
return state
}
};
export default userReducer

74
src/userSaga.js

@ -0,0 +1,74 @@
import { call, put, select, takeLatest, takeEvery } from 'redux-saga/effects'
const accounts = (state) => state.accounts;
let account;
let initFlag = false;
let forumContract;
let contractGrabbed = false;
function* initUser() {
if(!initFlag)
{
while(true)
if(contractGrabbed)
{
yield call(getUserData);
initFlag=true;
break;
}
}
}
function grabContract({contract}) {
if(!contractGrabbed)
{
forumContract = contract;
contractGrabbed=true;
}
}
function* updateUserData() {
if(initFlag)
yield call(getUserData);
}
function* getUserData() {
account = (yield select(accounts))[0];
forumContract.methods["hasUserSignedUp"].cacheCall(...[account]);
const txObject = yield call(forumContract.methods["hasUserSignedUp"], ...[account]);
try {
const callResult = yield call(txObject.call, {address:account});
if(callResult)
{
const username = yield call(forumContract.methods["getUsername"], ...[account]);
const dispatchArgs = {
address: account,
username: username
};
yield put({type: 'USER_HAS_SIGNED_UP', ...dispatchArgs}); //TODO: only dispatch if needed
}
else{
const dispatchArgs = {
address: account,
};
yield put({type: 'USER_IS_GUEST', ...dispatchArgs}); //TODO: only dispatch if needed
}
}
catch (error) {
console.error(error);
yield put({type: 'USER_FETCHING_ERROR', ...[]})
}
}
function* userSaga() {
yield takeLatest('LISTEN_FOR_EVENT', grabContract);
yield takeLatest("DRIZZLE_INITIALIZED", initUser);
yield takeEvery("ACCOUNTS_FETCHED", updateUserData);
}
export default userSaga;
Loading…
Cancel
Save