Skip to content

Commit

Permalink
Merge pull request #3 from Schwartz10/postPhotos
Browse files Browse the repository at this point in the history
Post photos
  • Loading branch information
Schwartz10 authored Jan 19, 2018
2 parents fde1b95 + f7f9f0f commit c2f0be1
Show file tree
Hide file tree
Showing 13 changed files with 1,696 additions and 316 deletions.
1,789 changes: 1,481 additions & 308 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-18T21:50:57.978Z"
"updatedAt": "2018-01-19T04:15:13.232Z"
}
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-18T21:50:57.975Z"
"updatedAt": "2018-01-19T04:15:13.219Z"
}
28 changes: 26 additions & 2 deletions contracts/CapCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ contract CapCoin {

event NewUser(string name);
event BoughtTokens(uint coinBalance);
event CreatedPost(string url);

uint coinSupply = 1000000;
uint coinsBought = 0;
Expand All @@ -16,10 +17,18 @@ contract CapCoin {
struct User {
string name;
uint coinBalance;
uint postNum;
}

struct Post {
string url;
uint lotteryAmount;
}

mapping (address => User) public addressToUser;
mapping (address => Post) public addressToPost;
address[] public users;
address[] public posts;

function createUser(string name) public {
var user = addressToUser[msg.sender];
Expand All @@ -35,8 +44,11 @@ contract CapCoin {
return users;
}

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

function buyTokens(uint amount) public {
Expand All @@ -45,4 +57,16 @@ contract CapCoin {
coinSupply -= amount;
BoughtTokens(addressToUser[msg.sender].coinBalance);
}

function createPost(string url) public {
// 5 coins per post
var post = addressToPost[msg.sender];
post.url = url;
post.lotteryAmount = 5;
posts.push(msg.sender) -1;
addressToUser[msg.sender].coinBalance -= 5;
CreatedPost(url);
}

function() internal payable {}
}
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@
c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3

http://127.0.0.1:7545


81 changes: 81 additions & 0 deletions src/components/CreatePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { Component } from 'react'
import {connect} from 'react-redux'
import { Link } from 'react-router-dom';
import TextField from 'material-ui/TextField';
import Post from './Post';
import RaisedButton from 'material-ui/RaisedButton';
import { post } from '../store/posts';

class CreatePost extends Component {
constructor(props){
super(props);
this.state = { canPost: this.props.user.coinBalance >= 5,
postUrl: "" }
}

handleChange = (event) => {
this.setState({
postUrl: event.target.value,
});
};

render(){
return(
<div>
{this.props.user ?
<div>
<h1>Create Your Post {this.props.user.name}</h1>
<h3>You currently have {this.props.user.coinBalance} Tokens <br /></h3>
<TextField
value={this.props.postUrl}
hintText={!this.state.postUrl.length && "Enter Post URL"}
onChange={this.handleChange}
/><br />
{ this.state.postUrl.length &&
<div>
<h3>Post Preview:</h3>
<Post
username={this.props.user.name}
tokenPot={5}
postUrl={this.state.postUrl}
isPreview={true}
/>
<RaisedButton
onClick={e =>
this.props.post(e, this.state.postUrl, this.props.contract.createPost, this.props.accounts[0])}
label="Create Post" primary={true}
/>
</div>
}
</div>
:
<div>
<h1>Create your account first!</h1>
<Link to='/profile'>Create Accout</Link>
</div>
}
</div> )
}
}

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

const mapDispatch = (dispatch) => {
return {
post: function(e, postUrl, contractFunc, account){
e.preventDefault();
return dispatch(post(postUrl, contractFunc, account));
}
}
}

export default connect(mapState, mapDispatch)(CreatePost)
3 changes: 2 additions & 1 deletion src/components/Drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class DrawerNav extends React.Component {
return (
<div>
<RaisedButton
label="Open Drawer"
label="Menu"
onClick={this.handleToggle}
/>
<Drawer
Expand All @@ -29,6 +29,7 @@ export default class DrawerNav extends React.Component {
onRequestChange={(open) => this.setState({open})}
>
<Link to="/explore"><MenuItem onClick={this.handleClose}>Explore</MenuItem></Link>
<Link to="/create-post"><MenuItem onClick={this.handleClose}>Create Post</MenuItem></Link>
<Link to="/profile"><MenuItem onClick={this.handleClose}>My Profile</MenuItem></Link>
<Link to="/exchange"><MenuItem onClick={this.handleClose}>Buy CapCoins</MenuItem></Link>
</Drawer>
Expand Down
17 changes: 17 additions & 0 deletions src/components/Explore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, {Component} from 'react'
import Post from './Post'

export default class Explore extends Component {

constructor(props) {
super(props);
}

render(){
return(
<div>
<h1>Explore</h1>
</div>
)
}
}
31 changes: 31 additions & 0 deletions src/components/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, {Component} from 'react';
import {connect} from 'react-redux'
import {Card, CardActions, CardHeader, CardMedia, CardTitle } from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';

const cardStyle = {
width: '30vw'
}

const Post = props => (
<Card style={cardStyle}>
<CardHeader
title={props.username}
/>
<CardMedia
overlay={<CardTitle title={'Token pot: ' + props.tokenPot} />}
>
<img src={props.postUrl} alt="" />
</CardMedia>
{!props.isPreview &&
<div>
<CardTitle title="Leave a Caption"/>
<CardActions>
<FlatButton label="Submit" />
</CardActions>
</div>
}
</Card>
)

export default Post;
10 changes: 10 additions & 0 deletions src/components/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Account from './Account';
import Drawer from './Drawer';
import history from '../history';
import Exchange from './Exchange';
import Explore from './Explore';
import CreatePost from './CreatePost';
import RaisedButton from 'material-ui/RaisedButton';
import { Link } from 'react-router-dom';

export default class Routes extends Component {
componentDidMount () {}
Expand All @@ -12,10 +16,16 @@ export default class Routes extends Component {
return (
<Router history={history}>
<div>
<span>
<Drawer />
<Link to="/" ><RaisedButton label="home" /> </Link>
</span>
<Switch>
<Route exact path='/profile' component={Account} />
<Route exact path='/exchange' component={Exchange} />
<Route exact path='/explore' component={Explore} />
<Route exact path='/create-post' component={CreatePost} />
<Route exact path='/' component={Explore} />
</Switch>
</div>
</Router>
Expand Down
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import user from './user'
import web3 from './web3'
import contract from './contract'
import accounts from './accounts'
import posts from './posts'


export const reducer = combineReducers({ user, web3, contract, accounts })
export const reducer = combineReducers({ user, web3, contract, accounts, posts })
const middleware = composeWithDevTools(applyMiddleware(
thunkMiddleware,
createLogger({collapsed: true})
Expand Down
42 changes: 42 additions & 0 deletions src/store/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* INITIAL STATE
*/
const defaultPosts = []

/**
* ACTION TYPES
*/
const CREATE_POST = 'CREATE_POST';

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

/**
* THUNK CREATORS
*/

export const post = (url, contractFunc, account) =>
dispatch =>
contractFunc(url, {from: account})
.then(res => {
let newPost = {}
newPost[account] = res.logs[0].args.url;
return dispatch(createPost(newPost));
})
.catch(err => console.log(err));

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

0 comments on commit c2f0be1

Please sign in to comment.