diff --git a/package.json b/package.json index 159cbbf..0de2376 100644 --- a/package.json +++ b/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", diff --git a/src/containers/UsernameFormContainer.js b/src/containers/UsernameFormContainer.js index 469a47f..8b299cb 100644 --- a/src/containers/UsernameFormContainer.js +++ b/src/containers/UsernameFormContainer.js @@ -45,7 +45,6 @@ class UsernameFormContainer extends Component { } } - console.dir(initialState); this.state = initialState; } diff --git a/src/drizzleOptions.js b/src/drizzleOptions.js index e6889d8..88e4204 100644 --- a/src/drizzleOptions.js +++ b/src/drizzleOptions.js @@ -11,8 +11,12 @@ const drizzleOptions = { Forum ], events: { - Forum: ['UserSignedUp'] - } + Forum: ['UserSignedUp', 'UsernameUpdated'] + }, + polls: { + accounts: 3000, + blocks: 3000 + }, }; export default drizzleOptions \ No newline at end of file diff --git a/src/layouts/home/Home.js b/src/layouts/home/Home.js index 2b68cc5..974deae 100644 --- a/src/layouts/home/Home.js +++ b/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 {

Account

-

Username:



diff --git a/src/reducer.js b/src/reducer.js index 9db9303..d49b26d 100644 --- a/src/reducer.js +++ b/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 diff --git a/src/rootSaga.js b/src/rootSaga.js index d9f654b..8206b5a 100644 --- a/src/rootSaga.js +++ b/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)) + ) } \ No newline at end of file diff --git a/src/userReducer.js b/src/userReducer.js new file mode 100644 index 0000000..17b893d --- /dev/null +++ b/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 diff --git a/src/userSaga.js b/src/userSaga.js new file mode 100644 index 0000000..9fd562c --- /dev/null +++ b/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;