Skip to content

Commit

Permalink
fix buscador por nombre
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvaldez committed Jun 6, 2023
1 parent 8515210 commit bcdfc03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
6 changes: 5 additions & 1 deletion ext/lib/api/topics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ app.get('/topics',
years: {
type: 'string',
default: ''
},
kwords: {
type: 'string',
default: ''
}
})
}, { formats }),
utils.findForum,
utils.parseStates,
utils.parseTipoIdea,
utils.parseYears,
utils.parseKWords,
utils.parseZonas,
utils.parseTags,
middlewares.forums.privileges.canView,
Expand All @@ -73,7 +78,6 @@ app.get('/topics',
opts.forum = req.forum
opts.user = req.user
opts.state = opts.tipoIdea
opts.years = opts.years
Promise.all([
utils.findTopics(opts).then(topics => apiNoExt.user.populateOwners(topics)),
utils.findTopicsCount(opts)
Expand Down
11 changes: 9 additions & 2 deletions ext/lib/api/topics/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.parseYears = (req, res, next) => {
next()
}

exports.parseKWords = (req, res, next) => {
req.query.kwords = req.query.kwords.split(' ').filter((t) => !!t)
next()
}

exports.findForum = (req, res, next) => {
api.forums.find({ name: req.query.forumName })
.findOne()
Expand All @@ -51,7 +56,8 @@ const queryTopics = (opts) => {
owners,
zonas,
zona,
years
years,
kwords
} = opts
const query = {
forum: forum._id,
Expand All @@ -65,7 +71,8 @@ const queryTopics = (opts) => {
if (related && related.length > 0) query['attrs.admin-comment-referencia'] = { $regex: `.*${related}.*` }

if (years && years.length > 0) query.$or = years.map(year => ({createdAt: {$gte: new Date(year,0,1),$lte: new Date(year,11,31)}}))

if (kwords && kwords.length > 0) query.mediaTitle = { $in: kwords.map(regex => new RegExp(regex))}
// console.log(kwords)
// if (years && years.length > 0) query.$where = function (){
// const year = this.createdAt.getFullYear().toString()
// return years.includes(year);
Expand Down
50 changes: 17 additions & 33 deletions ext/lib/site/home-catalogo/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ class HomePropuestas extends Component {
noMore: null,

selectedProyecto: null,
searchableProyectos: [],

archive: false,
years: [],
kwords: ''
}


this.handleInputChange = this.handleInputChange.bind(this)
this.renderSortFilter = this.renderSortFilter.bind(this)
this.handleInputSearch = this.handleInputSearch.bind(this)
}

componentDidMount () {
Expand Down Expand Up @@ -114,7 +115,8 @@ class HomePropuestas extends Component {
tags: this.state.tag,
sort: this.state.sort,
tipoIdea: this.state.tipoIdea,
years: this.state.years
years: this.state.years,
kwords: this.state.kwords
}

let queryString = Object.keys(query)
Expand Down Expand Up @@ -152,7 +154,6 @@ class HomePropuestas extends Component {
topics: page == 1 ? topics : prevState.topics.concat(topics),
page: page,
noMore: noMore,
searchableProyectos: topics.map(p => ({label: `${p.mediaTitle}`, value: p.id}))
}))
return topics
})
Expand Down Expand Up @@ -325,32 +326,14 @@ class HomePropuestas extends Component {
)
}

handleSelectedProyecto = (selectedProyecto) => {
//console.log(`Option selected:`, selectedProyecto);

const topicId = selectedProyecto.value
if (this.state.topics.find(t => t.id == selectedProyecto.value) == undefined){
// si el topic no está en la actual lista de resultados lo vamos a buscar
//console.log('Topic not found, searching it:', topicId)
topicStore.getTopic(topicId).then(topic => {
if (!topic)
return

//console.log('Adding topic:', topic.id)
this.setState(prevState => ({
topics: prevState.topics.concat(topic),
selectedProyecto
}));
})
}else{
//console.log('Topic found, filtering it')
this.setState({ selectedProyecto });
}
handleInputSearch = () => {
const {kwords} = this.state
this.fetchTopics();
}

render () {

const { forum, topics, zonas, searchableProyectos, selectedProyecto } = this.state
const { forum, topics, zonas, kwords, selectedProyecto } = this.state
const {archive} = this.props
let filteredTopics;

Expand Down Expand Up @@ -411,17 +394,18 @@ class HomePropuestas extends Component {
<div className='col-md-10 offset-md-1'>
<div className='search-proyecto-wrapper'>
{/* para esto usamos react-select version 2.4.4 */}
<Select
value={selectedProyecto}
onChange={this.handleSelectedProyecto}
options={searchableProyectos}
<input
value={kwords}
onChange={(e) => this.setState({kwords: e.target.value})}
placeholder='Buscá un proyecto por nombre'
isSearchable={true}
className='search-proyecto-select'
className='form-control search-proyecto-select'
/>
<button onClick={()=>this.setState({selectedProyecto: null})} disabled={selectedProyecto ? false : true}>
Limpiar filtro


<button onClick={this.handleInputSearch}>
Buscar
</button>

</div>

{ this.renderSortFilter() }
Expand Down

0 comments on commit bcdfc03

Please sign in to comment.