From ea80b4bf2b19ad88e7181ac2a99272d811a2d348 Mon Sep 17 00:00:00 2001 From: grey Date: Mon, 10 Jul 2023 17:50:31 +0200 Subject: [PATCH] - fixed issues with jumpy actions for item view - added AFLOW-23 support for categories and rewrote interface --- src/frontend/items.eta.html | 2 +- src/frontend/manage/categoryManager.eta.html | 26 +---- src/routes/api/v1/categories.ts | 106 +++++++++++++++---- static/js/editCategory.js | 41 +++++++ static/js/itemPageHandler.js | 11 +- 5 files changed, 137 insertions(+), 49 deletions(-) diff --git a/src/frontend/items.eta.html b/src/frontend/items.eta.html index ec1078a..1461c5c 100644 --- a/src/frontend/items.eta.html +++ b/src/frontend/items.eta.html @@ -100,7 +100,7 @@ Name Comment Status - Actions + Actions <% if(it.items.length == 0) { %> diff --git a/src/frontend/manage/categoryManager.eta.html b/src/frontend/manage/categoryManager.eta.html index 8e2b8a9..e2867ce 100644 --- a/src/frontend/manage/categoryManager.eta.html +++ b/src/frontend/manage/categoryManager.eta.html @@ -40,31 +40,15 @@ - +
- - - - + + + - - <% it.items.forEach(function(user){ %> - - - - - - <% }) %> - +
NameDescriptionActionNameDescriptionActions
<%= user.name %><%= user.description %> - - -
diff --git a/src/routes/api/v1/categories.ts b/src/routes/api/v1/categories.ts index c334e07..3e6dc42 100644 --- a/src/routes/api/v1/categories.ts +++ b/src/routes/api/v1/categories.ts @@ -1,31 +1,97 @@ import { Request, Response } from 'express'; import { prisma, __path, log } from '../../../index.js'; +import { parseIntOrUndefined, parseDynamicSortBy } from '../../../assets/helper.js'; -// Get category. -function get(req: Request, res: Response) { +// Get category +async function get(req: Request, res: Response) { // Check if required fields are present. - if (!req.query.name) { - res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' }); - return; + if (req.query.sort === undefined) { + req.query.sort = 'id'; + } + if (req.query.order === undefined) { + req.query.order = 'asc'; + } + if (req.query.search === undefined) { + req.query.search = ''; } - prisma.itemCategory - .findUnique({ + if (req.query.name) { + prisma.itemCategory + .findUnique({ + where: { + name: req.query.name.toString() + } + }) + .then((item) => { + if (item) { + res.status(200).json(item); + } else { + res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Category does not exist' }); + } + }) + .catch((err) => { + log.db.error(err); + res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' }); + }); + } else { + // Get all items + const itemCountNotFiltered = await prisma.itemCategory.count({}); + + // Get all items (filtered) + const itemCountFiltered = await prisma.itemCategory.count({ where: { - name: req.query.name.toString() - } - }) - .then((item) => { - if (item) { - res.status(200).json(item); - } else { - res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Category does not exist' }); - } - }) - .catch((err) => { - log.db.error(err); - res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' }); + OR: [ + { + name: { + // @ts-ignore + contains: req.query.search.length > 0 ? req.query.search : '' + } + }, + { + description: { + // @ts-ignore + contains: req.query.search.length > 0 ? req.query.search : '' + } + } + ] + }, + orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()) }); + + prisma.itemCategory + .findMany({ + take: parseIntOrUndefined(req.query.limit), + skip: parseIntOrUndefined(req.query.offset), + orderBy: parseDynamicSortBy(req.query.sort.toString(), req.query.order.toString()), + where: { + OR: [ + { + name: { + // @ts-ignore + contains: req.query.search.length > 0 ? req.query.search : '' + } + }, + { + description: { + // @ts-ignore + contains: req.query.search.length > 0 ? req.query.search : '' + } + } + ] + }, + }) + .then((items) => { + if (items) { + res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items }); + } else { + res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' }); + } + }) + .catch((err) => { + log.db.error(err); + res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' }); + }); + } } // Create category. diff --git a/static/js/editCategory.js b/static/js/editCategory.js index dbeb4a1..91ac32a 100644 --- a/static/js/editCategory.js +++ b/static/js/editCategory.js @@ -1,3 +1,5 @@ +const FLAG_supports_new_data_loader = true; + function getDataForEdit(name) { $.ajax({ type: 'get', @@ -37,3 +39,42 @@ function primeEdit() { form.setAttribute('method', 'PATCH'); return true; } + +const itemList = $('#itemList'); +// itemList.empty(); +itemList.bootstrapTable({ url: '/api/v1/categories', search: true, showRefresh: true, responseHandler: dataResponseHandler, sidePagination: 'server', serverSort: true, silentSort: false }); +setTimeout(() => { + activateTooltips(); +}, 1000); + +function loadPageData() { + itemList.bootstrapTable('refresh') + + setTimeout(() => { + activateTooltips(); + }, 1000); +} + +function dataResponseHandler(json) { + // console.log(json) + totalNotFiltered = json.totalNotFiltered; + total = json.total; + json = json.items; + json.forEach((item) => { + item.actions = ` + + ` + }); + ///// --------------------------------- ///// + setTimeout(() => { + activateTooltips(); + }, 200); + return {"rows": json, total: total, totalNotFiltered: totalNotFiltered, totalRows: total}; + +} + +loadPageData() diff --git a/static/js/itemPageHandler.js b/static/js/itemPageHandler.js index 11aef43..4361fe5 100644 --- a/static/js/itemPageHandler.js +++ b/static/js/itemPageHandler.js @@ -7,15 +7,12 @@ const FLAG_supports_new_data_loader = true; // Inital thing const itemList = $('#itemList'); - // itemList.empty(); - itemList.bootstrapTable({url: "/api/v1/items", search: true, showRefresh: true, responseHandler: dataResponseHandler, sidePagination: 'server', serverSort: true, silentSort: false}) - setTimeout(() => { - activateTooltips(); - }, 1000); +itemList.bootstrapTable({ url: '/api/v1/items', search: true, showRefresh: true, responseHandler: dataResponseHandler, sidePagination: 'server', serverSort: true, silentSort: false }); +setTimeout(() => { + activateTooltips(); +}, 1000); function loadPageData() { - const itemList = $('#itemList'); - // itemList.empty(); itemList.bootstrapTable('refresh') setTimeout(() => {