diff --git a/components/arcade/prizes.js b/components/arcade/prizes.js index 3da73a24..e1d089bf 100644 --- a/components/arcade/prizes.js +++ b/components/arcade/prizes.js @@ -20,6 +20,7 @@ const Prizes = ({ index, hoursBalance = null, stock, + inStock = true, ...props }) => { const parsedFulfillmentDesc = fulfillmentDescription?.replace( @@ -36,7 +37,7 @@ const Prizes = ({ return ( {text} - {stock && stock != null && stock > 0 && stock <= 100 && ( + {inStock && stock != null && stock > 0 && stock <= 100 && ( + {inStock && ( - + + > You can order up to {quantity} of these @@ -119,10 +122,10 @@ const Prizes = ({ // only show the quantity dropdown if you have enough hours to buy at least 2 of the item (hoursBalance ? hoursBalance / cost < 2 : null) ? null : ( ) } @@ -130,15 +133,15 @@ const Prizes = ({ // only show the buy button if you have enough hours to buy at least 1 of the item (hoursBalance ? hoursBalance / cost < 1 : null) ? null : ( @@ -147,6 +150,7 @@ const Prizes = ({ )} + )} - {cost} {link ? '🎟️' : cost == 1 ? 'ticket' : 'tickets'} + {cost} 🎟️ - { document.getElementById(`${parsedFullName}-info`).showModal() }} - > + > 📦 + )} 0.5 ? 1 : -1) }, []); + + const inStockItems = availableItems.filter(item => item['Stock'] === null || item['Stock'] > 0 ); + const outOfStockItems = availableItems.filter(item => item['Stock'] !== null && item['Stock'] <= 0); + return ( <> - {availableItems + {inStockItems .sort((a, b) => a['Cost Hours'] - b['Cost Hours']) .map((item) => ( handleQuantityChange(item.id, q)} // Pass handler to update quantity hoursBalance={hoursBalance} stock={item['Stock']} + inStock={true} + /> + ))} + + Out of stock items + + {outOfStockItems + .sort((a, b) => a['Cost Hours'] - b['Cost Hours']) + .map((item) => ( + handleQuantityChange(item.id, q)} // Pass handler to update quantity + hoursBalance={hoursBalance} + stock={item['Stock']} + inStock={false} /> ))} diff --git a/pages/api/arcade/shop.js b/pages/api/arcade/shop.js index ea5b6224..21eb9b3d 100644 --- a/pages/api/arcade/shop.js +++ b/pages/api/arcade/shop.js @@ -1,14 +1,46 @@ import AirtablePlus from "airtable-plus" export const shopParts = async () => { - const airtable = new AirtablePlus({ + const baseID = "app4kCWulfB02bV8Q" + const shopItemsTable = new AirtablePlus({ apiKey: process.env.AIRTABLE_API_KEY, - baseID: "app4kCWulfB02bV8Q", + baseID, tableName: "Shop Items" }) + const ordersTable = new AirtablePlus({ + apiKey: process.env.AIRTABLE_API_KEY, + baseID, + tableName: "Orders" + }) - const records = await airtable.read() - return records.map(record => ({id: record.id, ...record.fields})) + const records = await shopItemsTable.read() + const newRecordsPromise = records.map(async record => { + const fields = record.fields; + let stock = fields["Stock"] + + if (stock && fields["Orders"]) { + const orderIds = fields["Orders"] + const ordersFilter = orderIds.map(id => `RECORD_ID() = "${id}"`).join(", ") + const data = await ordersTable.read({ + filterByFormula: ` + AND( + OR(${ordersFilter}), + OR( + {Status} = "Fulfilled", + {Status} = "Awaiting Fulfillment" + ) + )` + }) + + stock -= data.length; + } + return { id: record.id, ...record.fields, "Stock": (stock == null)? null : (stock >= 0 ? stock : 0) } + }) + + + const newRecords = await Promise.all(newRecordsPromise) + + return newRecords } export default async function handler(req, res) { @@ -21,8 +53,9 @@ export default async function handler(req, res) { description: record['Description'], hours: record['Cost Hours'], imageURL: record['Image URL'], + stock: record['Stock'], } }) - res.json(filteredData) + return res.json(filteredData) } \ No newline at end of file diff --git a/pages/arcade/[userAirtableID]/shop.js b/pages/arcade/[userAirtableID]/shop.js index 3b60630b..9349c1b5 100644 --- a/pages/arcade/[userAirtableID]/shop.js +++ b/pages/arcade/[userAirtableID]/shop.js @@ -88,7 +88,7 @@ export async function getStaticProps({params}) { id: item.id, 'Image URL': item['Image URL'] || null, 'Max Order Quantity': item['Max Order Quantity'] || 1, - Stock: item['Stock'] || null + Stock: item['Stock'] >= 0 ? item['Stock'] : null })) props.availableItems = availableItems }), diff --git a/pages/arcade/shop.js b/pages/arcade/shop.js index 17f644cc..8df71a54 100644 --- a/pages/arcade/shop.js +++ b/pages/arcade/shop.js @@ -79,7 +79,7 @@ export async function getStaticProps() { id: item.id, 'Image URL': item['Image URL'] || null, 'Max Order Quantity': item['Max Order Quantity'] || 1, - Stock: item['Stock'] || null + Stock: item['Stock'] >= 0 ? item['Stock'] : null })) props.availableItems = availableItems }),