forked from quic-issues/427e7578-d7bf-49c8-aee9-2dd999e25316
* init p2p-poll
This commit is contained in:
152
p2p-poll/app/app.vue
Normal file
152
p2p-poll/app/app.vue
Normal file
@@ -0,0 +1,152 @@
|
||||
|
||||
|
||||
<style>
|
||||
/* Basic styling to make it look clean */
|
||||
body {
|
||||
font-family: system-ui, -apple-system, sans-serif;
|
||||
background-color: #f4f4f9;
|
||||
color: #333;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.poll-container {
|
||||
background: white;
|
||||
padding: 2rem;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
||||
width: 100%;
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1 { margin: 0 0 0.5rem 0; }
|
||||
|
||||
.status {
|
||||
font-size: 0.85rem;
|
||||
color: #666;
|
||||
}
|
||||
.status .connected { color: #10b981; font-weight: bold; }
|
||||
|
||||
.add-option-form {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
input {
|
||||
flex-grow: 1;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
button {
|
||||
background: #3b82f6;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-weight: bold;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
button:hover { background: #2563eb; }
|
||||
|
||||
.poll-list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.poll-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
background: #f8fafc;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.option-name { font-weight: 500; }
|
||||
.vote-section { display: flex; align-items: center; gap: 1rem; }
|
||||
.vote-count { font-size: 0.9rem; color: #475569; }
|
||||
.vote-btn { padding: 0.4rem 0.8rem; background: #10b981; }
|
||||
.vote-btn:hover { background: #059669; }
|
||||
.empty-state { text-align: center; color: #94a3b8; font-style: italic; }
|
||||
</style>
|
||||
<template>
|
||||
<div class="poll-container">
|
||||
<ClientOnly>
|
||||
<header>
|
||||
<h1>P2P Polling App 🗳️</h1>
|
||||
<div class="status">
|
||||
<span :class="{ 'connected': isConnected }">
|
||||
● {{ isConnected ? 'Synced' : 'Connecting...' }}
|
||||
</span>
|
||||
<span> | Peers online: {{ connectedPeers }}</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<form @submit.prevent="handleAddNewOption" class="add-option-form">
|
||||
<input
|
||||
v-model="newOption"
|
||||
type="text"
|
||||
placeholder="Enter a new poll option..."
|
||||
required
|
||||
/>
|
||||
<button type="submit">Add Option</button>
|
||||
</form>
|
||||
|
||||
<div v-if="Object.keys(pollData).length === 0" class="empty-state">
|
||||
No options yet. Be the first to add one!
|
||||
</div>
|
||||
|
||||
<ul v-else class="poll-list">
|
||||
<li v-for="(votes, optionName) in pollData" :key="optionName" class="poll-item">
|
||||
<span class="option-name">{{ optionName }}</span>
|
||||
<div class="vote-section">
|
||||
<span class="vote-count">{{ votes }} {{ votes === 1 ? 'vote' : 'votes' }}</span>
|
||||
<button @click="vote(String(optionName))" class="vote-btn">+1</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</main>
|
||||
<template #fallback>
|
||||
<div class="empty-state">Loading P2P Node...</div>
|
||||
</template>
|
||||
</ClientOnly>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
// Manually define 'global' for the browser
|
||||
if (process.client) {
|
||||
(window as any).global = window;
|
||||
}
|
||||
import { usePoll } from '../composables/usePoll';
|
||||
|
||||
// Because WebRTC requires the browser environment (window/navigator),
|
||||
// we ensure our composable only runs on the client side in Nuxt.
|
||||
const newOption = ref('');
|
||||
|
||||
// Destructure our P2P logic
|
||||
const { pollData, isConnected, connectedPeers, addOption, vote } = usePoll();
|
||||
|
||||
const handleAddNewOption = () => {
|
||||
addOption(newOption.value);
|
||||
newOption.value = ''; // Reset input
|
||||
};
|
||||
</script>
|
||||
Reference in New Issue
Block a user