Feat: News in the client app (#34392)

This commit is contained in:
Stuart Taylor
2018-11-29 12:12:15 +00:00
committed by Valeriy
parent 28798dc008
commit d327a5c36b
87 changed files with 2334 additions and 1403 deletions

View File

@@ -0,0 +1,55 @@
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') });
const { MongoClient } = require('mongodb');
const { createNewsNode } = require('./create-news-node');
const { db } = require('../../../config/secrets');
exports.sourceNodes = function sourceChallengesSourceNodes(
{ actions, reporter },
pluginOptions
) {
function handleError(err, client, reject) {
if (err) {
if (client) {
client.close();
}
reject(err);
reporter.panic(err);
}
}
const { maximumStaticRenderCount = 100 } = pluginOptions;
const { createNode } = actions;
return new Promise((resolve, reject) =>
MongoClient.connect(
db,
{ useNewUrlParser: true },
async function(err, client) {
handleError(err, client, reject);
reporter.info('fcc-source-news connected successfully to mongo');
const db = client.db('freecodecamp');
const articleCollection = db.collection('article');
articleCollection
.aggregate([
{ $match: { featured: true } },
{ $sort: { firstPublishedDate: -1 } },
{ $limit: maximumStaticRenderCount }
])
.toArray((err, articles) => {
handleError(err, client, reject);
articles
.map(article => createNewsNode(article))
.map(node => createNode(node));
client.close();
reporter.info('fcc-source-news mongo connection closed');
return resolve();
});
}
)
);
};