enable vote when directly visitng page

This commit is contained in:
@s.roertgen 2025-04-30 10:54:24 +02:00
parent a9c3d7e32a
commit c90b5ce0b1
6 changed files with 40 additions and 50 deletions

View file

@ -1,6 +1,2 @@
@import 'tailwindcss';
@plugin "daisyui";
html, body {
height: 100%;
}

View file

@ -9,6 +9,7 @@
let reactions = writable([]);
let reacted = writable(window.localStorage.getItem(event.id));
let reaction = writable({})
async function sendReaction() {
const reactionEvent = new NDKEvent($ndk, {
@ -17,6 +18,7 @@
tags: [['e', event.id]]
});
await reactionEvent.publish();
$reaction = reactionEvent;
const r = await $ndk.fetchEvents({ kinds: [7], '#e': [event.id] });
console.log('r', r);
$reactions = Array.from(r);
@ -39,12 +41,7 @@
}
onMount(async () => {
if (!$user) {
console.log('no user, logging in');
login();
}
const r = await $ndk.fetchEvents({ kinds: [7], '#e': [event.id] });
console.log('r', r);
$reactions = Array.from(r);
});
</script>
@ -55,7 +52,7 @@
{#if $reacted}
<span>👍 {$reactions.length}</span>
<span class="thanks">Danke für deinen Vote!</span>
<button onclick={() => deleteVote()} class="btn">Vote zurückziehen</button>
<!-- <button onclick={() => deleteVote()} class="btn">Vote zurückziehen</button> -->
{:else}
<button onclick={() => sendReaction()} class="like">👍</button>
<span>{$reactions.length}</span>

View file

@ -11,21 +11,23 @@ export async function login() {
const signedUser = await signer.user();
userStore.set(signedUser)
} else {
console.log("no extension")
const storedPrivateKey = window.localStorage.getItem('nostrPrivateKey');
if (storedPrivateKey) {
const privateKey = JSON.parse(storedPrivateKey);
console.log("stored private key", privateKey)
const signer = new NDKPrivateKeySigner(privateKey);
ndk.signer = signer;
const signedUser = await signer.user();
userStore.set(signedUser)
ndk.signer = signer;
console.log("ndk signer", ndk)
ndkStore.set(ndk)
} else {
console.log('No private key found, generating a new one...');
const privateKey = NDKPrivateKeySigner.generate();
const signer = new NDKPrivateKeySigner(privateKey.privateKey);
console.log('Generated Private Key:', privateKey);
ndk.signer = signer;
const signedUser = await signer.user();
userStore.set(signedUser)

View file

@ -4,7 +4,7 @@ import { NDKNip07Signer } from "@nostr-dev-kit/ndk";
export let connected = writable(false);
export const ndk = writable(new NDKSvelte);
export const ndk = writable(new NDKSvelte());
export const ndkReady = derived(ndk, $ndk => $ndk !== null);

View file

@ -5,22 +5,22 @@
import NDKSvelte from '@nostr-dev-kit/ndk-svelte/svelte5';
import { ndk, connected, initializeNDK } from '$lib/stores';
import { browser } from '$app/environment';
import { login } from '$lib';
let { children } = $props();
onMount(async () => {
const nip07signer = new NDKNip07Signer();
if (browser) {
try {
await initializeNDK();
console.log('NDK initialized successfully');
connected.set(true);
} catch (error) {
console.error('Failed to initialize NDK:', error);
}
}
try {
await initializeNDK();
console.log('NDK initialized successfully');
connected.set(true);
await login()
} catch (error) {
console.error('Failed to initialize NDK:', error);
}
}
});
</script>

View file

@ -22,14 +22,6 @@
let comment = '';
let comments = writable([]);
onMount(async () => {
if (!$user) {
console.log('no user, logging in');
login();
}
});
$effect(() => {
if ($ndkReady) {
console.log('ndk ready');
@ -42,24 +34,27 @@
});
</script>
<div class="main-layout mx-auto flex flex-col items-center justify-center w-3/4">
{#await $ndk.fetchEvent(data.id) then question}
{#if question}
<div class="question mb-4 w-full rounded border p-4 text-xl">
<h2>Question:<br />{question.content}</h2>
</div>
<div class="main-layout mx-auto flex w-3/4 flex-col items-center justify-center">
{#key $ndkReady}
{#await $ndk.fetchEvent(data.id) then question}
{#if question}
<div class="question mb-4 w-full rounded border p-4 text-xl">
<h2>Question:<br />{question.content}</h2>
</div>
<div class="w-full flex flex-col items-center justify-center gap-2 mb-2">
<h1 class="text-xl">Ideensammlung</h1>
<textarea class="w-full border p-2" bind:value={comment} placeholder="Mein Kommentar"></textarea>
<button class="btn btn-success" onclick={() => submitComment()}>Absenden</button>
</div>
{:else}
<p>Loading...</p>
{/if}
{:catch error}
<p>Error fetching question: {error.message}</p>
{/await}
<div class="mb-2 flex w-full flex-col items-center justify-center gap-2">
<h1 class="text-xl">Ideensammlung</h1>
<textarea class="w-full border p-2" bind:value={comment} placeholder="Mein Kommentar"
></textarea>
<button class="btn btn-success" onclick={() => submitComment()}>Absenden</button>
</div>
{:else}
<p>Loading...</p>
{/if}
{:catch error}
<p>Error fetching question: {error.message}</p>
{/await}
{/key}
<div class="mx-auto flex w-full flex-col items-center justify-center gap-5">
{#each $comments as event}
@ -83,4 +78,4 @@
border-radius: 20px;
border-color: #ccc;
}
</style>
</style>