From 83c236e9d1d965b05dca89e8586f34ea8b0fe9de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@sedlistka.cz>
Date: Fri, 30 Aug 2019 01:24:29 +0200
Subject: [PATCH] Podpora permalinku

---
 lib/RVVote.pm                       |  1 +
 lib/RVVote/Controller/Evaluation.pm |  3 ++
 lib/RVVote/Controller/RV.pm         | 25 ++++++++++++--
 templates/result.html.ep            | 16 +--------
 templates/rv_form.html.ep           | 52 +++++++++++++++++------------
 5 files changed, 57 insertions(+), 40 deletions(-)

diff --git a/lib/RVVote.pm b/lib/RVVote.pm
index de3f086..702ef86 100644
--- a/lib/RVVote.pm
+++ b/lib/RVVote.pm
@@ -13,6 +13,7 @@ sub startup {
 
   # Normal route to controller
   $r->get('/')->to('RV#index');
+  $r->get('/:election')->to('RV#index');
   $r->get('/api/members/')->to('RV#members');
   $r->post('/api/calculate/')->to('evaluation#process');
 }
diff --git a/lib/RVVote/Controller/Evaluation.pm b/lib/RVVote/Controller/Evaluation.pm
index 59f4654..233ee4e 100644
--- a/lib/RVVote/Controller/Evaluation.pm
+++ b/lib/RVVote/Controller/Evaluation.pm
@@ -1,5 +1,6 @@
 package RVVote::Controller::Evaluation;
 use Mojo::Base 'Mojolicious::Controller';
+use MIME::Base64 qw(encode_base64url);
 use YAML;
 
 sub process {
@@ -9,6 +10,8 @@ sub process {
 
     my $r;
 
+    $c->stash->{permalink} = encode_base64url( $c->req->body );
+
     BALLOT:
     foreach my $ballot ( @{ $c->req->json } ) {
 
diff --git a/lib/RVVote/Controller/RV.pm b/lib/RVVote/Controller/RV.pm
index 2acfe39..c1def3a 100644
--- a/lib/RVVote/Controller/RV.pm
+++ b/lib/RVVote/Controller/RV.pm
@@ -1,6 +1,8 @@
 package RVVote::Controller::RV;
 use Mojo::Base 'Mojolicious::Controller';
 use RVVote::GraphAPI;
+use MIME::Base64 qw(decode_base64url);
+use JSON;
 
 sub index {
     my $c = shift;
@@ -9,9 +11,26 @@ sub index {
 
 sub members {
     my $c = shift;
-    
-    my $gapi = RVVote::GraphAPI->new($c->config->{graph_api}{url});
-    my @members = $gapi->get_group_members($c->config->{graph_api}{rv_gid});
+   
+    my @members = ();
+
+    if ($c->param('election')) {
+        eval {
+            my $election = decode_json(decode_base64url($c->param('election')));
+            @members = map {
+                {
+                    fullname =>	$_->{voter},
+                    value    =>	$_->{value},
+                    user_id  => $_->{id},
+                }
+            } @{ $election }
+        };
+    } 
+
+    if ( ! scalar @members ) {
+        my $gapi = RVVote::GraphAPI->new($c->config->{graph_api}{url});
+        @members = $gapi->get_group_members($c->config->{graph_api}{rv_gid});
+    }
 
     $c->render( json => \@members );
 }
diff --git a/templates/result.html.ep b/templates/result.html.ep
index 5480a45..e9e5e87 100644
--- a/templates/result.html.ep
+++ b/templates/result.html.ep
@@ -1,18 +1,3 @@
-<!--
-% if (scalar @{ $c->stash->{ballots}} ) {
-<div class="card bg-light">
-  <div class="card-header">Platné listky: <%= scalar @{$c->stash->{ballots}} %></div>
-  <div class="card-body">
-    <div class="card-text">
-% foreach my $ballot ( @{ $c->stash->{ballots}} ) {
-%= include 'layouts/ballot',  ballot => $ballot 
-% }
-    </div>
-  </div>
-</div>
-% }
--->
-
 % foreach my $log ( @{ $c->stash->{log}} ) {
 %= include 'layouts/log',  log => $log 
 % }
@@ -27,3 +12,4 @@
 </div>
 % }
 
+<p><a href="/<%= $c->stash->{permalink} %>">permalink</a></p>
diff --git a/templates/rv_form.html.ep b/templates/rv_form.html.ep
index 8522fc3..5a326b5 100644
--- a/templates/rv_form.html.ep
+++ b/templates/rv_form.html.ep
@@ -5,7 +5,7 @@
   <div class="row">
     <div class="col-5">
 
-      <table id="Members" data-toggle="table" data-url="/api/members/" data-classes="table table-borderless table-sm" data-show-header="false">
+      <table id="Members" data-toggle="table" data-url="/api/members/<%= $c->stash->{election} ? '?election='.$c->stash->{election}:'' %>" data-classes="table table-borderless table-sm" data-show-header="false">
       <thead>
         <tr>
           <th data-field="fullname">Člen RV</th>
@@ -24,34 +24,42 @@
 <script>
 
 function votes_detail(value, record) {
-    return '<input class="votes" name="votes_'+value+'" type="text" size="16" data-fullname="'+record.fullname+'">';
+    return '<input class="votes" name="votes_'+value+'" type="text" size="16" data-fullname="'+record.fullname+'" value="'+ (record.value || '' )+'">';
 }
 
-$('#Members').on('post-body.bs.table', function (data) {
-
-    $('input.votes').on('keyup', function () {
-        var bills = [];
-        var a = $('form').serializeArray();
-        $.each(a, function() {
-            var id = this.name.split('_');
-            bills.push({ 
-                "id": id[1], 
-                "value": this.value, 
-                "voter": $('input[name='+this.name+']').attr('data-fullname'),
-            });
+function evaluate() {
+    var bills = [];
+    var a = $('form').serializeArray();
+    $.each(a, function() {
+        var id = this.name.split('_');
+        bills.push({ 
+            "id": id[1], 
+            "value": this.value, 
+            "voter": $('input[name='+this.name+']').attr('data-fullname'),
         });
+    });
+
+    $.ajax({
+        url: '/api/calculate/',                 
+        data: JSON.stringify(bills),
+        contentType: 'application/json',
+        type: "POST",
+    }).done(function( html ) {
+        $( "#Result" ).html( html );
+    })
+}
+
 
-        $.ajax({
-            url: '/api/calculate/',                 
-            data: JSON.stringify(bills),
-            contentType: 'application/json',
-            type: "POST",
-        }).done(function( html ) {
-            $( "#Result" ).html( html );
-        })
+$('#Members').on('post-body.bs.table', function (data) {
 
+
+    $('input.votes').on('keyup', function () {
+        evaluate();
     });
 
+% if ($c->stash->{election}) {
+    evaluate();
+% }
 });
 
 
-- 
GitLab