2026-01-27 16:21:58 +00:00
|
|
|
---
|
|
|
|
|
export const prerender = false;
|
|
|
|
|
import Layout from '../../../layouts/Layout.astro';
|
|
|
|
|
import { API_BASE } from '../../../lib/api';
|
|
|
|
|
const { slug } = Astro.params;
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
<Layout title="Voting Configuration">
|
2026-01-30 11:27:50 +00:00
|
|
|
<section class="ui-page">
|
|
|
|
|
<div class="ui-container ui-config-container">
|
|
|
|
|
<div class="ui-hero ui-card ui-card-glass">
|
|
|
|
|
<div class="ui-hero-top">
|
|
|
|
|
<div class="ui-hero-title">
|
|
|
|
|
<h1>Voting Configuration</h1>
|
|
|
|
|
<p class="ui-hero-subtitle">Configure voting methods for this community</p>
|
|
|
|
|
</div>
|
|
|
|
|
<a href={`/communities/${slug}`} class="ui-btn ui-btn-secondary">Back to Community</a>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="voting-methods" id="voting-methods">
|
|
|
|
|
<p class="loading">Loading voting methods...</p>
|
|
|
|
|
</div>
|
2026-01-27 16:21:58 +00:00
|
|
|
</div>
|
2026-01-30 11:27:50 +00:00
|
|
|
</section>
|
2026-01-27 16:21:58 +00:00
|
|
|
</Layout>
|
|
|
|
|
|
|
|
|
|
<style>
|
2026-01-30 11:27:50 +00:00
|
|
|
.ui-config-container {
|
|
|
|
|
max-width: 900px;
|
2026-01-27 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.voting-methods {
|
|
|
|
|
display: grid;
|
|
|
|
|
gap: 1.25rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.method-card.disabled {
|
|
|
|
|
opacity: 0.5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.method-header {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: flex-start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.method-info h3 {
|
|
|
|
|
margin: 0 0 0.25rem 0;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.complexity {
|
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
padding: 0.15rem 0.5rem;
|
|
|
|
|
border-radius: 1rem;
|
|
|
|
|
font-weight: normal;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 11:27:50 +00:00
|
|
|
.complexity.simple { background: var(--color-success); color: var(--color-on-primary); }
|
|
|
|
|
.complexity.moderate { background: var(--color-warning); color: var(--color-on-primary); }
|
|
|
|
|
.complexity.advanced { background: var(--color-error); color: var(--color-on-primary); }
|
2026-01-27 16:21:58 +00:00
|
|
|
|
|
|
|
|
.method-info p {
|
|
|
|
|
margin: 0;
|
2026-01-30 11:27:50 +00:00
|
|
|
color: var(--color-text-muted);
|
2026-01-27 16:21:58 +00:00
|
|
|
font-size: 0.9rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.method-controls {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
align-items: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggle-switch {
|
2026-01-30 11:51:48 +00:00
|
|
|
--ui-toggle-width: 48px;
|
|
|
|
|
--ui-toggle-height: 26px;
|
|
|
|
|
--ui-toggle-thumb-size: 20px;
|
2026-01-27 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.default-badge {
|
2026-01-30 11:27:50 +00:00
|
|
|
background: var(--color-primary);
|
|
|
|
|
color: var(--color-on-primary);
|
2026-01-27 16:21:58 +00:00
|
|
|
padding: 0.2rem 0.6rem;
|
|
|
|
|
border-radius: 1rem;
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.loading {
|
|
|
|
|
text-align: center;
|
|
|
|
|
padding: 2rem;
|
2026-01-30 11:27:50 +00:00
|
|
|
color: var(--color-text-muted);
|
2026-01-27 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.error-message {
|
2026-01-30 11:27:50 +00:00
|
|
|
background: var(--color-error-muted);
|
|
|
|
|
border: 1px solid var(--color-error);
|
2026-01-27 16:21:58 +00:00
|
|
|
padding: 1rem;
|
2026-01-30 11:27:50 +00:00
|
|
|
border-radius: var(--radius-md);
|
|
|
|
|
color: var(--color-error);
|
2026-01-27 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<script define:vars={{ slug, API_BASE }}>
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
let communityId = null;
|
|
|
|
|
|
2026-01-28 23:47:14 +00:00
|
|
|
if (!token) {
|
|
|
|
|
window.location.href = `/login?redirect=/communities/${slug}/voting-config`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 16:21:58 +00:00
|
|
|
async function init() {
|
|
|
|
|
try {
|
2026-01-28 23:47:14 +00:00
|
|
|
const res = await fetch(`${API_BASE}/api/communities`);
|
2026-01-27 16:21:58 +00:00
|
|
|
if (!res.ok) {
|
2026-01-28 23:47:14 +00:00
|
|
|
document.getElementById('voting-methods').innerHTML =
|
|
|
|
|
'<p class="error-message">Failed to load community</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const communities = await res.json();
|
|
|
|
|
const community = (communities || []).find(c => c.slug === slug);
|
|
|
|
|
|
|
|
|
|
if (!community) {
|
2026-01-27 16:21:58 +00:00
|
|
|
document.getElementById('voting-methods').innerHTML =
|
|
|
|
|
'<p class="error-message">Community not found</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-28 23:47:14 +00:00
|
|
|
|
2026-01-27 16:21:58 +00:00
|
|
|
communityId = community.id;
|
|
|
|
|
await loadVotingMethods();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
document.getElementById('voting-methods').innerHTML =
|
|
|
|
|
'<p class="error-message">Failed to load community</p>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadVotingMethods() {
|
|
|
|
|
const container = document.getElementById('voting-methods');
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-28 23:47:14 +00:00
|
|
|
const res = await fetch(`${API_BASE}/api/communities/${communityId}/voting-methods`, {
|
|
|
|
|
headers: { 'Authorization': `Bearer ${token}` },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res.status === 401) {
|
|
|
|
|
window.location.href = `/login?redirect=/communities/${slug}/voting-config`;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res.status === 403) {
|
|
|
|
|
container.innerHTML = '<p class="error-message">Admin/moderator access required</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 16:21:58 +00:00
|
|
|
const methods = await res.json();
|
|
|
|
|
|
|
|
|
|
container.innerHTML = methods.map(m => `
|
2026-01-30 11:27:50 +00:00
|
|
|
<div class="method-card ui-card ui-card-soft ui-card-pad-md ${m.is_enabled ? '' : 'disabled'}" data-id="${m.voting_method.id}">
|
2026-01-27 16:21:58 +00:00
|
|
|
<div class="method-header">
|
|
|
|
|
<div class="method-info">
|
|
|
|
|
<h3>
|
|
|
|
|
${m.voting_method.display_name}
|
|
|
|
|
<span class="complexity ${m.voting_method.complexity_level}">${m.voting_method.complexity_level}</span>
|
|
|
|
|
</h3>
|
|
|
|
|
<p>${m.voting_method.description || 'No description'}</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="method-controls">
|
|
|
|
|
${m.is_default ? '<span class="default-badge">Default</span>' : `
|
2026-01-29 18:49:44 +00:00
|
|
|
<button class="ui-btn ui-btn-secondary js-set-default" data-id="${m.voting_method.id}" type="button">
|
2026-01-27 16:21:58 +00:00
|
|
|
Set Default
|
|
|
|
|
</button>
|
|
|
|
|
`}
|
|
|
|
|
<label class="toggle-switch">
|
|
|
|
|
<input type="checkbox"
|
|
|
|
|
data-id="${m.voting_method.id}"
|
|
|
|
|
${m.is_enabled ? 'checked' : ''}>
|
|
|
|
|
<span class="toggle-slider"></span>
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`).join('');
|
|
|
|
|
|
|
|
|
|
setupHandlers();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
container.innerHTML = '<p class="error-message">Failed to load voting methods</p>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setupHandlers() {
|
|
|
|
|
// Toggle handlers
|
|
|
|
|
document.querySelectorAll('.method-card .toggle-switch input').forEach(toggle => {
|
|
|
|
|
toggle.addEventListener('change', async (e) => {
|
|
|
|
|
const methodId = e.target.dataset.id;
|
|
|
|
|
const isEnabled = e.target.checked;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`${API_BASE}/api/communities/${communityId}/voting-methods/${methodId}`, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ is_enabled: isEnabled })
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const err = await res.text();
|
|
|
|
|
alert('Failed: ' + err);
|
|
|
|
|
e.target.checked = !isEnabled;
|
|
|
|
|
} else {
|
|
|
|
|
const card = e.target.closest('.method-card');
|
|
|
|
|
card.classList.toggle('disabled', !isEnabled);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
alert('Failed to update');
|
|
|
|
|
e.target.checked = !isEnabled;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Default handlers
|
2026-01-29 18:49:44 +00:00
|
|
|
document.querySelectorAll('.js-set-default').forEach(btn => {
|
2026-01-27 16:21:58 +00:00
|
|
|
btn.addEventListener('click', async () => {
|
|
|
|
|
const methodId = btn.dataset.id;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`${API_BASE}/api/communities/${communityId}/voting-methods/${methodId}`, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ is_default: true, is_enabled: true })
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
loadVotingMethods();
|
|
|
|
|
} else {
|
|
|
|
|
const err = await res.text();
|
|
|
|
|
alert('Failed: ' + err);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
alert('Failed to set default');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
</script>
|