Skip to content

Commit

Permalink
Create account if member doesn't exists in blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragsinghbisht authored and nrchandan committed May 10, 2017
1 parent 412622c commit c6f3768
Show file tree
Hide file tree
Showing 32 changed files with 761 additions and 306 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"parser": "babel-eslint",
"extends": [
"standard"
"standard",
"eslint:recommended",
"plugin:react/recommended"
],
"plugins": [
"babel"
"babel",
"react"
],
"rules": {
"key-spacing" : 0,
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.DS_STORE
.idea
app/config.js
build
coverage
dist/**
installed_contracts
node_modules
npm-debug.log
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TBD
## Building and the frontend

1. First run `truffle compile`, then run `truffle migrate` to deploy the contracts onto your network of choice (default "development").
1. Then run `npm run dev` to build the app and serve it on http://localhost:8080
1. Then run `npm start` to build the app and serve it on http://localhost:8080


## Debugging
Expand Down
14 changes: 13 additions & 1 deletion app/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ const actionType = {
SEARCH_BOOK : 'SEARCH_BOOK',
RATE_BOOK_LOADING : 'RATE_BOOK_LOADING',
RATE_BOOK_ERROR : 'RATE_BOOK_ERROR',
RATE_BOOK_SUCCESS : 'RATE_BOOK_SUCCESS'
RATE_BOOK_SUCCESS : 'RATE_BOOK_SUCCESS',
GET_MEMBER_DETAILS_EMAIL_LOADING : 'GET_MEMBER_DETAILS_EMAIL_LOADING',
GET_MEMBER_DETAILS_EMAIL_ERROR : 'GET_MEMBER_DETAILS_EMAIL_ERROR',
GET_MEMBER_DETAILS_EMAIL_SUCCESS : 'GET_MEMBER_DETAILS_EMAIL_SUCCESS',
GET_RATE_BOOK_LOADING : 'GET_RATE_BOOK_LOADING',
GET_RATE_BOOK_ERROR : 'GET_RATE_BOOK_ERROR',
GET_RATE_BOOK_SUCCESS : 'GET_RATE_BOOK_SUCCESS',
CREATE_ACCOUNT_LOADING : 'CREATE_ACCOUNT_LOADING',
CREATE_ACCOUNT_SUCCESS : 'CREATE_ACCOUNT_SUCCESS',
CREATE_ACCOUNT_ERROR : 'CREATE_ACCOUNT_ERROR',
ADD_MEMBER_LOADING : 'ADD_MEMBER_LOADING',
ADD_MEMBER_ERROR : 'ADD_MEMBER_ERROR',
ADD_MEMBER_SUCCESS : 'ADD_MEMBER_SUCCESS'
}

export default actionType
116 changes: 102 additions & 14 deletions app/actions/libraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import contractConfig from '../config'
import { web as web3, lms as LMS } from '../web3'
import actionType from './actionTypes'
import { sessionService } from 'redux-react-session'
import axios from 'axios'

export const action = (type, flag) => {
return {
Expand Down Expand Up @@ -45,6 +46,7 @@ export const getAllBooks = () => {
LMS.at(contractConfig.id).then((instance) => {
return instance.getAllBooks.call()
}).then((books) => {
getRatings(dispatch)
dispatch(action(actionType.GET_ALL_BOOKS_SUCCESS, books))
dispatch(action(actionType.GET_ALL_BOOKS_LOADING, false))
}).catch((e) => {
Expand All @@ -69,24 +71,24 @@ export const getMyBooks = () => {
}
}

export const addBook = (title, author, publisher, imageUrl, description, genre) => {
export const addBook = (book) => {
return (dispatch) => {
dispatch(action(actionType.GET_ADD_BOOKS_LOADING, true))
LMS.at(contractConfig.id).then((instance) => {
return instance.addBook(
title,
author,
publisher,
imageUrl,
description,
genre,
book.title,
book.author,
book.publisher,
book.imageUrl,
book.description,
book.genre,
{
from: web3.eth.accounts[0],
from: book.owner.account,
gas: 600000
}
)
}).then((response) => {
dispatch(getMyBooks())
dispatch(getAllBooks())
dispatch(action(actionType.GET_ADD_BOOKS_SUCCESS, true))
dispatch(action(actionType.GET_ADD_BOOKS_LOADING, false))
}).catch((e) => {
Expand All @@ -102,7 +104,7 @@ export const returnBook = (book) => {
LMS.at(contractConfig.id).then((instance) => {
return instance.returnBook(book.id, { from : book.owner, gas: 200000 })
}).then((response) => {
dispatch(getMyBooks())
dispatch(getAllBooks())
dispatch(action(actionType.GET_RETURN_BOOKS_SUCCESS, true))
dispatch(action(actionType.GET_RETURN_BOOKS_LOADING, false))
}).catch((e) => {
Expand All @@ -118,7 +120,7 @@ export const borrowBook = (book, ownerDetails) => {
LMS.at(contractConfig.id).then((instance) => {
return instance.borrowBook(book.id, { from: ownerDetails.account, value: web3.toWei(0.1), gas: 200000 })
}).then((response) => {
dispatch(getMyBooks())
dispatch(getAllBooks())
dispatch(action(actionType.GET_BORROW_BOOKS_SUCCESS, true))
dispatch(action(actionType.GET_BORROW_BOOKS_LOADING, false))
}).catch((e) => {
Expand All @@ -136,7 +138,7 @@ export const rateBook = (rating, comment, book, ownerDetails) => {
return (dispatch) => {
dispatch(action(actionType.RATE_BOOK_LOADING, true))
LMS.at(contractConfig.id).then((instance) => {
return instance.rateBook(book.id, rating, comment, {
return instance.rateBook(book.id, rating, comment,'0', {
from: ownerDetails.account,
gas: 300000
})
Expand All @@ -151,13 +153,14 @@ export const rateBook = (rating, comment, book, ownerDetails) => {
}

export const login = (response, userVal) => {
sessionService.saveSession(response.accessToken)
sessionService.saveSession(response)
.then(() => {
const user = {
'name' : userVal[0],
'account' : userVal[1],
'status' : userVal[2],
'dateAdded' : userVal[3]
'dateAdded' : userVal[3],
'body': response
}
sessionService.saveUser(user)
}).catch(err => console.error(err))
Expand All @@ -169,3 +172,88 @@ export const logout = () => {
sessionService.deleteUser()
}
}

export const getMemberDetailsByEmail = (response) => {
return (dispatch) => {
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_LOADING, true))
LMS.at(contractConfig.id).then((instance) => {
return instance.getMemberDetailsByEmail(response.profileObj.email)
}).then((user) => {
login(response, user)
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_SUCCESS, user))
}).catch((e) => {
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_ERROR, e))
}).then(() => {
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_LOADING, false))
})
}
}

export const getRatings = (dispatch) => {
dispatch(action(actionType.GET_RATE_BOOK_LOADING, true))
LMS.at(contractConfig.id).then((instance) => {
return instance.Rate({}, {
fromBlock: 0,
toBlock: 'latest'
})
}).then((rateEvent) => {
rateEvent.watch(function(err, result) {
rateEvent.stopWatching();
if (err) {
dispatch(action(actionType.GET_RATE_BOOK_ERROR, err))
} else {
dispatch(action(actionType.GET_RATE_BOOK_SUCCESS, result.args))
}
dispatch(action(actionType.GET_RATE_BOOK_LOADING, false))
});
})
}

export const createAccount = (session,password) => {
return (dispatch) => {
dispatch(action(actionType.CREATE_ACCOUNT_LOADING, true))
const request = {
"jsonrpc":"2.0",
"method":"personal_newAccount",
"params":[password],
"id":74
}
return axios.post('/api/create_account',request)
.then((response) => {
addMember([
session.user.body.profileObj.name,
response.data.data.result,
session.user.body.profileObj.email,
password,
], dispatch, session.user.body)
})
.catch((error) => {
dispatch(action(actionType.CREATE_ACCOUNT_ERROR, error))
}).then(() => {
dispatch(action(actionType.CREATE_ACCOUNT_LOADING, false))
});
};
}

export const addMember = (member, dispatch, session) => {
dispatch(action(actionType.ADD_MEMBER_LOADING, true))
if(web3.personal.unlockAccount(member[1],member[3],0)) {
LMS.at(contractConfig.id).then((instance) => {
return instance.addMember(member[0], member[1], member[2], member[3], {
from: web3.eth.accounts[0],
gas: 600000
})
}).then((response) => {
web3.eth.sendTransaction({
from: web3.eth.accounts[0],
to: member[1],
value: web3.toWei(0.1)
})
login(session, member)
}).catch((err) => {
dispatch(action(actionType.ADD_MEMBER_ERROR, err))
}).then(() => {
dispatch(action(actionType.ADD_MEMBER_LOADING, false))
})
}
}
42 changes: 16 additions & 26 deletions app/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,34 @@ import Header from './Header'

const mapStateToProps = (state, ownProps) => {
return {
ownerDetails : state.ownerDetails,
loading: state.loading
}
}

const mapDispatchToProps = (dispatch) => {
return {
getOwnerDetails: () => {
dispatch(libraryActions.getOwnerDetails())
}
session: state.session
}
}

export class App extends React.Component {
constructor (props) {
super(props)
}

componentDidMount () {
if (!this.props.ownerDetails) {
this.props.getOwnerDetails()
}
}

render () {
const ownerDetails = this.props.loading.ownerDetailsLoading ? '' : this.props.ownerDetails
return (
<div>
<Header ownerDetails={ownerDetails} />
<Header
loginSuccess = {
(response) => this.props.getMemberDetailsByEmail(response)
}
loginFailure = {
(response) => { console.log(response) }
}
session={ this.props.session }
logout = {
() => this.props.logout()
} />
<div className='container'>
{
ownerDetails
? <Dashboard owner={ownerDetails}/>
: <div>Loading...</div>
this.props.session.authenticated
? <Dashboard owner={this.props.session.user}/>
: <div>Logged out</div>
}
</div>
</div>)
}
}

export default connect(mapStateToProps, mapDispatchToProps)(App)
export default connect(mapStateToProps, libraryActions)(App)
26 changes: 26 additions & 0 deletions app/components/Banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'

const Banner = () => (
<div className='row yogalogo'>
<div className='col-sm-6'>
<div className='yoga-wrapper'>
<img width = '1016'
height = '590'
src = 'http://www.pramati.com/wp-content/uploads/2017/03/Triad-01.png'
className = 'vc_single_image-img attachment-large'
alt = ''
sizes = '(max-width: 1016px) 100vw, 1016px' />
</div>
</div>
<div className='col-sm-6'>
<div className='yoga-header'>
<h1>Yoga for the mind</h1>
</div>
<div className='yoga-subheader'>
<h4>Get Rewarded for Reading</h4>
</div>
</div>
</div>
)

export default Banner
Loading

0 comments on commit c6f3768

Please sign in to comment.