Skip to content

Commit

Permalink
feat: reduce amount of data loaded on acp admin page
Browse files Browse the repository at this point in the history
get rid of socket call and use ajaxify.data.categories
  • Loading branch information
barisusakli committed Jul 29, 2020
1 parent 7331fae commit 8d8117f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 1 addition & 7 deletions public/src/admin/manage/categories.js
Expand Up @@ -11,13 +11,7 @@ define('admin/manage/categories', [
var sortables;

Categories.init = function () {
socket.emit('admin.categories.getAll', function (err, payload) {
if (err) {
return app.alertError(err.message);
}

Categories.render(payload);
});
Categories.render(ajaxify.data.categories);

$('button[data-action="create"]').on('click', Categories.throwCreateModal);

Expand Down
14 changes: 12 additions & 2 deletions src/controllers/admin/categories.js
Expand Up @@ -43,9 +43,19 @@ categoriesController.get = async function (req, res, next) {
});
};

categoriesController.getAll = function (req, res) {
categoriesController.getAll = async function (req, res) {
// Categories list will be rendered on client side with recursion, etc.
res.render('admin/manage/categories', {});
const cids = await categories.getAllCidsFromSet('categories:cid');
const fields = [
'cid', 'name', 'level', 'icon', 'parentCid', 'disabled', 'link',
'color', 'bgColor', 'backgroundImage', 'imageClass',
];
const categoriesData = await categories.getCategoriesFields(cids, fields);
const result = await plugins.fireHook('filter:admin.categories.get', { categories: categoriesData, fields: fields });
const tree = categories.getTree(result.categories, 0);
res.render('admin/manage/categories', {
categories: tree,
});
};

categoriesController.getAnalytics = async function (req, res) {
Expand Down
11 changes: 9 additions & 2 deletions src/socket.io/admin/categories.js
@@ -1,5 +1,7 @@
'use strict';

const winston = require('winston');

const groups = require('../../groups');
const user = require('../../user');
const categories = require('../../categories');
Expand All @@ -18,9 +20,14 @@ Categories.create = async function (socket, data) {
};

Categories.getAll = async function () {
winston.warn('[deprecated] admin.categories.getAll deprecated, data is returned in the api route');
const cids = await categories.getAllCidsFromSet('categories:cid');
const categoriesData = await categories.getCategoriesData(cids);
const result = await plugins.fireHook('filter:admin.categories.get', { categories: categoriesData });
const fields = [
'cid', 'name', 'level', 'icon', 'parentCid', 'disabled', 'link',
'color', 'bgColor', 'backgroundImage', 'imageClass',
];
const categoriesData = await categories.getCategoriesFields(cids, fields);
const result = await plugins.fireHook('filter:admin.categories.get', { categories: categoriesData, fields: fields });
return categories.getTree(result.categories, 0);
};

Expand Down

0 comments on commit 8d8117f

Please sign in to comment.