Skip to content

Commit

Permalink
Fix nasty bug in bid logic (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmellor authored Jul 29, 2023
1 parent 24a097e commit ddb2702
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
20 changes: 12 additions & 8 deletions js/auctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,22 @@ function dataListenerCallback(data) {
}
}

export function parseDoc(doc) {
// Parse flat document data into structured Object
let data = {};
for (const [key, details] of Object.entries(doc.data())) {
let [item, bid] = key.split("_").map((i) => Number(i.match(/\d+/)));
data[item] = data[item] || {};
data[item][bid] = details;
}
return data;
}

export function dataListener(callback) {
// Listen for updates in active auctions
onSnapshot(doc(db, "auction", "items"), (doc) => {
console.debug("dataListener() read from auction/items");
// Parse flat document data into structured Object
let data = {};
for (const [key, details] of Object.entries(doc.data())) {
let [item, bid] = key.split("_").map((i) => Number(i.match(/\d+/)));
data[item] = data[item] || {};
data[item][bid] = details;
}
callback(data);
callback(parseDoc(doc));
});
}

Expand Down
24 changes: 15 additions & 9 deletions js/popups.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { auth, db } from "./firebase.js";
import { parseDoc } from "./auctions.js";
import {
doc,
setDoc,
Expand Down Expand Up @@ -132,12 +133,19 @@ if (bidModal) {
}
});

// Function for formatting bid field names
function fieldName(item, bid) {
const item_padded = item.toString().padStart(5, "0");
const bid_padded = bid.toString().padStart(5, "0");
return `item${item_padded}_bid${bid_padded}`;
}

// Function that handles bidding logic
function placeBid() {
let nowTime = new Date().getTime();
bidModalSubmit.setAttribute("disabled", ""); // disable the button while we check
let i = Number(bidModal.dataset.activeAuction.match("[0-9]+"));
let endTime = document.querySelector(`.card[data-id="${i}"]`).dataset
let itemId = Number(bidModal.dataset.activeAuction.match("[0-9]+"));
let endTime = document.querySelector(`.card[data-id="${itemId}"]`).dataset
.endTime;
let feedback = bidModal.querySelector(".invalid-feedback");
// Cleanse input
Expand Down Expand Up @@ -166,16 +174,14 @@ if (bidModal) {
let docRef = doc(db, "auction", "items");
getDoc(docRef).then(function (doc) {
console.debug("placeBid() read from auction/items");
let data = doc.data();
let itemId = `item${i.toString().padStart(5, "0")}`;
let bids = Object.keys(data).filter((key) => key.includes(itemId));
let item = data[bids[0]];
let bidId = `bid${bids.length.toString().padStart(5, "0")}`;
let currentBid = data[bids[bids.length - 1]].amount;
let bids = parseDoc(doc)[itemId];
let item = bids[0];
let bidId = Object.keys(bids).length;
let currentBid = bids[bidId - 1].amount;
console.debug(`itemId=${itemId} currentBid=${currentBid}`);
if (amount >= 1 + currentBid) {
updateDoc(docRef, {
[`${itemId}_${bidId}`]: {
[fieldName(itemId, bidId)]: {
amount: amount,
uid: auth.currentUser.uid,
},
Expand Down

0 comments on commit ddb2702

Please sign in to comment.