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'; @import 'tailwindcss';
@plugin "daisyui"; @plugin "daisyui";
html, body {
height: 100%;
}

View file

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

View file

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

View file

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

View file

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

View file

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