Kuadratic Voting Component

A Val Town voting widget that provides embeddable HTML voting components with SQLite persistence and vote limiting.

Features

  • Multiple Options: Display multiple voteable options in a single poll
  • Persistent Storage: All votes stored in Val Town's SQLite database
  • Vote Limiting: Configurable limits for total votes and votes per user
  • Embeddable: Export HTML components for easy integration
  • Real-time Updates: Vote counts update immediately after voting

Usage

Basic Example

import { createVotingWidget, initializeOptions } from "./index.ts"; const config = { pollId: "my-poll-2024", maxTotalVotes: 1000, maxVotesPerUser: 3, options: [ { id: "option1", title: "Option 1", description: "First choice" }, { id: "option2", title: "Option 2", description: "Second choice" }, { id: "option3", title: "Option 3" } ] }; // Initialize the poll options in the database await initializeOptions(config); // Generate HTML widget const widgetHTML = createVotingWidget(config);

API Endpoints

The component provides these API endpoints:

  • POST /api/vote - Submit a vote
  • GET /api/results/:pollId - Get current results

Vote Submission

{ "pollId": "my-poll-2024", "optionId": "option1", "voterId": "unique-voter-id", "maxTotalVotes": 1000, "maxVotesPerUser": 3 }

Database Schema

kuadratic_votes_1

  • id - Auto-increment primary key
  • poll_id - Poll identifier
  • option_id - Selected option ID
  • voter_id - Unique voter identifier
  • timestamp - Vote timestamp

kuadratic_options_1

  • id - Option ID (primary key)
  • poll_id - Poll identifier
  • title - Option title
  • description - Optional description
  • vote_count - Current vote count

Configuration

VoteConfig

  • pollId: Unique identifier for the poll
  • maxTotalVotes: Maximum total votes allowed across all users
  • maxVotesPerUser: Maximum votes per individual user
  • options: Array of voting options

Vote Limiting

The component enforces two types of limits:

  1. Global Limit: Total votes across all users
  2. Per-User Limit: Votes per individual (tracked by voter ID)

Integration

The widget generates self-contained HTML with embedded CSS and JavaScript. Simply insert the generated HTML into any webpage to enable voting functionality.

The component automatically:

  • Loads current vote counts on page load
  • Updates counts in real-time after voting
  • Shows success/error messages
  • Prevents voting when limits are reached