mirror of https://gitlab.com/ecentrics/concordia
Ezerous
7 years ago
8 changed files with 117 additions and 12 deletions
@ -1,10 +1,12 @@ |
|||||
import { combineReducers } from 'redux' |
import { combineReducers } from 'redux' |
||||
import { routerReducer } from 'react-router-redux' |
import { routerReducer } from 'react-router-redux' |
||||
import { drizzleReducers } from 'drizzle' |
import { drizzleReducers } from 'drizzle' |
||||
|
import userReducer from "./userReducer"; |
||||
|
|
||||
const reducer = combineReducers({ |
const reducer = combineReducers({ |
||||
routing: routerReducer, |
routing: routerReducer, |
||||
...drizzleReducers |
user: userReducer, |
||||
|
...drizzleReducers |
||||
}); |
}); |
||||
|
|
||||
export default reducer |
export default reducer |
||||
|
@ -1,8 +1,10 @@ |
|||||
import { all, fork } from 'redux-saga/effects' |
import { all, fork } from 'redux-saga/effects' |
||||
import { drizzleSagas } from 'drizzle' |
import { drizzleSagas } from 'drizzle' |
||||
|
import userSaga from "./userSaga"; |
||||
|
|
||||
export default function* root() { |
export default function* root() { |
||||
yield all( |
let sagas = [...drizzleSagas,userSaga]; |
||||
drizzleSagas.map(saga => fork(saga)) |
yield all( |
||||
) |
sagas.map(saga => fork(saga)) |
||||
|
) |
||||
} |
} |
@ -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 |
@ -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…
Reference in new issue