From 2cc616fc664db976d687cace8632988a1e3a8c48 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 17 Apr 2015 12:25:28 -0300 Subject: [PATCH] Require login to send a new proposal --- index.html | 33 ++++++++++++++++++++++++++------- js/main.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/index.html b/index.html index 97e744e..13667c6 100644 --- a/index.html +++ b/index.html @@ -80,7 +80,9 @@
Qual a sua sugestão para melhorar este programa?
Ela se tornará pública em até 24 horas após o envio e poderá ser apoiada por outros participantes.
Envie Sua Proposta
+
Login
+
@@ -89,7 +91,6 @@
-
@@ -129,13 +130,31 @@ -
+ - +
diff --git a/js/main.js b/js/main.js index e9b161d..ef5c908 100644 --- a/js/main.js +++ b/js/main.js @@ -5,12 +5,15 @@ var templateSource = document.getElementById('proposal-template').innerHTML; var template = Handlebars.compile(templateSource); var supportProposalTemplate = Handlebars.compile(document.getElementById('support-proposal-template').innerHTML); +var loginTemplate = Handlebars.compile(document.getElementById('login').innerHTML); // The div/container that we are going to display the results in var resultsPlaceholder = document.getElementById('proposal-result'); var topics; +var logged_in = false; + var participa = true; if(participa){ var host = 'http://www.participa.br'; @@ -31,6 +34,7 @@ $.getJSON(noosferoAPI) data['host'] = host; data['private_token'] = private_token; resultsPlaceholder.innerHTML = template(data); + $('.login-container').html(loginTemplate()); //Actions for links $( '#nav-proposal-categories a' ).click(function(event){ //Display the category tab @@ -62,6 +66,10 @@ $.getJSON(noosferoAPI) $('.proposal-detail').hide(); $('#' + item).show(); + $('.send-proposal-button').show(); + $('.make-proposal-form').hide(); + $('.login-container').hide(); + var topic_id = this.id.replace('\#',''); loadRandomProposal(topic_id, private_token); }); @@ -82,35 +90,32 @@ $.getJSON(noosferoAPI) } event.preventDefault(); }); - $( '.send-proposal-button a' ).click(function(event){ - //Display Topics or Discussion by category - $('.make-proposal-form').show(); + $( '.send-proposal-button a, .success-proposal-sent a' ).click(function(event){ + //display form to send proposal (or login form for non-logged users) $('.send-proposal-button').hide(); - event.preventDefault(); - }); - $( '.success-proposal-sent a' ).click(function(event){ - //Display Topics or Discussion by category - $('.make-proposal-form').show(); $('.success-proposal-sent').hide(); + loginCallback(logged_in); event.preventDefault(); }); $('.make-proposal-form').submit(function (e) { e.preventDefault(); var proposal_id = this.id.split('-').pop(); + var form = this; $.ajax({ type: 'post', url: host + '/api/v1/articles/' + proposal_id + '/children', - data: $('#'+this.id).serialize() + data: $('#'+this.id).serialize() + "&private_token="+private_token }) .done(function( data ) { - this.reset(); + form.reset(); $('.make-proposal-form').hide(); $('.success-proposal-sent').show(); }) .fail(function( jqxhr, textStatus, error ) { var err = textStatus + ", " + error; console.log( "Request Failed: " + err ); + $('.make-proposal-form .message').text('Não foi possível enviar sua proposta.'); }); }); @@ -149,6 +154,36 @@ function loadRandomProposal(topic_id, private_token) { }); } +function loginCallback(loggedIn, token) { + logged_in = loggedIn; + $('.login .message').text(''); + + if(logged_in) { + if(token) private_token = token; + $('.make-proposal-form').show(); + $('.make-proposal-form .message').text(''); + $('.login-container').hide(); + } else { + $('.make-proposal-form').hide(); + $('.login-container').show(); + } +} + function oauthPluginHandleLoginResult(loggedIn, token) { - private_token = token; + loginCallback(loggedIn, token); } + +jQuery(document).ready(function($) { + $(document).on('click', '.login-action', function(e) { + $.ajax({ + type: 'post', + url: host + '/api/v1/login', + data: $(this).parents('.login').serialize(), + }).done(function(data) { + loginCallback(true, data.private_token); + }).fail(function(data) { + $('.login .message').text('Não foi possível logar'); + }); + e.preventDefault(); + }); +}); -- libgit2 0.21.2