Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Findeton committed Jul 4, 2024
1 parent 61087d1 commit 5475261
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<!-- top navbar -->
<a href="#main-content" class="skip-link">{{ 'avBooth.skipLinks.skipToMain' | i18next }}</a>
<a href="#footer" class="skip-link">{{ 'avBooth.skipLinks.skipToFooter' | i18next }}</a>
<div avb-common-header></div>
<div avb-watermark></div>

<main class="container" id="main-content">
Felix test
<div
av-booth-children-elections
mode="toggle-and-callback"
callback="chooseElection(electionId)"
parent-election-id="{{parentAuthEvent.id}}"
hide-parent="true"
can-vote="false"
has-voted="hasVoted"
children-election-info="childrenElectionInfo"
show-skipped-elections="showSkippedElections"
skipped-elections="skippedElections"
parent-election="parentElection"
></div>
</main>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,186 @@ angular.module('avBooth')
.directive('avbVoterEligibilityScreen', function($window, $timeout, $q, $modal, ConfigService) {

function link(scope, element, attrs) {
scope.showSkippedElections = false;
scope.organization = ConfigService.organization;
function findElectionCredentials(electionId, credentials) {
return _.find(
credentials,
function (credential) {
return credential.electionId === electionId;
}
);
}

function calculateCanVote(elCredentials) {
return (
!!elCredentials &&
!!elCredentials.token &&
(
elCredentials.numSuccessfulLogins < elCredentials.numSuccessfulLoginsAllowed ||
elCredentials.numSuccessfulLoginsAllowed === 0
)
);
}

function calculateIsVoter(elCredentials) {
return (
!!elCredentials &&
elCredentials.numSuccessfulLoginsAllowed !== -1
);
}

function getElectionCredentials() {
// need to reload in case this changed in success screen..
var credentialsStr = $window.sessionStorage.getItem("vote_permission_tokens");
return JSON.parse(credentialsStr);
}

function isChooserDisabled() {
return (
scope.parentElection &&
scope.parentElection.presentation &&
scope.parentElection.presentation.extra_options &&
!!scope.parentElection.presentation.extra_options.disable__election_chooser_screen
);
}

function generateChildrenInfo() {
var childrenInfo = angular.copy(
scope.parentAuthEvent.children_election_info
);

// need to reload in case this changed in success screen..
var credentials = getElectionCredentials();

// if it's a demo, yes, allow voting by default
scope.canVote = scope.isDemo || scope.isPreview;
scope.hasVoted = false;
scope.skippedElections = [];
childrenInfo.presentation.categories = _.map(
childrenInfo.presentation.categories,
function (category) {
category.events = _.map(
category.events,
function (election) {
var elCredentials = findElectionCredentials(
election.event_id, credentials
);
var canVote = calculateCanVote(elCredentials);
var isVoter = calculateIsVoter(elCredentials);
if (
elCredentials &&
elCredentials.numSuccessfulLogins > 0
) {
scope.hasVoted = true;
}
var retValue = Object.assign(
{},
election,
elCredentials || {},
{
disabled: (!scope.isDemo && !scope.isPreview && !canVote),
hidden: (!scope.isDemo && !scope.isPreview && !isVoter)
}
);
if (!!retValue.skipped) {
scope.skippedElections.push(retValue);
}
return retValue;
}
);
return category;
});
return childrenInfo;
}

function getChildrenElectionsData() {
if (!scope.childrenElectionInfo || isChooserDisabled()) {
return;
}

_.map(
scope.childrenElectionInfo.presentation.categories,
function (category) {
_.map(
category.events,
function (event) {
if (event.hidden) {
return {};
}
return scope.simpleGetElection(event.event_id).then(
function (electionData) {
event.electionData = electionData;
$timeout(function () {
scope.$apply();
});
}
);
}
);
}
);
}

function chooseElection(electionId) {
scope.setState(scope.stateEnum.receivingElection, {});
scope.retrieveElectionConfig(electionId + "");
}

scope.childrenElectionInfo = generateChildrenInfo();
getChildrenElectionsData();

function checkDisabled() {
// if election chooser is disabled and can vote, then go to the first
// election in which it can vote
if (isChooserDisabled()) {
var orderedElectionIds = scope
.childrenElectionInfo
.natural_order;
// If it's a demo booth, do not rely on election credentials
if (scope.isDemo || scope.isPreview) {
scope.increaseDemoElectionIndex();
if (scope.demoElectionIndex < orderedElectionIds.length) {
chooseElection(
orderedElectionIds[scope.demoElectionIndex]
);
} else {
scope.hasVoted = true;
scope.canVote = false;
}
return;
}

var credentials = getElectionCredentials();
for (var i = 0; i < orderedElectionIds.length; i++) {
var electionId = orderedElectionIds[i];
var elCredentials = findElectionCredentials(
electionId,
credentials
);
if (
!elCredentials.skipped &&
!elCredentials.voted &&
calculateCanVote(elCredentials)
) {
chooseElection(electionId);
return;
}
}
// If redirected to no election but there are skipped elections, it
// means that the voter can re-login to vote again so we set the
// showSkippedElections flag
if (scope.skippedElections.length > 0) {
scope.showSkippedElections = true;
}
}
}

checkDisabled();
scope.chooseElection = chooseElection;
scope.goToVoterEligibility = function () {
scope.setState(scope.stateEnum.voterEligibilityScreen, {});
};
}
return {
restrict: 'AE',
Expand Down

0 comments on commit 5475261

Please sign in to comment.