Skip to content

Commit

Permalink
Merge pull request #7 from Schwartz10/explore
Browse files Browse the repository at this point in the history
Explore
  • Loading branch information
Schwartz10 authored Jan 19, 2018
2 parents c2f0be1 + 277975d commit 167672f
Show file tree
Hide file tree
Showing 9 changed files with 1,279 additions and 491 deletions.
1,666 changes: 1,186 additions & 480 deletions build/contracts/CapCoin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/contracts/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,5 @@
}
},
"schemaVersion": "1.0.1",
"updatedAt": "2018-01-19T04:15:13.232Z"
"updatedAt": "2018-01-19T16:26:08.222Z"
}
2 changes: 1 addition & 1 deletion build/contracts/SimpleStorage.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,5 @@
}
},
"schemaVersion": "1.0.1",
"updatedAt": "2018-01-19T04:15:13.219Z"
"updatedAt": "2018-01-19T16:26:08.220Z"
}
19 changes: 17 additions & 2 deletions contracts/CapCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ contract CapCoin {

struct Post {
string url;
string userName;
uint lotteryAmount;
string[] captions;
}

mapping (address => User) public addressToUser;
Expand All @@ -44,11 +46,12 @@ contract CapCoin {
return users;
}

function getUser() public view returns (string, uint, string, uint) {
function getUser() public view returns (string, uint, string, uint, address) {
return (addressToUser[msg.sender].name,
addressToUser[msg.sender].coinBalance,
addressToPost[msg.sender].url,
addressToPost[msg.sender].lotteryAmount);
addressToPost[msg.sender].lotteryAmount,
msg.sender);
}

function buyTokens(uint amount) public {
Expand All @@ -63,10 +66,22 @@ contract CapCoin {
var post = addressToPost[msg.sender];
post.url = url;
post.lotteryAmount = 5;
post.userName = addressToUser[msg.sender].name;

posts.push(msg.sender) -1;
addressToUser[msg.sender].coinBalance -= 5;
CreatedPost(url);
}

function getPosts() public view returns (address[]) {
return posts;
}

function seed(string name, string url, uint amount) public {
createUser(name);
buyTokens(amount);
createPost(url);
}

function() internal payable {}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@


c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3

ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
http://127.0.0.1:7545
11 changes: 7 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class App extends Component {

componentWillMount() {
this.collectBlockchainInfo()
.then(() => {
this.props.findUser(this.props.contract.getUser, this.props.accounts[0])
})
}

async collectBlockchainInfo() {
// Get network provider, web3, and truffle contract instance and store them on state.
await this.props.getWeb3()
await this.props.getContract(this.props.web3)
await this.props.getAccounts(this.props.web3)
await this.props.findUser(this.props.contract.getUser, this.props.accounts[0])
return Promise.all([await this.props.getWeb3(),
this.props.getContract(this.props.web3),
this.props.getAccounts(this.props.web3)])
}


render() {
return (
<div className="App">
Expand Down
42 changes: 41 additions & 1 deletion src/components/Explore.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
import React, {Component} from 'react'
import Post from './Post'
import {connect} from 'react-redux'
import { fetchPosts } from '../store/posts'

export default class Explore extends Component {
class Explore extends Component {

constructor(props) {
super(props);
}

componentWillReceiveProps(nextProps){
if (this.props.contract.getPosts && this.props.posts.length === 0) {
this.props.getPosts(this.props.contract.getPosts,
this.props.contract.addressToPost);
}
}

render(){
return(
<div>
<h1>Explore</h1>
{this.props.posts.length > 0 &&
this.props.posts.map(post =>
<div key={post.username}>
<Post
username={post.username}
tokenPot={post.tokenPot}
postUrl={post.postUrl}
isPreview={false}
/>
<br />
</div>
)}
</div>
)
}
}

const mapState = (state) => {
return {
user: state.user,
contract: state.contract,
accounts: state.accounts,
posts: state.posts
}
}

const mapDispatch = (dispatch) => {
return {
getPosts: function(contractFunc, addressToPost){
return dispatch(fetchPosts(contractFunc, addressToPost));
}
}
}

export default connect(mapState, mapDispatch)(Explore)
25 changes: 24 additions & 1 deletion src/store/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const defaultPosts = []
* ACTION TYPES
*/
const CREATE_POST = 'CREATE_POST';
const GET_POSTS = 'GET_POSTS';

/**
* ACTION CREATORS
*/
const createPost = post => ({type: CREATE_POST, post})
const createPost = post => ({type: CREATE_POST, post});
const gotPosts = posts => ({type: GET_POSTS, posts});

/**
* THUNK CREATORS
Expand All @@ -27,6 +29,25 @@ export const post = (url, contractFunc, account) =>
})
.catch(err => console.log(err));

export const fetchPosts = (fetchAddressArray, addressToPostFunc) =>
dispatch =>
fetchAddressArray.call()
.then(res => {
let addresses = res.map(address => addressToPostFunc(address))
return Promise.all(addresses)
})
.then(posts => {
let finalArr = posts.map(post => {
let completedPost = {}
completedPost.postUrl = post[0];
completedPost.username = post[1];
completedPost.tokenPot = post[2].toString(10);
return completedPost;
})
dispatch(gotPosts(finalArr))
})
.catch(err => console.log(err))

/**
* REDUCER
*/
Expand All @@ -36,6 +57,8 @@ export default function (state = defaultPosts, action) {
let posts = state.slice()
posts.push(action.post)
return posts;
case GET_POSTS:
return action.posts;
default:
return state
}
Expand Down
1 change: 1 addition & 0 deletions src/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const fetchUser = (contractFunc, account) =>
user.coinBalance = Number(res[1].toString(10));
user.postUrl = res[2];
user.postLottery = Number(res[3].toString(10));
user.address = res[4];
return dispatch(getUser(user))
})
.catch(err => console.log(err))
Expand Down

0 comments on commit 167672f

Please sign in to comment.