From e9f812290c592acc57ad8b5cb1c4c488b45cd5c9 Mon Sep 17 00:00:00 2001 From: Ben <36240171+benkyd@users.noreply.github.com> Date: Tue, 12 Apr 2022 19:47:37 +0100 Subject: [PATCH] lol Former-commit-id: e3fe758afdc1e015a2332f62e201f2f53c9d0aea --- client/public/components/product-list.mjs | 42 +++++++++++++++----- client/public/components/storefront.mjs | 12 ++++++ src/controllers/controller-master.js | 3 ++ src/controllers/set-controller.js | 48 +++++++++++++++++++++++ src/database/database.js | 3 +- src/index.js | 4 +- src/routes/sets-router.js | 33 ++++------------ 7 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 src/controllers/controller-master.js diff --git a/client/public/components/product-list.mjs b/client/public/components/product-list.mjs index 52dc400..1019f19 100644 --- a/client/public/components/product-list.mjs +++ b/client/public/components/product-list.mjs @@ -9,15 +9,37 @@ class ProductList extends Component { async OnMount() { const route = this.state.getroute; - this.products = await fetch(route).then(response => response.json()); + const products = await fetch(route).then(response => response.json()); + this.setState({ + ...this.getState, + products: products.data, + current_page: products.page.current_page, + last_page: products.page.last_page, + total: products.page.total, + }); } Render() { + this.keepLoading = false; + if (this.state.current_page >= this.state.last_page) { + this.keepLoading = false; + this.loadingBar = ''; + } else { + this.keepLoading = true; + this.loadingBar = ` + +
+ +
+
+ `; + } + return { template: `

{this.state.title}

- ${this.products.data.map(product => { + ${this.state.products.data.map(product => { return ` - -
- -
-
+ ${this.loadingBar} `, style: ` .product-list { @@ -136,8 +154,14 @@ class ProductList extends Component { OnRender() { - - + // scroll to bottom event listener + if (this.keepLoading) { + window.addEventListener('scroll', () => { + if (window.innerHeight + window.scrollY >= document.body.offsetHeight) { + console.log('scrolled to bottom'); + } + }); + } } } diff --git a/client/public/components/storefront.mjs b/client/public/components/storefront.mjs index d6d172e..332035e 100644 --- a/client/public/components/storefront.mjs +++ b/client/public/components/storefront.mjs @@ -20,12 +20,24 @@ class StoreFront extends Component {
diff --git a/src/controllers/controller-master.js b/src/controllers/controller-master.js new file mode 100644 index 0000000..e7f2025 --- /dev/null +++ b/src/controllers/controller-master.js @@ -0,0 +1,3 @@ +module.exports = { + ResultsPerPage: 16, +}; diff --git a/src/controllers/set-controller.js b/src/controllers/set-controller.js index e69de29..19e692a 100644 --- a/src/controllers/set-controller.js +++ b/src/controllers/set-controller.js @@ -0,0 +1,48 @@ +const ControllerMaster = require('./controller-master.js'); +const Database = require('../database/database.js'); + +function ValidateQuery(query) { + const validation = ControllerMaster.ValidateQuery(query); + if (!validation.isValid) { + return { + isValid: false, + error: validation.error, + longError: validation.longError, + }; + } + + return { + isValid: true, + }; +} + +async function GetSets(page, resPerPage) { + // query for the set + await Database.Query('BEGIN TRANSACTION;'); + const countRes = await Database.Query('SELECT COUNT (*) FROM lego_set;'); + const total = parseInt(countRes.rows[0].count); + const dbres = await Database.Query(` + SELECT + id, name, price, new_price AS "discount" + FROM lego_set + LEFT JOIN lego_set_inventory as inv ON lego_set.id = inv.set_id + ORDER BY id ASC + LIMIT $1::int + OFFSET $2::int`, + [resPerPage, page * resPerPage]); + await Database.Query('END TRANSACTION;'); + + const sets = dbres.rows; + + for (const set of sets) { + set.image = `/api/cdn/${set.id}.png`; + set.type = 'set'; + } + + return { total, sets }; +} + +module.exports = { + ValidateQuery, + GetSets, +}; diff --git a/src/database/database.js b/src/database/database.js index a207942..43446a1 100644 --- a/src/database/database.js +++ b/src/database/database.js @@ -45,7 +45,7 @@ async function connectToDatabase() { async function Query(query, params, callback) { if (!connection) { - await connect(); + await Connect(); } // debug moment @@ -59,7 +59,6 @@ async function Destroy() { connection = null; } - module.exports = { Connect, Query, diff --git a/src/index.js b/src/index.js index ada2dfe..9402f1c 100644 --- a/src/index.js +++ b/src/index.js @@ -12,8 +12,8 @@ async function main() { logLevel: process.env.LOG_LEVEL, logToConsole: process.env.LOG_CONSOLE, logFile: process.env.LOG_FILE, - networkHost: process.env.LOG_NET_HOST, - networkPort: process.env.LOG_NET_PORT, + // networkHost: process.env.LOG_NET_HOST, + // networkPort: process.env.LOG_NET_PORT, }); Logger.Info('Pre-Init Complete'); diff --git a/src/routes/sets-router.js b/src/routes/sets-router.js index 97409a6..279b489 100644 --- a/src/routes/sets-router.js +++ b/src/routes/sets-router.js @@ -40,36 +40,17 @@ async function Get(req, res) { async function Featured(req, res) { // query all sets and return all of them - const dbres = await Database.Query(`SELECT - id, name, price, new_price AS "discount" - FROM lego_set - LEFT JOIN lego_set_inventory as inv ON lego_set.id = inv.set_id - ORDER BY id ASC; - `); - const sets = dbres.rows; - - for (const set of sets) { - set.image = `/api/cdn/${set.id}.png`; - set.type = 'set'; - } + const { sets } = await Controller.GetSets(0, 8); res.send(JSON.stringify({ data: [...sets], + page: { + total_sent: sets.length, + per_page: 8, + current_page: 1, + last_page: 1, + }, })); - - // Validation - // const validation = Controller.ValidateQuery(query); - // if (!validation.isValid) { - // return res.status(400).json({ - // error: { - // short: validation.error, - // long: validation.longError, - // }, - // }); - // } - - - // next(); } module.exports = {