mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-01 21:30:30 +01:00
feat: added PUT /api/v1/categories/:cid route
Deprecated admin.categories.update socket call Showing stack trace in console for errors, when in development mode
This commit is contained in:
@@ -195,18 +195,18 @@ define('admin/manage/categories', [
|
||||
};
|
||||
|
||||
Categories.toggle = function (cids, disabled) {
|
||||
var payload = {};
|
||||
|
||||
cids.forEach(function (cid) {
|
||||
payload[cid] = {
|
||||
disabled: disabled ? 1 : 0,
|
||||
};
|
||||
var requests = cids.map(function (cid) {
|
||||
return $.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + cid,
|
||||
method: 'put',
|
||||
data: {
|
||||
disabled: disabled ? 1 : 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
socket.emit('admin.categories.update', payload, function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
$.when(requests).fail(function (ev) {
|
||||
app.alertError(ev.responseJSON.status.message);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -236,7 +236,14 @@ define('admin/manage/categories', [
|
||||
}
|
||||
|
||||
newCategoryId = -1;
|
||||
socket.emit('admin.categories.update', modified);
|
||||
|
||||
Object.keys(modified).forEach(function (cid) {
|
||||
$.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + cid,
|
||||
method: 'put',
|
||||
data: modified[cid],
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ define('admin/manage/category', [
|
||||
'benchpress',
|
||||
], function (uploader, iconSelect, colorpicker, categorySelector, Benchpress) {
|
||||
var Category = {};
|
||||
var modified_categories = {};
|
||||
var updateHash = {};
|
||||
|
||||
Category.init = function () {
|
||||
$('#category-settings select').each(function () {
|
||||
@@ -53,24 +53,24 @@ define('admin/manage/category', [
|
||||
return app.alertError('[[admin/manage/categories:alert.not-enough-whitelisted-tags]]');
|
||||
}
|
||||
|
||||
if (Object.keys(modified_categories).length) {
|
||||
socket.emit('admin.categories.update', modified_categories, function (err, result) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
if (result && result.length) {
|
||||
app.flags._unsaved = false;
|
||||
app.alert({
|
||||
title: 'Updated Categories',
|
||||
message: 'Category IDs ' + result.join(', ') + ' was successfully updated.',
|
||||
type: 'success',
|
||||
timeout: 2000,
|
||||
});
|
||||
}
|
||||
var cid = ajaxify.data.category.cid;
|
||||
$.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + cid,
|
||||
method: 'put',
|
||||
data: updateHash,
|
||||
}).done(function (res) {
|
||||
app.flags._unsaved = false;
|
||||
app.alert({
|
||||
title: 'Updated Categories',
|
||||
message: 'Category "' + res.response.name + '" was successfully updated.',
|
||||
type: 'success',
|
||||
timeout: 2000,
|
||||
});
|
||||
modified_categories = {};
|
||||
}
|
||||
updateHash = {};
|
||||
}).fail(function (ev) {
|
||||
app.alertError(ev.responseJSON.status.message);
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
@@ -224,55 +224,51 @@ define('admin/manage/category', [
|
||||
|
||||
$('button[data-action="setParent"], button[data-action="changeParent"]').on('click', Category.launchParentSelector);
|
||||
$('button[data-action="removeParent"]').on('click', function () {
|
||||
var payload = {};
|
||||
payload[ajaxify.data.category.cid] = {
|
||||
parentCid: 0,
|
||||
};
|
||||
|
||||
socket.emit('admin.categories.update', payload, function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
$.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + ajaxify.data.category.cid,
|
||||
method: 'put',
|
||||
data: {
|
||||
parentCid: 0,
|
||||
},
|
||||
}).done(function () {
|
||||
$('button[data-action="removeParent"]').parent().addClass('hide');
|
||||
$('button[data-action="changeParent"]').parent().addClass('hide');
|
||||
$('button[data-action="setParent"]').removeClass('hide');
|
||||
}).fail(function (ev) {
|
||||
app.alertError(ev.responseJSON.status.message);
|
||||
});
|
||||
});
|
||||
$('button[data-action="toggle"]').on('click', function () {
|
||||
var payload = {};
|
||||
var $this = $(this);
|
||||
var disabled = $this.attr('data-disabled') === '1';
|
||||
payload[ajaxify.data.category.cid] = {
|
||||
disabled: disabled ? 0 : 1,
|
||||
};
|
||||
socket.emit('admin.categories.update', payload, function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
$.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + ajaxify.data.category.cid,
|
||||
method: 'put',
|
||||
data: {
|
||||
disabled: disabled ? 0 : 1,
|
||||
},
|
||||
}).done(function () {
|
||||
$this.translateText(!disabled ? '[[admin/manage/categories:enable]]' : '[[admin/manage/categories:disable]]');
|
||||
$this.toggleClass('btn-primary', !disabled).toggleClass('btn-danger', disabled);
|
||||
$this.attr('data-disabled', disabled ? 0 : 1);
|
||||
}).fail(function (ev) {
|
||||
app.alertError(ev.responseJSON.status.message);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function modified(el) {
|
||||
var cid = ajaxify.data.category.cid;
|
||||
|
||||
if (cid) {
|
||||
var value;
|
||||
if ($(el).is(':checkbox')) {
|
||||
value = $(el).is(':checked') ? 1 : 0;
|
||||
} else {
|
||||
value = $(el).val();
|
||||
}
|
||||
|
||||
modified_categories[cid] = modified_categories[cid] || {};
|
||||
modified_categories[cid][$(el).attr('data-name')] = value;
|
||||
|
||||
app.flags = app.flags || {};
|
||||
app.flags._unsaved = true;
|
||||
var value;
|
||||
if ($(el).is(':checkbox')) {
|
||||
value = $(el).is(':checked') ? 1 : 0;
|
||||
} else {
|
||||
value = $(el).val();
|
||||
}
|
||||
|
||||
updateHash[$(el).attr('data-name')] = value;
|
||||
|
||||
app.flags = app.flags || {};
|
||||
app.flags._unsaved = true;
|
||||
}
|
||||
|
||||
function handleTags() {
|
||||
@@ -306,16 +302,13 @@ define('admin/manage/category', [
|
||||
});
|
||||
|
||||
categorySelector.modal(categories, function (parentCid) {
|
||||
var payload = {};
|
||||
|
||||
payload[ajaxify.data.category.cid] = {
|
||||
parentCid: parentCid,
|
||||
};
|
||||
|
||||
socket.emit('admin.categories.update', payload, function (err) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
$.ajax({
|
||||
url: config.relative_path + '/api/v1/categories/' + ajaxify.data.category.cid,
|
||||
method: 'put',
|
||||
data: {
|
||||
parentCid: parentCid,
|
||||
},
|
||||
}).done(function () {
|
||||
var parent = allCategories.filter(function (category) {
|
||||
return category && parseInt(category.cid, 10) === parseInt(parentCid, 10);
|
||||
});
|
||||
@@ -325,6 +318,8 @@ define('admin/manage/category', [
|
||||
$('button[data-action="setParent"]').addClass('hide');
|
||||
var buttonHtml = '<i class="fa ' + parent.icon + '"></i> ' + parent.name;
|
||||
$('button[data-action="changeParent"]').html(buttonHtml).parent().removeClass('hide');
|
||||
}).fail(function (ev) {
|
||||
app.alertError(ev.responseJSON.status.message);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -357,7 +357,11 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||
}
|
||||
|
||||
const returnPayload = helpers.generateError(statusCode, message);
|
||||
returnPayload.stack = payload.stack;
|
||||
|
||||
if (global.env === 'development') {
|
||||
returnPayload.stack = payload.stack;
|
||||
process.stdout.write(payload.stack);
|
||||
}
|
||||
res.status(statusCode).json(returnPayload);
|
||||
} else if (!payload) {
|
||||
// Non-2xx statusCode, generate predefined error
|
||||
|
||||
@@ -11,3 +11,12 @@ Categories.create = async (req, res) => {
|
||||
const categoryObjs = await categories.getCategories([response.cid]);
|
||||
helpers.formatApiResponse(200, res, categoryObjs[0]);
|
||||
};
|
||||
|
||||
Categories.update = async (req, res) => {
|
||||
const payload = {};
|
||||
payload[req.params.cid] = req.body;
|
||||
|
||||
await categories.update(payload);
|
||||
const categoryObjs = await categories.getCategories([req.params.cid]);
|
||||
helpers.formatApiResponse(200, res, categoryObjs[0]);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const router = require('express').Router();
|
||||
const middleware = require('../../middleware');
|
||||
const controllers = require('../../controllers');
|
||||
const routeHelpers = require('../../routes/helpers');
|
||||
const routeHelpers = require('../helpers');
|
||||
|
||||
const setupApiRoute = routeHelpers.setupApiRoute;
|
||||
|
||||
@@ -11,26 +11,9 @@ module.exports = function () {
|
||||
const middlewares = [middleware.authenticate];
|
||||
|
||||
setupApiRoute(router, '/', middleware, [...middlewares, middleware.checkRequired.bind(null, ['name'])], 'post', controllers.write.categories.create);
|
||||
|
||||
// app.post('/', apiMiddleware.requireUser, apiMiddleware.requireAdmin, function(req, res) {
|
||||
// if (!utils.checkRequired(['name'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// Categories.create(req.body, function(err, categoryObj) {
|
||||
// return errorHandler.handle(err, res, categoryObj);
|
||||
// });
|
||||
// });
|
||||
setupApiRoute(router, '/:cid', middleware, [...middlewares], 'put', controllers.write.categories.update);
|
||||
|
||||
// app.route('/:cid')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.requireAdmin, apiMiddleware.validateCid, function(req, res) {
|
||||
// var payload = {};
|
||||
// payload[req.params.cid] = req.body;
|
||||
|
||||
// Categories.update(payload, function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.requireAdmin, apiMiddleware.validateCid, function(req, res) {
|
||||
// Categories.purge(req.params.cid, req.user.uid, function(err) {
|
||||
// return errorHandler.handle(err, res);
|
||||
|
||||
@@ -52,6 +52,8 @@ Categories.purge = async function (socket, cid) {
|
||||
};
|
||||
|
||||
Categories.update = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, 'PUT /api/v1/categories/:cid');
|
||||
|
||||
if (!data) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user