React home and share pages working isomorphically
This commit is contained in:
parent
0b00b190fc
commit
a2148dc00a
21 changed files with 482 additions and 658 deletions
|
@ -2,18 +2,23 @@
|
|||
|
||||
var React = require('react');
|
||||
var nodejsx = require('node-jsx').install({extension: '.jsx'});
|
||||
var Home = React.createFactory(require('../client/index.jsx').Home);
|
||||
var Router = require('react-router');
|
||||
var routes = require('../views/app.jsx').routes;
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
|
||||
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
||||
var recent = [];
|
||||
var recents = [];
|
||||
docs.forEach(function(doc) {
|
||||
recent.push(doc.services[doc._id.split("$$")[0]]);
|
||||
})
|
||||
recents.push(doc.services[doc._id.split("$$")[0]]);
|
||||
});
|
||||
|
||||
var home = Home({recent: recent});
|
||||
res.send('<!doctype html>\n' + React.renderToString(home).replace("</body></html>", "<script>var recent = " + JSON.stringify(recent) + "</script></body></html>"));
|
||||
//res.render('index', { page: "home", recent: docs, error: req.flash('search-error') });
|
||||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
var content = React.renderToString(App({recents: recents}));
|
||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var recents = " + JSON.stringify(recents) + "</script></body></html>"));
|
||||
});
|
||||
}).catch(function(err) {
|
||||
console.log(err)
|
||||
});
|
||||
}
|
|
@ -29,14 +29,14 @@ module.exports = function(req, res, next) {
|
|||
searching = true;
|
||||
services[id].parseUrl(req.body.url).timeout(10000).then(function(result) {
|
||||
if (!result.id) {
|
||||
req.flash('search-error', 'No match found for this link');
|
||||
res.redirect('/');
|
||||
res.json({error:{message:"No match found for url"}});
|
||||
} else {
|
||||
services[id].lookupId(result.id, result.type).then(function(item) {
|
||||
items[id] = item;
|
||||
req.db.matches.save({_id:id + "$$" + result.id, created_at: new Date(), services:items}).then(function() {
|
||||
res.json(item);
|
||||
//res.redirect("/" + id + "/" + result.type + "/" + result.id);
|
||||
setTimeout(function() {
|
||||
res.json(item);
|
||||
}, 1000)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -50,8 +50,7 @@ module.exports = function(req, res, next) {
|
|||
error.status = 500;
|
||||
next(error);
|
||||
} else if (error.status == 404){
|
||||
req.flash('search-error', 'No match found for this link');
|
||||
res.redirect('/');
|
||||
res.json({error:{message:"No match found for url"}});
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
@ -59,7 +58,6 @@ module.exports = function(req, res, next) {
|
|||
}
|
||||
}
|
||||
if (url.host && !searching) {
|
||||
req.flash('search-error', 'No match found for this link');
|
||||
res.redirect('/');
|
||||
res.json({error:{message:"No match found for url"}});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -5,8 +5,9 @@ var util = require('util');
|
|||
|
||||
var browserify = require('connect-browserify');
|
||||
var React = require('react');
|
||||
var Router = require('react-router');
|
||||
var nodejsx = require('node-jsx').install();
|
||||
var Share = React.createFactory(require('../client/share').Share);
|
||||
var routes = require('../views/app.jsx').routes;
|
||||
|
||||
var services = {};
|
||||
|
||||
|
@ -18,7 +19,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach
|
|||
});
|
||||
|
||||
|
||||
module.exports.html = function(req, res, next) {
|
||||
module.exports = function(req, res, next) {
|
||||
var serviceId = req.params.service;
|
||||
var type = req.params.type;
|
||||
var itemId = req.params.id;
|
||||
|
@ -32,11 +33,13 @@ module.exports.html = function(req, res, next) {
|
|||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
||||
if (err) {
|
||||
return next(new Error());
|
||||
} else if (!doc) {
|
||||
return next();
|
||||
}
|
||||
var items = [];
|
||||
var shares = [];
|
||||
for (var docService in Object.keys(services)) {
|
||||
var loopServiceId = Object.keys(services)[docService];
|
||||
items.push(doc.services[loopServiceId]);
|
||||
shares.push(doc.services[loopServiceId]);
|
||||
if (doc.services[loopServiceId].id === undefined) {
|
||||
services[loopServiceId].search(doc.services[serviceId]).timeout(15000).then(function(item) {
|
||||
if (!item.id) {
|
||||
|
@ -52,11 +55,11 @@ module.exports.html = function(req, res, next) {
|
|||
}
|
||||
}
|
||||
|
||||
var items = items.filter(function(item) {
|
||||
var shares = shares.filter(function(item) {
|
||||
return item.service != serviceId;
|
||||
});
|
||||
|
||||
items.sort(function(a, b) {
|
||||
shares.sort(function(a, b) {
|
||||
return !a.id || !b.id;
|
||||
}).sort(function(a, b) {
|
||||
return !a.streamUrl || b.streamUrl;
|
||||
|
@ -64,33 +67,18 @@ module.exports.html = function(req, res, next) {
|
|||
return a.type == "video" && b.type != "video";
|
||||
});
|
||||
|
||||
items.unshift(doc.services[serviceId]);
|
||||
|
||||
var share = Share({items: items});
|
||||
res.send('<!doctype html>\n' + React.renderToString(share).replace("</body></html>", "<script>var items = " + JSON.stringify(items) + "</script></body></html>"));
|
||||
|
||||
// res.render(type, {
|
||||
// page: type,
|
||||
// title: doc.services[serviceId].name + " by " + doc.services[serviceId].artist.name,
|
||||
// matching: doc.services[serviceId],
|
||||
// matches: items,
|
||||
// thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl
|
||||
// });
|
||||
shares.unshift(doc.services[serviceId]);
|
||||
if (req.accepts(['html', 'json']) === 'json') {
|
||||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
||||
res.json({shares:shares});
|
||||
});
|
||||
} else {
|
||||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
var content = React.renderToString(App({shares: shares}));
|
||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.json = function(req, res, next) {
|
||||
var serviceId = req.params.service;
|
||||
var type = req.params.type;
|
||||
var itemId = req.params.id;
|
||||
var promises = [];
|
||||
|
||||
if (!services[serviceId] || (type != "album" && type != "track")) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
||||
res.json(doc);
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue