diff --git a/client/src/components/Auction.jsx b/client/src/components/Auction.jsx index f990665..98da043 100644 --- a/client/src/components/Auction.jsx +++ b/client/src/components/Auction.jsx @@ -17,6 +17,7 @@ class Auction extends Component { flag: false }; this.setBid = this.setBid.bind(this); + this.handleClick = this.handleClick.bind(this); } componentWillMount() { @@ -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`, { @@ -159,7 +183,6 @@ class Auction extends Component { // .catch((err) => { // dispatch(Bids.error(err)); // }); - // } // } } @@ -183,7 +206,7 @@ class Auction extends Component { } else { return (
- { + { this.handleSave(auction.id) }} handleUnsave={() => { this.handleUnsave(auction.id) diff --git a/client/src/components/AuctionDetail.jsx b/client/src/components/AuctionDetail.jsx index c897b16..d048da3 100644 --- a/client/src/components/AuctionDetail.jsx +++ b/client/src/components/AuctionDetail.jsx @@ -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}) => { @@ -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; } @@ -42,8 +35,8 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn

{auction.first_name} {auction.last_name} ({auction.artwork.age})

Auction Ends: {endTime}

Description: {auction.artwork.description}

-

Current Price (USD): ${(auction.current_bid || auction.start_price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

-

Buyout Price (USD): ${(auction.buyout_price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

+

Current Price (USD): ${(current || start).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

+

Buyout Price (USD): ${buyout.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

@@ -54,7 +47,7 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn Bid for: - { + { if(isNaN(e.target.value)) { e.target.value = ''; } else { @@ -62,7 +55,9 @@ const AuctionDetail = ({auction, setBid, handleClick, user, handleSave, handleUn } }} /> - + diff --git a/client/src/components/LogIn.jsx b/client/src/components/LogIn.jsx index 0611021..4632cc5 100644 --- a/client/src/components/LogIn.jsx +++ b/client/src/components/LogIn.jsx @@ -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', diff --git a/server/controllers/auctions.js b/server/controllers/auctions.js index 5d4b66a..b129a54 100644 --- a/server/controllers/auctions.js +++ b/server/controllers/auctions.js @@ -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 @@ -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') @@ -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 diff --git a/server/database/queries.js b/server/database/queries.js index 43d2229..23e3c24 100644 --- a/server/database/queries.js +++ b/server/database/queries.js @@ -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]); } - - -}; \ No newline at end of file +}; diff --git a/server/database/schema.js b/server/database/schema.js index c09e210..990fb5d 100644 --- a/server/database/schema.js +++ b/server/database/schema.js @@ -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\ )'); @@ -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,\