From 2e6be2bd6fe139035e0edaae6b2343b3feb115fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Robles?= Date: Sat, 22 Jun 2024 15:10:57 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=F0=9F=90=9E=20Improve/rethink/fix=20L?= =?UTF-8?q?ive=20Preview=20(#450)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parent issue: https://github.com/sequentech/meta/issues/752 --- app.js | 9 ++ avBooth/booth-directive/booth-directive.js | 115 +++++++++++++-------- avBooth/booth.html | 1 + avBooth/booth.js | 1 + 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/app.js b/app.js index c69ac889..bd16739b 100755 --- a/app.js +++ b/app.js @@ -214,6 +214,15 @@ angular.module('voting-booth').config( isPreview: true } }) + .state('election.booth-uuid-preview', { + url: '/:id/uuid-preview-vote', + templateUrl: 'avBooth/booth.html', + controller: "BoothController", + params: { + isPreview: true, + isUuidPreview: true + } + }) .state('election.public.show.home.simple', { template: '
' }) diff --git a/avBooth/booth-directive/booth-directive.js b/avBooth/booth-directive/booth-directive.js index 77b4384b..800fb5a5 100644 --- a/avBooth/booth-directive/booth-directive.js +++ b/avBooth/booth-directive/booth-directive.js @@ -24,6 +24,7 @@ angular.module('avBooth') $rootScope, $timeout, $window, + $location, Authmethod, ConfigService, HmacService, @@ -44,6 +45,7 @@ angular.module('avBooth') // demo booth scope.isDemo = (attrs.isDemo === "true"); scope.isPreview = (attrs.isPreview === "true"); + scope.isUuidPreview = (attrs.isUuidPreview === "true"); scope.documentation = ConfigService.documentation; scope.hasSeenStartScreenInThisSession = false; @@ -888,21 +890,34 @@ angular.module('avBooth') var futureResult = $q.defer(); try { if (scope.isPreview) { - var previewElection = JSON.parse(scope.previewElection || sessionStorage.getItem(parseInt(attrs.electionId))); - var foundElection = previewElection.find( - function (element) {return element.id === parseInt(electionId); - }); - - if (foundElection) { - var ballotBoxData = ElectionCreation.generateBallotBoxResponse(foundElection); - futureResult.resolve({ - data: { - payload: ballotBoxData - } - }); + var previewResult = $q.defer(); + if (scope.isUuidPreview) { + var uuid = $location.search().uuid; + Authmethod.getLivePreview(uuid) + .then(function (res) { + previewResult.resolve(res.data); + }, previewResult.reject); } else { - futureResult.reject("election not found"); + previewResult.resolve(JSON.parse(scope.previewElection || sessionStorage.getItem(parseInt(attrs.electionId)))); } + + previewResult.promise + .then(function onSuccess(previewElection) { + var foundElection = previewElection.find( + function (element) {return element.id === parseInt(electionId); + }); + + if (foundElection) { + var ballotBoxData = ElectionCreation.generateBallotBoxResponse(foundElection); + futureResult.resolve({ + data: { + payload: ballotBoxData + } + }); + } else { + futureResult.reject("election not found"); + } + }, futureResult.reject); } else { var electionPromise = $http.get( scope.baseUrl + "election/" + electionId, @@ -941,7 +956,7 @@ angular.module('avBooth') function execIfAllRetrieved(callback) { - if (!sequentElectionsRetrieved || !iamRetrieved) { + if (!sequentElectionsRetrieved || !iamRetrieved) { return; } callback(); @@ -955,24 +970,9 @@ angular.module('avBooth') var ballotBoxData; var authapiData; - if (scope.isPreview) { - var previewElection = JSON.parse(scope.previewElection || sessionStorage.getItem(parseInt(attrs.electionId))); - var foundElection; - if (electionId === undefined) { - electionId = parseInt(attrs.electionId); - } - - if (previewElection.length === 1) { - foundElection = previewElection[0]; - foundElection.id = foundElection.id || (electionId && parseInt(electionId)); - } else { - foundElection = previewElection.find(function (element) { return element.id === parseInt(electionId); }); - } - authapiData = ElectionCreation.generateAuthapiResponse(foundElection); - ballotBoxData = ElectionCreation.generateBallotBoxResponse(foundElection); - } - - var electionPromise; + var authapiDataFuture = $q.defer(); + var electionFuture = $q.defer(); + var electionPromise = electionFuture.promise; if (!scope.isPreview) { electionPromise = $http.get( scope.baseUrl + "election/" + scope.electionId, @@ -981,15 +981,40 @@ angular.module('avBooth') } ); } else { - var deferredElection = $q.defer(); + var previewResult = $q.defer(); + if (scope.isUuidPreview) { + var uuid = $location.search().uuid; + Authmethod.getLivePreview(uuid) + .then(function (res) { + previewResult.resolve(res.data); + }, previewResult.reject); + } else { + previewResult.resolve(JSON.parse(scope.previewElection || sessionStorage.getItem(parseInt(attrs.electionId)))); + } - deferredElection.resolve({ - data: { - payload: ballotBoxData - } - }); + previewResult.promise + .then(function onSuccess(previewElection) { + var foundElection; + if (electionId === undefined) { + electionId = parseInt(attrs.electionId); + } + + if (previewElection.length === 1) { + foundElection = previewElection[0]; + foundElection.id = foundElection.id || (electionId && parseInt(electionId)); + } else { + foundElection = previewElection.find(function (element) { return element.id === parseInt(electionId); }); + } + authapiData = ElectionCreation.generateAuthapiResponse(foundElection); + ballotBoxData = ElectionCreation.generateBallotBoxResponse(foundElection); + authapiDataFuture.resolve(authapiData); - electionPromise = deferredElection.promise; + electionFuture.resolve({ + data: { + payload: ballotBoxData + } + }); + }); } electionPromise @@ -1171,11 +1196,13 @@ angular.module('avBooth') } else { var deferredAuthEvent = $q.defer(); - deferredAuthEvent.resolve({ - data: { - status: "ok", - events: authapiData - } + authapiDataFuture.promise.then(function (data) { + deferredAuthEvent.resolve({ + data: { + status: "ok", + events: data + } + }); }); authEventPromise = deferredAuthEvent.promise; diff --git a/avBooth/booth.html b/avBooth/booth.html index acceec5a..c5fe2570 100644 --- a/avBooth/booth.html +++ b/avBooth/booth.html @@ -4,6 +4,7 @@ config="{{config}}" is-demo="{{isDemo}}" is-preview="{{isPreview}}" + is-uuid-preview="{{isUuidPreview}}" preview-election="{{previewElection}}" > diff --git a/avBooth/booth.js b/avBooth/booth.js index 815e4e25..d12da70e 100644 --- a/avBooth/booth.js +++ b/avBooth/booth.js @@ -48,6 +48,7 @@ angular var previewElectionParam = ($location.search())['preview-election']; $scope.previewElection = previewElectionParam && decodeURIComponent(previewElectionParam); $scope.isPreview = $stateParams.isPreview || false; + $scope.isUuidPreview = $stateParams.isUuidPreview || false; $scope.electionId = $stateParams.id; $scope.baseUrl = ConfigService.baseUrl; $scope.config = $filter('json')(ConfigService);