Sha256: cfa38612b8c1a5b0f981b46894623e6c54f24c8a6d5c8c88120ee36cfbb47bb7

Contents?: true

Size: 1.98 KB

Versions: 31

Compression:

Stored size: 1.98 KB

Contents

/* eslint-disable require-jsdoc */
// The wait time used to simulate the encryption of the vote during the preview
const FAKE_ENCRYPTION_TIME = 1000;

class PreviewVoteComponent {
  constructor({ electionUniqueId, voterUniqueId }) {
    this.electionUniqueId = electionUniqueId;
    this.voterUniqueId = voterUniqueId;
  }

  async bindEvents({
    onBindEncryptButton,
    onStart,
    onVoteEncryption,
    castOrAuditBallot,
    onBindAuditBallotButton,
    onBindCastBallotButton,
    onAuditBallot,
    onCastBallot,
    onAuditComplete,
    onCastComplete,
    onInvalid
  }) {
    onBindEncryptButton(async () => {
      onStart();
      onVoteEncryption(
        (plainVote) => {
          this.fakeEncrypt(plainVote).then((ballot) => {
            castOrAuditBallot(ballot);
            onBindAuditBallotButton(() => {
              onAuditBallot(
                ballot,
                `${this.voterUniqueId}-election-${this.electionUniqueId}.txt`
              );
              onAuditComplete();
            });

            onBindCastBallotButton(async () => {
              await onCastBallot(ballot);
              onCastComplete();
            });
          });
        },
        () => {
          onInvalid();
        }
      );
    });
  }
  async fakeEncrypt(plainVote) {
    await new Promise((resolve) => setTimeout(resolve, FAKE_ENCRYPTION_TIME));

    return {
      encryptedData: plainVote,
      encryptedDataHash: this.generateHexString(64),
      auditableData: plainVote
    };
  }
  generateHexString(length) {
    return Array(length).
      fill("").
      map(() => Math.random().toString(16).charAt(2)).
      join("");
  }
}

export default function setupVoteComponent($voteWrapper) {
  const voterUniqueId = $voteWrapper.data("voterId");
  const electionUniqueId = $voteWrapper.data("electionUniqueId");

  return new PreviewVoteComponent({
    electionUniqueId,
    voterUniqueId
  });
}

window.Decidim = window.Decidim || {};
window.Decidim.setupVoteComponent = setupVoteComponent;

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
decidim-elections-0.27.9 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.8 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.7 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.6 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.10 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.9 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.5 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.8 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.4 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.3 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.7 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.5 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.2 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.1 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.4 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.0 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.3 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.0.rc2 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.27.0.rc1 app/packs/src/decidim/elections/voter/setup-preview.js
decidim-elections-0.26.2 app/packs/src/decidim/elections/voter/setup-preview.js