Here is the code:
Meteor.publish('articles.countryList', function(country) {
var c = Country.findOne({name: country});
const cursor = Articles.find({country: c._id},{limit: 5});
return cursor;
});
Meteor.publish('country.all', function () {
const selector = {};
const options = {
// fields: {_id: 1, title: 1},
// sort: {createdAt: -1},
// limit: 10
};
return Country.find(selector, options);
});
Country List
Container (Composer)
import {useDeps, composeWithTracker, composeAll } from 'mantra-core';
import ListCountries from '../../components/home/ListCountries';
export const composer = ({context, clearErrors, country}, onData) => {
const {Meteor, Collections} = context();
if (Meteor.subscribe('country.all').ready()) {
const options = {};
const records = Collections.Country.find().fetch();
onData(null, {records});
} else {
onData();
}
};
export default composeAll(composeWithTracker(composer),useDeps())(ListCountries);
Component
import React from 'react';
import CountryList from '../../containers/home/CountryList';
class ListCountries extends React.Component {
constructor(props) {
super();
this.state = props;
}
render() {
var count = 0;
const countryModules = this.state.records.map(function(country) {
count++;
return <CountryList key={country.name+count} country={country.name} />
});
return (<div>
<h2>View News By Country</h2>
{countryModules}
</div>);
}
}
export default ListCountries;
NewsList (CountryList for Container)
Container
import {useDeps, composeWithTracker, composeAll } from 'mantra-core';
import NewsList from '../../components/home/NewsList';
export const composer = ({context, clearErrors, country}, onData) => {
const {Meteor, Collections} = context();
if (Meteor.subscribe('articles.countryList',country).ready()) {
const options = {};
const records = Collections.Articles.find().fetch();
onData(null, {records});
} else {
onData();
}
};
export default composeAll(composeWithTracker(composer),useDeps())(NewsList);
Component
import React from 'react';
import DOMPurify from 'dompurify';
class NewsList extends React.Component {
constructor(props) {
super();
this.state = props;
}
render() {
var count = 0;
const articles = this.state.records.map(function(article) {
var clean = DOMPurify.sanitize(article.content, {ALLOWED_TAGS: []});
count++;
return <li key={this.state.country + article._id.valueOf() + count}><a href="#"><h4>{article.title}</h4><p>{clean.substring(0,97) + '...'}</p></a></li>
},this);
return (<div className='industryList'>
<h3>{this.state.country || 'Loading'}</h3>
<ul>
{articles}
</ul>
</div>);
}
}
export default NewsList;