Skip to content

Commit

Permalink
changed schema for well-rounded front-end render logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonzthu committed Apr 26, 2017
1 parent 1ae6f7d commit 401683f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
59 changes: 41 additions & 18 deletions client/src/components/Auction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Auction extends Component {
flag: false
};
this.setBid = this.setBid.bind(this);
this.handleClick = this.handleClick.bind(this);
}

componentWillMount() {
Expand Down Expand Up @@ -111,29 +112,52 @@ class Auction extends Component {
dispatch(Bids.setBid(bid));
}

handleClick(id) {
handleClick(auctionId, avail, buyout) {
const { bid, user, history, dispatch } = this.props;
//Alison's new:
//this is for invalid input:
if (!bid.bid){
alert('Please enter a valid value');
} else if(bid.bid < avail) {
alert('Please at least bid for the next available amount');
} else if(bid.bid > buyout) {
alert('Do you want to bid for the buyout amount?');
if(!user.username) {
alert('You are not logged in, please sign up or log in');
history.push('/login');
} else {
//can send the request here now~~~
if (!bid.bid){
alert('Please enter a valid value');
} else if(bid.bid < avail) {
alert('Please at least bid for the next available amount');
} else if(bid.bid > buyout) {
alert('Do you want to bid for the buyout amount?');
} else {
//can send the request here now~~~
//send
fetch('/auctions/' + auctionId + '/bids', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': `Bearer ${sessionStorage.getItem('authToken')}`
}),
body: JSON.stringify({
bidPrice: bid.bid
})
})
.then(response => {
if (!response.ok) {
throw Error(response.json());
}
return response.json();
})
.then(data => {
bid.current_bid_id = user.userId;
bid.current_bid = bid.bid;
dispatch(Auctions.updateBid(bid));
alert(`You have successfully bid $${bid.bid}`);
})
.catch(err => {
dispatch(Bids.error(err));
});
}
}

//old:
// if (bid.bid === 0) {
// alert('Please select a value');
// } else {
// //if user not logged in, redirect
// if(!user.username) {
// alert('you are not logged in, please sign up or log in');
// history.push('/login');
// } else {
// //grab userid, artwork_id and value
// dispatch(Bids.toggleSend());
// fetch(`/auctions/${id}/bids`, {
Expand All @@ -159,7 +183,6 @@ class Auction extends Component {
// .catch((err) => {
// dispatch(Bids.error(err));
// });
// }
// }
}

Expand All @@ -183,7 +206,7 @@ class Auction extends Component {
} else {
return (
<div>
<AuctionDetail flag={this.state.flag} user={user} handleClick={this.handleClick.bind(this, auction.id)} auction={auction} setBid={this.setBid} handleSave={() => {
<AuctionDetail flag={this.state.flag} user={user} handleClick={this.handleClick} auction={auction} setBid={this.setBid} handleSave={() => {
this.handleSave(auction.id)
}} handleUnsave={() => {
this.handleUnsave(auction.id)
Expand Down
23 changes: 9 additions & 14 deletions client/src/components/AuctionDetail.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { Container, Image, Grid, Button, Form } from 'semantic-ui-react';
import { Container, Image, Grid, Button, Form, Input } from 'semantic-ui-react';
import Moment from 'moment';
import BiddingRange from './BiddingRange.jsx';

const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUnsave, flag}) => {

Expand All @@ -11,14 +10,8 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn
let buyout = +auction.buyout_price;

let interval = 0;
if (buyout < 100) {
interval = 10;
} else if (buyout < 500) {
interval = 50;
} else if (buyout < 1000) {
interval = 100;
} else if (buyout < 5000) {
interval = 500;
if (buyout < 5000) {
interval = parseInt(0.1 * buyout);
} else {
interval = 1000;
}
Expand All @@ -42,8 +35,8 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn
<h3>{auction.first_name} {auction.last_name} ({auction.artwork.age})</h3>
<p><strong>Auction Ends:</strong> {endTime}</p>
<p><strong>Description:</strong> {auction.artwork.description}</p>
<p><strong>Current Price (USD):</strong> ${(auction.current_bid || auction.start_price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</p>
<p><strong>Buyout Price (USD):</strong> ${(auction.buyout_price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</p>
<p><strong>Current Price (USD):</strong> ${(current || start).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</p>
<p><strong>Buyout Price (USD):</strong> ${buyout.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</p>
<Form.Group widths='equal'>
<span>
<p>
Expand All @@ -54,15 +47,17 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn
<strong>
Bid for:
</strong>
<input onChange={e => {
<Input onChange={e => {
if(isNaN(e.target.value)) {
e.target.value = '';
} else {
setBid(e.target.value);
}
}} />
</span>
<Button className="ui right floated" color="green" onClick={handleClick}>Submit</Button>
<Button className="ui right floated" color="green" onClick={() => {
handleClick(auction.id, avail, buyout)
}}>Submit</Button>
</Form.Group>
</Container>
</Grid.Column>
Expand Down
1 change: 0 additions & 1 deletion client/src/components/LogIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class LogIn extends Component {
this.passwordNode.value = '';

fetch('/auth/login', {
//don't forget the headers, otherwise it won't work
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand Down
14 changes: 7 additions & 7 deletions server/controllers/auctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ module.exports = function(io) {
bid.bidder_id = req.user.userId;
bid.bid_price = req.body.bidPrice;
bid.bid_date = new Moment().format('YYYY-MM-DD HH:mm:ss');
// console.log('bidder', bid.bidder_id);
// console.log(io.sockets.sockets, 'listof sockets in auctions handler');
console.log('bid: ', bid);
model.createBid(bid)
.then((bid) => {
const update = {};
update.auction_id = req.params.auctionId;
update.bid = bid;

console.log('update: ', update);
return model.updateAuction(update)
.then((bid) => {
// new bid beats current highest bid
Expand All @@ -103,7 +103,7 @@ module.exports = function(io) {
type: 'auction',
date: new Moment().format('YYYY-MM-DD HH:mm:ss'),
text: `Someone has bid on your auction ${auction[0].artwork.art_name}`
}]
}];
return model.createMassNotifications(noty)
.then(() => {
// console.log(io.socketList, 'list socket emissions')
Expand All @@ -115,15 +115,15 @@ module.exports = function(io) {
io.socketList[bid.bidder_id].emit('action', {type: 'UPDATE_NEW_NOTIFICATIONS', data: [noty[0]]});
}
// console.log('room:'+req.params.auctionId);
io.emit('action', {type: 'UPDATE_CURRENT_BID', current_bid: bid.bid_price || bid.current_bid, current_bid_id: bid.id || bid.current_bid_id});
io.emit('action', {type: 'UPDATE_CURRENT_BID', bid: bid.bid_price || bid.current_bid});
res.status(201).json({
current_bid: bid.bid_price || bid.current_bid,
current_bid_id: bid.id || bid.current_bid_id
});
})
})
});
});
}
io.emit('action', {type: 'UPDATE_CURRENT_BID', current_bid: bid.bid_price || bid.current_bid, current_bid_id: bid.id || bid.current_bid_id});
io.emit('action', {type: 'UPDATE_CURRENT_BID', bid: bid.bid_price || bid.current_bid});
res.status(201).json({
current_bid: bid.bid_price || bid.current_bid,
current_bid_id: bid.id || bid.current_bid_id
Expand Down
4 changes: 1 addition & 3 deletions server/database/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,4 @@ module.exports = {
updatePaymentStatus(status, auctionId) {
return db.query('UPDATE closed_auctions SET payment_status=$1 WHERE auction_id=$2', [status, auctionId]);
}


};
};
8 changes: 4 additions & 4 deletions server/database/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ module.exports = function createSchemas(db) {
artwork_id BIGINT NOT NULL REFERENCES artworks(id),\
start_date TIMESTAMP NOT NULL,\
end_date TIMESTAMP NOT NULL,\
start_price BIGINT NOT NULL,\
buyout_price BIGINT NOT NULL,\
current_bid BIGINT DEFAULT NULL,\
start_price DECIMAL NOT NULL,\
buyout_price DECIMAL NOT NULL,\
current_bid DECIMAL DEFAULT NULL,\
current_bid_id BIGINT DEFAULT NULL,\
bid_counter BIGINT DEFAULT 0\
)');
Expand All @@ -43,7 +43,7 @@ module.exports = function createSchemas(db) {
bidder_id BIGINT NOT NULL REFERENCES users(id),\
auction_id BIGINT NOT NULL REFERENCES auctions(id),\
bid_date TIMESTAMP NOT NULL,\
bid_price BIGINT NOT NULL\
bid_price DECIMAL NOT NULL\
)');
let attributes = t.query('CREATE TABLE IF NOT EXISTS attributes (\
id SERIAL PRIMARY KEY NOT NULL,\
Expand Down

0 comments on commit 401683f

Please sign in to comment.