Refactor post flow, get url info back from server, redirect to final submission form

This commit is contained in:
Nathan Leniz
2015-03-07 17:42:22 +09:00
parent b655cb7532
commit 9bea52d42f
6 changed files with 108 additions and 26 deletions

15
app.js
View File

@ -317,11 +317,15 @@ app.get(
storyController.submitNew storyController.submitNew
); );
app.post( app.get(
'/stories/', '/stories/submit/:newStory',
storyController.storySubmission storyController.preSubmit
); );
app.post(
'/stories/preliminary',
storyController.newStory
);
app.get( app.get(
'/stories/hot', '/stories/hot',
@ -354,11 +358,6 @@ app.post(
storyController.upvote storyController.upvote
); );
app.post(
'/stories/getURLMetaData',
resourcesController.getMetaData
);
/** /**
* Challenge related routes * Challenge related routes
*/ */

View File

@ -264,7 +264,7 @@ module.exports = {
return { return {
_id: elem._id, _id: elem._id,
difficulty: elem.difficulty difficulty: elem.difficulty
} };
}) })
.sort(function(a, b) { .sort(function(a, b) {
return a.difficulty - b.difficulty; return a.difficulty - b.difficulty;
@ -278,7 +278,7 @@ module.exports = {
return { return {
name: elem.name, name: elem.name,
difficulty: elem.difficulty difficulty: elem.difficulty
} };
}) })
.sort(function(a, b) { .sort(function(a, b) {
return a.difficulty - b.difficulty; return a.difficulty - b.difficulty;
@ -290,19 +290,25 @@ module.exports = {
whichEnvironment: function() { whichEnvironment: function() {
return process.env.NODE_ENV; return process.env.NODE_ENV;
}, },
getURLTitle: function(req, res, next) { getURLTitle: function(url, callback) {
var url = req.body.data.url; debug('getURL called initialled');
(function () {
var result = {title: ''}; var result = {title: ''};
request(url, function (error, response, body) { request(url, function (error, response, body) {
debug('request fired');
if (!error && response.statusCode === 200) { if (!error && response.statusCode === 200) {
debug('fetching data');
var $ = cheerio.load(body); var $ = cheerio.load(body);
var title = $('title').text(); var title = $('title').text();
result.title = title; result.title = title;
debug(result); debug('calling callback with', result);
res.json(result); callback(null, result);
} else {
callback('failed');
} }
}); });
})();
} }
}; };

View File

@ -53,6 +53,16 @@ exports.recent = function(req, res, next) {
}); });
}; };
exports.preSubmit = function(req, res, next) {
var data = req.params.newStory;
debug('got presubmission with info', data.url, data.title);
res.render('stories/index', {
page: 'storySubmission',
storyURL: data.url,
storyTitle: data.title
});
};
exports.returnIndividualStory = function(req, res, next) { exports.returnIndividualStory = function(req, res, next) {
var dashedName = req.params.storyName; var dashedName = req.params.storyName;
@ -166,6 +176,26 @@ exports.comments = function(req, res, next) {
}); });
}; };
exports.newStory = function(req, res, next) {
var url = req.body.data.url;
debug('Got new story submission, calling resources with', url);
resources.getURLTitle(url, processResponse);
function processResponse(err, storyTitle) {
if (err) {
res.json({
storyURL: url,
storyTitle: ''
});
} else {
storyTitle = storyTitle ? storyTitle : '';
res.json({
storyURL: url,
storyTitle: storyTitle.title
});
}
}
};
exports.storySubmission = function(req, res, next) { exports.storySubmission = function(req, res, next) {
var data = req.body.data; var data = req.body.data;
var storyLink = data.headline var storyLink = data.headline

View File

@ -15,6 +15,10 @@ block content
if (page === 'recent') if (page === 'recent')
include ./new-stories include ./new-stories
if (page === 'submit') if (page === 'submit')
include ./submit-story include ./preliminary-submit
if (page === 'search') if (page === 'search')
include ./search-stories include ./search-stories
if (page === 'storySubmission')
include ./submit-story
if (page === 'storyShow')
include ./show

View File

@ -0,0 +1,37 @@
.spacer
.col-xs-12
form.form-horizontal.control-label-story-submission#story-submission-form
.col-xs-2.img-story-post
img.img-responsive(src='#{user.profile.picture}')
.col-xs-10
.form-group
.col-xs-1
label.control-label.control-label-story-submission(for='name') Link
.col-xs-11
input#story-url.form-control(placeholder='Paste your link here', name='Link')
.spacer
.form-group
.btn.btn-big.btn-block.btn-primary#preliminary-story-submit Submit
script.
var preliminaryStorySubmit = function preliminaryStorySubmit() {
var storyURL = $('#story-url').val();
console.log(storyURL);
$('#preliminary-story-submit').attr('disabled', 'disabled');
$.post('/stories/preliminary',
{
data: {
url: storyURL
}
})
.fail(function (xhr, textStatus, errorThrown) {
$('#preliminary-story-submit').attr('disabled', false);
})
.done(function (data, textStatus, xhr) {
window.location = '/stories/submit/url=' +
encodeURIComponent(data.storyURL) +
'&title=' + encodeURIComponent(data.storyTitle);
});
}
$('#preliminary-story-submit').on('click', preliminaryStorySubmit);

View File

@ -1,5 +1,8 @@
.spacer .spacer
.col-xs-12 .col-xs-12
script.
var storyURL = !{JSON.stringify(storyURL)};
var storyTitle = !{JSON.stringify(storyTitle)};
form.form-horizontal.control-label-story-submission#story-submission-form form.form-horizontal.control-label-story-submission#story-submission-form
.col-xs-2.img-story-post .col-xs-2.img-story-post
img.img-responsive(src='#{user.profile.picture}') img.img-responsive(src='#{user.profile.picture}')
@ -8,12 +11,15 @@
.col-xs-1 .col-xs-1
label.control-label.control-label-story-submission(for='name') Link label.control-label.control-label-story-submission(for='name') Link
.col-xs-11 .col-xs-11
input.form-control(placeholder='Paste your link here', name='Link') input#story-url.form-control(placeholder='Paste your link here', name='Link')
.form-group .form-group
.col-xs-1 .col-xs-1
label.control-label.control-label-story-submission(for='name') Title label.control-label.control-label-story-submission(for='name') Title
.col-xs-11 .col-xs-11
input.form-control(placeholder='Type a headline for your link here', name='Title') input#story-title.form-control(placeholder='Type a headline for your link here', name='Title')
.spacer .spacer
.form-group .form-group
.btn.btn-big.btn-block.btn-primary#story-submit Submit .btn.btn-big.btn-block.btn-primary#story-submit Submit
script.
$('#story-url').val(storyURL).attr('disabled', 'disabled');
$('#story-title').val(storyTitle);