Skip to content

Commit

Permalink
Add update functionality to card pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
lannonbr committed Oct 19, 2024
1 parent cbe8289 commit 840f2e2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
13 changes: 11 additions & 2 deletions mvc/controllers/setListing.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,22 @@ module.exports = (router, app) => {
}
);

model.cardMetadata = JSON.stringify({
let cardMetadata = {
identifier: card.id,
name: card.name,
rarity: card.rarity,
tcgLink: model.tcgPlayerUrl,
prices: model.priceList,
});
};

const followCard = getCard(cardId);

if (followCard != undefined) {
cardMetadata.requestedPrice = followCard.requestedPrice;
cardMetadata.refreshCron = followCard.refreshCron;
}

model.cardMetadata = JSON.stringify(cardMetadata);

model.content.pageTitle = `Card: ${card.name} (${cardId})`;
res.render("card", model);
Expand Down
33 changes: 19 additions & 14 deletions mvc/models/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ function getCard(cardId) {
}

function saveCard(card) {
const insertStmt = appDb.db.prepare(
"INSERT INTO followedCards (uuid, identifier, cardName, rarity, marketPrice, requestedPrice, tcgLink, refreshCron) VALUES (@uuid, @identifier, @cardName, @rarity, @marketPrice, @requestedPrice, @tcgLink, @refreshCron)"
);

insertStmt.run({
uuid: card.uuid,
identifier: card.identifier,
cardName: card.name,
rarity: card.rarity,
marketPrice: card.marketPrice,
requestedPrice: card.requestedPrice,
tcgLink: card.tcgLink,
refreshCron: card.refreshCron,
});
// Update the card if it already has been saved
if (getCard(card.identifier) != undefined) {
updateFollowCard(card.identifier, card.requestedPrice, card.refreshCron);
} else {
const insertStmt = appDb.db.prepare(
"INSERT INTO followedCards (uuid, identifier, cardName, rarity, marketPrice, requestedPrice, tcgLink, refreshCron) VALUES (@uuid, @identifier, @cardName, @rarity, @marketPrice, @requestedPrice, @tcgLink, @refreshCron)"
);

insertStmt.run({
uuid: card.uuid,
identifier: card.identifier,
cardName: card.name,
rarity: card.rarity,
marketPrice: card.marketPrice,
requestedPrice: card.requestedPrice,
tcgLink: card.tcgLink,
refreshCron: card.refreshCron,
});
}
}

function removeCard(id) {
Expand Down
8 changes: 6 additions & 2 deletions statics/js/cardDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ function createSaveCardDialog() {
<input type="hidden" name="tcgLink" value="${data.tcgLink}" />
<label for="requestedPrice">Requested Price:</label>
<input type="text" name="requestedPrice" id="requestedPrice" required />
<input type="text" name="requestedPrice" id="requestedPrice" required ${
data.requestedPrice != undefined ? `value="${data.requestedPrice}"` : ""
} />
<label for="refreshCron">Schedule for refreshing (cron format, ref: <a href="https://crontab.guru">crontab.guru</a>):</label>
<input type="text" name="refreshCron" id="refreshCron" required />
<input type="text" name="refreshCron" id="refreshCron" required ${
data.refreshCron != undefined ? `value="${data.refreshCron}"` : ""
}/>
<button type="submit">Submit</button>
`;

Expand Down
13 changes: 10 additions & 3 deletions statics/js/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const { createSaveCardDialog } = require("./cardDialog");

if (window.location.href.includes("/card/")) {
document
.getElementById("addToWatchlistBtn")
.addEventListener("click", createSaveCardDialog);
const data = JSON.parse(document.getElementById("metadata").innerText);

if (data.requestedPrice) {
createSaveCardDialog();
document.getElementById("addToWatchlistBtn").remove();
} else {
document
.getElementById("addToWatchlistBtn")
.addEventListener("click", createSaveCardDialog);
}
}

if (window.location.pathname == "/") {
Expand Down

0 comments on commit 840f2e2

Please sign in to comment.