Skip to content

Commit

Permalink
Merge pull request #8 from Schwartz10/likingPhoto
Browse files Browse the repository at this point in the history
Liking photo
  • Loading branch information
Schwartz10 authored Jan 20, 2018
2 parents 167672f + 203177c commit 77858a6
Show file tree
Hide file tree
Showing 13 changed files with 2,074 additions and 1,239 deletions.
3,142 changes: 1,934 additions & 1,208 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-19T16:26:08.222Z"
"updatedAt": "2018-01-19T23:48:42.207Z"
}
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-19T16:26:08.220Z"
"updatedAt": "2018-01-19T23:48:42.206Z"
}
32 changes: 16 additions & 16 deletions contracts/CapCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,36 @@ contract CapCoin {
event NewUser(string name);
event BoughtTokens(uint coinBalance);
event CreatedPost(string url);
event LikedPost(uint lotteryAmount, string posterName,
address poster, uint posterCoinbalance, string likerName,
address liker, uint likerCoinbalance);

uint coinSupply = 1000000;
uint coinsBought = 0;
uint contractNum;

function CapCoin () {
contractNum = 1;
}

struct User {
string name;
uint coinBalance;
uint postNum;
}

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

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];

user.name = name;
user.coinBalance = 0;

users.push(msg.sender) -1;
NewUser(name);
}

function getUsers() public view returns (address[]) {
return users;
}

function getUser() public view returns (string, uint, string, uint, address) {
return (addressToUser[msg.sender].name,
addressToUser[msg.sender].coinBalance,
Expand All @@ -54,6 +43,7 @@ contract CapCoin {
msg.sender);
}

// need to add economics
function buyTokens(uint amount) public {
addressToUser[msg.sender].coinBalance += amount;
coinsBought += amount;
Expand All @@ -65,14 +55,24 @@ contract CapCoin {
// 5 coins per post
var post = addressToPost[msg.sender];
post.url = url;
post.lotteryAmount = 5;
post.lotteryAmount -= 5;
post.userName = addressToUser[msg.sender].name;
post.owner = msg.sender;

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

function likePost(address post, uint amount) public {
addressToPost[post].lotteryAmount += amount;
addressToUser[post].coinBalance += amount;
addressToUser[msg.sender].coinBalance -= amount;
LikedPost(addressToPost[post].lotteryAmount,
addressToUser[post].name, post, addressToUser[post].coinBalance,
addressToUser[msg.sender].name, msg.sender, addressToUser[msg.sender].coinBalance);
}

function getPosts() public view returns (address[]) {
return posts;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/CreatePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CreatePost extends Component {
{this.props.user ?
<div>
<h1>Create Your Post {this.props.user.name}</h1>
<h3>You currently have {this.props.user.coinBalance} Tokens <br /></h3>
<h3>You currently have {this.props.user.coinBalance} Coins <br /></h3>
<TextField
value={this.props.postUrl}
hintText={!this.state.postUrl.length && "Enter Post URL"}
Expand All @@ -36,7 +36,7 @@ class CreatePost extends Component {
<h3>Post Preview:</h3>
<Post
username={this.props.user.name}
tokenPot={5}
tokenPot={-5}
postUrl={this.state.postUrl}
isPreview={true}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class DrawerNav extends React.Component {
<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>
<Link to="/exchange"><MenuItem onClick={this.handleClose}>Buy InstaCoins</MenuItem></Link>
</Drawer>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Exchange extends Component {
render() {
return (
<div>
<h1>Buy Tokens</h1>
<h1>Buy InstaCoins</h1>
<SelectField
floatingLabelText="Coin Amount"
value={this.state.value}
Expand All @@ -35,6 +35,7 @@ class Exchange extends Component {
this.props.buyTokens(e, this.state.value, this.props.contract.buyTokens, this.props.accounts[0])}
label="Buy Tokens" primary={true}
/>
<h3>You have {this.props.user.coinBalance} Coins</h3>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/Explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Explore extends Component {
username={post.username}
tokenPot={post.tokenPot}
postUrl={post.postUrl}
address={post.address}
isPreview={false}
/>
<br />
Expand Down
41 changes: 41 additions & 0 deletions src/components/LikePhoto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { Component } from 'react'
import {connect} from 'react-redux'
import RaisedButton from 'material-ui/RaisedButton';
import { likePost } from '../store/posts';

class LikePhoto extends Component {
constructor(props){
super(props);
}

render(){
return(
<div>
<RaisedButton label="like" onClick={e => this.props.like(e, this.props.postUrl, this.props.postAddress, this.props.contract.likePost, this.props.accounts[0])}/>
</div>
)
}
}

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

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

export default connect(mapState, mapDispatch)(LikePhoto)
14 changes: 8 additions & 6 deletions src/components/Post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {Component} from 'react';
import {connect} from 'react-redux'
import React from 'react';
import {Card, CardActions, CardHeader, CardMedia, CardTitle } from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';
import LikePhoto from './LikePhoto';

const cardStyle = {
width: '30vw'
Expand All @@ -13,15 +12,18 @@ const Post = props => (
title={props.username}
/>
<CardMedia
overlay={<CardTitle title={'Token pot: ' + props.tokenPot} />}
overlay={<CardTitle title={'Total InstaCoins: ' + props.tokenPot} />}
>
<img src={props.postUrl} alt="" />
</CardMedia>
{!props.isPreview &&
<div>
<CardTitle title="Leave a Caption"/>
<CardTitle title="Like this pic?"/>
<CardActions>
<FlatButton label="Submit" />
<LikePhoto
postUrl={props.postUrl}
postAddress={props.address}
/>
</CardActions>
</div>
}
Expand Down
23 changes: 22 additions & 1 deletion src/store/posts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import likedPostHelper from './../utils/likedPost';

/**
* INITIAL STATE
*/
Expand All @@ -8,12 +10,14 @@ const defaultPosts = []
*/
const CREATE_POST = 'CREATE_POST';
const GET_POSTS = 'GET_POSTS';
const LIKED_POST = 'LIKED_POST';

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

/**
* THUNK CREATORS
Expand All @@ -24,7 +28,9 @@ export const post = (url, contractFunc, account) =>
contractFunc(url, {from: account})
.then(res => {
let newPost = {}
newPost[account] = res.logs[0].args.url;
newPost.postUrl = res.logs[0].args.url;
newPost.account = account;
newPost.tokenPot = -5
return dispatch(createPost(newPost));
})
.catch(err => console.log(err));
Expand All @@ -42,12 +48,25 @@ export const fetchPosts = (fetchAddressArray, addressToPostFunc) =>
completedPost.postUrl = post[0];
completedPost.username = post[1];
completedPost.tokenPot = post[2].toString(10);
completedPost.address = post[3];
return completedPost;
})
dispatch(gotPosts(finalArr))
})
.catch(err => console.log(err))

// in process
export const likePost = (postUrl, postAddress, contractFunc, account) =>
dispatch =>
contractFunc(postAddress, 10, {from: account})
.then(res => {
let postInfo = Object.assign({}, res.logs[0].args);
postInfo.likerCoinbalance = Number(postInfo.likerCoinbalance.toString(10));
postInfo.posterCoinbalance = Number(postInfo.posterCoinbalance.toString(10));
postInfo.lotteryAmount = Number(postInfo.lotteryAmount.toString(10));
return dispatch(likedPost(postInfo))
})

/**
* REDUCER
*/
Expand All @@ -59,6 +78,8 @@ export default function (state = defaultPosts, action) {
return posts;
case GET_POSTS:
return action.posts;
case LIKED_POST:
return likedPostHelper.updatePost(state, action.post);
default:
return state
}
Expand Down
22 changes: 20 additions & 2 deletions src/store/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import likedPostHelper from './../utils/likedPost';

/**
* INITIAL STATE
*/
Expand All @@ -9,6 +11,8 @@ const defaultUser = {}
const GET_USER = 'GET_USER';
const CREATE_USER = 'CREATE_USER';
const BUY_TOKENS = 'BUY_TOKENS';
const LIKED_POST = 'LIKED_POST';
const CREATE_POST = 'CREATE_POST';

/**
* ACTION CREATORS
Expand Down Expand Up @@ -37,7 +41,14 @@ export const fetchUser = (contractFunc, account) =>
export const addUser = (name, contractFunc, account) =>
dispatch =>
contractFunc(name, {from: account})
.then(res => dispatch(createUser(res.logs[0].args)))
.then(res => {
let newUser = Object.assign({}, res.logs[0].args);
newUser.coinBalance = 0;
newUser.postUrl = "";
newUser.postLottery = "";
newUser.address = account;
dispatch(createUser(newUser));
})
.catch(err => console.log(err));

export const buyTokens = (amount, contractFunc, account) =>
Expand All @@ -56,7 +67,14 @@ export default function (state = defaultUser, action) {
case CREATE_USER:
return action.user;
case BUY_TOKENS:
return Object.assign({}, state, {coinBalance: action.amount})
return Object.assign({}, state, {coinBalance: action.amount});
case LIKED_POST:
return likedPostHelper.updateUser(state, action.post);
case CREATE_POST:
let updatedUser = Object.assign({}, state)
updatedUser.postUrl = action.post.postUrl;
updatedUser.postLottery = action.post.tokenPot;
return updatedUser;
default:
return state
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/likedPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const likedPostHelper = {}

likedPostHelper.updateUser = function(state, newPostInfo) {
let newState = Object.assign({}, state);

if (newState.address === newPostInfo.liker) {
newState.coinBalance = newPostInfo.likerCoinbalance
}

return newState;
}

likedPostHelper.updatePost = function(state, newPostInfo) {
let newState = state.slice();

for (let i = 0; i < newState.length; i++) {
if (newState[i].address === newPostInfo.poster){
newState[i].tokenPot = newPostInfo.lotteryAmount;
}
}

return newState;
}

module.exports = likedPostHelper;

0 comments on commit 77858a6

Please sign in to comment.