Skip to content

Commit

Permalink
Added new ability to enable or disable tags for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
guillecro committed Aug 29, 2023
1 parent 325cf5e commit f6a5dd0
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ext/lib/site/formulario-propuesta/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FormularioPropuesta extends Component {

let newState = {
forum,
availableTags: tags,
availableTags: tags.filter(t => t.enabled),
facultades,
claustros,
availableEjes: ejes,
Expand Down
2 changes: 1 addition & 1 deletion ext/lib/site/home-propuestas/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class HomePropuestas extends Component {
textStore.findAllDict()
]).then((results) => {
const [facultades, claustros, tags, forum, proyectos, texts] = results
const tagsMap = tags.map((tag) => { return { value: tag.id, name: tag.name } })
const tagsMap = tags.filter((t) => t.enabled).map((tag) => { return { value: tag.id, name: tag.name } })
const tag = this.props.location.query.tags ? [tagsMap.find((j) => j.name == this.props.location.query.tags).value] : []
const tiposIdea = forum.topicsAttrs.find((a) => a.name == 'state').options.map((state) => { return { value: state.name, name: state.title } })
const tipoIdea = forum.config.ideacion ? ['pendiente'] : forum.config.preVotacion || forum.config.votacion ? ['proyecto'] : []
Expand Down
26 changes: 19 additions & 7 deletions lib/admin/admin-tags-form/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@
.form-group
label= t('admin-tags-form.label.name')
input.form-control(type='text', maxlength='50', id="name", name='name', placeholder=t('admin-tags-form.placeholder.name'), value=tag.name, validate='required')
//- Color input
.form-group
label= t('admin-tags-form.label.image')
.tag-images
- each image in images
- const id = 'tag-image-' + image.name
.tag-image(style='background-image: url(' + image.url + ')')
input(type='radio', name='image', value=image.name, checked= tag.image === image.name, validate='any:image', id=id)
label(for=id)
label= 'Color'
input.form-control(type='color', id="color", name='color', placeholder=t('admin-tags-form.placeholder.color'), value=tag.color)
//- Checkbox for enabled
.form-group
label= 'Habilitado en el formulario de ideas'
p.small= 'Habilite o deshabilite la tematica en el formulario de ideas. Si esta deshabilitado, no se podra seleccionar en el formulario de ideas, aunque como administrador usted puede "forzar" la tematica en el formulario de ideas del administrador.'
select.form-control(name="enabled")
option(value="true")= '✅ Habilitado'
option(value="false")= '❌ Deshabilitado'

//- .form-group
//- label= t('admin-tags-form.label.image')
//- .tag-images
//- - each image in images
//- - const id = 'tag-image-' + image.name
//- .tag-image(style='background-image: url(' + image.url + ')')
//- input(type='radio', name='image', value=image.name, checked= tag.image === image.name, validate='any:image', id=id)
//- label(for=id)
.form-group
button.btn.btn-success.btn-block.btn-lg
= t('admin-tags-form.button.submit')
6 changes: 6 additions & 0 deletions lib/admin/admin-tags-form/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export default class TagForm extends FormView {
switchOn () {
this.on('success', this.bound('onsuccess'))
this.bind('click', 'input[name="image"]', this.bound('onimageclick'))
// select the proper option in the select field
if (this.options.form.action === '/api/tag/create') {
this.find('select[name="enabled"]').val('true')
} else {
this.find('select[name="enabled"]').val(this.options.tag.enabled === true ? 'true' : 'false')
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/admin/admin-tags/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@
- each tag in tags
a.list-group-item(href=urlBuilder.for('admin.tags.id', {id: tag.id, forum: forum.name}))
span.tag-title= tag.name
- if (tag.enabled)
span.label.label-success.pull-right
span.glyphicon.glyphicon-ok
= ' Habilitado'
- else
span.label.label-danger.pull-right
span.glyphicon.glyphicon-remove
= ' Deshabilitado'
//- - each tag in tags
//- //- show all tag data raw
//- pre= JSON.stringify(tag, null, 2)
14 changes: 14 additions & 0 deletions lib/admin/admin-topics-form/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ let created = false

export default class TopicForm extends FormView {
constructor (topic, forum, tags, enableUploadPictures) {
tags.forEach(tag => {
console.log(tag)
if(!tag.enabled){
tag.name = tag.name + ' ( ❌ Deshabilitado )'
}
})
// put all the disabled tags at the end of the list
tags.sort((a, b) => {
if (a.enabled && !b.enabled) return -1
if (!a.enabled && b.enabled) return 1
return 0
})

const locals = {
form: { title: null, action: null, method: null, type: null },
topic: topic || { clauses: [] },
Expand Down Expand Up @@ -67,6 +80,7 @@ export default class TopicForm extends FormView {

if (tags.length === 0) return


if (created) {
this.messages([t('admin-topics-form.message.onsuccess')])
created = false
Expand Down
2 changes: 1 addition & 1 deletion lib/api-v2/db-api/topics/scopes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ exports.ordinary.populate = [
},
{
path: 'tag',
select: 'id hash name color image'
select: 'id hash name color image enabled'
},
{
path: 'eje'
Expand Down
16 changes: 9 additions & 7 deletions lib/api/tag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.get('/tag/all', function (req, res) {
if (req.query && req.query.field)
res.status(200).json(tags.map(expose('id ' + req.query.field)))
else
res.status(200).json(tags.map(expose('id hash name color image createdAt')))
res.status(200).json(tags.map(expose('id hash name color image createdAt enabled')))
})
})

Expand All @@ -37,7 +37,7 @@ app.get('/tag/search', function (req, res) {
if (err) return _handleError(err, req, res)

log('Serving tags %j', tags)
res.status(200).json(tags.map(expose('id hash name color image createdAt')))
res.status(200).json(tags.map(expose('id hash name color image createdAt enabled')))
})
})

Expand All @@ -49,7 +49,7 @@ app.get('/tag/:id', function (req, res) {

log('Serving tag %s', tagDoc.id)
var keys = [
'id hash name color image createdAt'
'id hash name color image createdAt enabled'
].join(' ')

res.status(200).json(expose(keys)(tagDoc.toJSON()))
Expand All @@ -58,25 +58,27 @@ app.get('/tag/:id', function (req, res) {

app.post('/tag/create', staff, function (req, res, next) {
log('Request /tag/create %j', req.body.tag)

if (req.body.enabled === 'true') req.body.enabled = true
if (req.body.enabled === 'false') req.body.enabled = fals
api.tag.create(req.body, function (err, tagDoc) {
if (err) return next(err)
var keys = [
'id hash name color image createdAt'
'id hash name color image createdAt enabled'
].join(' ')
res.status(200).json(expose(keys)(tagDoc))
})
})

app.post('/tag/:id', staff, function (req, res) {
log('Request POST /tag/%s', req.params.id)

if (req.body.enabled === 'true') req.body.enabled = true
if (req.body.enabled === 'false') req.body.enabled = false
api.tag.update(req.params.id, req.body, function (err, tagDoc) {
if (err) return _handleError(err, req, res)

log('Serving tag %s', tagDoc.id)
var keys = [
'id hash name color image createdAt'
'id hash name color image createdAt enabled'
].join(' ')

res.status(200).json(expose(keys)(tagDoc.toJSON()))
Expand Down
4 changes: 2 additions & 2 deletions lib/db-api/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.all = function all (params, fn) {
Topic
.find(query)
.select('id topicId mediaTitle tag tags participants votes createdAt updatedAt closingAt publishedAt deletedAt status open closed links author authorUrl forum coverUrl extra action')
.populate('tag', 'id hash name color image')
.populate('tag', 'id hash name color image enabled')
.exec(function (err, topics) {
if (err) {
log('Found error %j', err)
Expand Down Expand Up @@ -183,7 +183,7 @@ exports.vote = function vote (id, user, value, fn) {

Topic
.findOne(query)
.populate('tag', 'id hash name color image')
.populate('tag', 'id hash name color image enabled')
.exec(function (err, topic) {
if (err) {
log('Found error %s', err)
Expand Down
1 change: 1 addition & 0 deletions lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var TagSchema = new Schema({
name: { type: String, trim: true, required: true, maxlength: 50 },
color: { type: String, default: '#091A33', validate: hexColorValidation },
image: { type: String, enum: images, default: images[0] },
enabled: { type: Boolean, default: true },
createdAt: { type: Date, default: Date.now }
})

Expand Down
36 changes: 36 additions & 0 deletions migrations/2000000000029-add-attr-tags-enabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const config = require('lib/config')

const dbReady = require('lib/models').ready

const Tag = require('lib/models').Tag

/**
* Make any changes you need to make to the database here
*/
class SaltearPromises { }
exports.up = function up (done) {
dbReady()
// update all tags to have enabled = true
.then(() => {
console.log(`-- Actualizando tags para que todos tengan enabled = true`)
return Tag.update({}, { enabled: true }, { multi: true })
})
.then(() => {
done()
})
.catch((err) => {
if (err instanceof SaltearPromises)
done()
else{
console.log('-- Error en migration 2000000000029-add-attr-tags-enabled.js:', err)
done(err)
}
})
}

/**
* Make any changes that UNDO the up function side effects here (if possible)
*/
exports.down = function down(done) {
done();
};

0 comments on commit f6a5dd0

Please sign in to comment.