problem: can't publish new products

This commit is contained in:
gsovereignty
2024-07-05 17:57:06 +08:00
parent 1f38a63e61
commit aa2d9a3075
6 changed files with 184 additions and 14 deletions

View File

@@ -0,0 +1,89 @@
<script lang="ts">
import { Button, buttonVariants } from '$lib/components/ui/button/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
import { Input } from '$lib/components/ui/input/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import { ndk } from '@/ndk';
import Todo from './Todo.svelte';
import { currentUser } from '@/stores/session';
import { Terminal } from 'lucide-svelte';
import * as Alert from '@/components/ui/alert';
import type NDKSvelte from '@nostr-dev-kit/ndk-svelte';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { getRocketURL } from '@/helpers';
import Textarea from '@/components/ui/textarea/textarea.svelte';
export let rocketEvent:NDKEvent;
let name: string;
let desc: string;
let image: string;
function publish(ndk: NDKSvelte) {
if (!ndk.signer) {
throw new Error('no ndk signer found');
}
let e = new NDKEvent(ndk);
let author = $currentUser;
if (!author) {
throw new Error('no current user');
}
e.author = author;
e.kind = 1908;
e.created_at = Math.floor(new Date().getTime() / 1000);
//todo validate d tag
e.tags.push(['name', name]);
e.tags.push(['description', desc]);
e.tags.push(['cover', image])
e.tags.push(['a', `31108:${rocketEvent.pubkey}:${rocketEvent.dTag}`])
e.tags.push(['ruleset', '334000']);
console.log(e.rawEvent())
e.publish().then((x) => {
console.log(x);
//goto(`${base}/rockets/${getRocketURL(e)}`);
});
}
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: 'default' })}>Create a Product</Dialog.Trigger>
<Dialog.Content class="sm:max-w-[425px]">
<Todo text={["validate sane field entries", "name.length > 5 < 20", "description length > 20", "image url resolves and is image"]} />
{#if !currentUser}
<Alert.Root>
<Terminal class="h-4 w-4" />
<Alert.Title>Heads up!</Alert.Title>
<Alert.Description>You need a nostr signing extension to use Nostrocket!</Alert.Description>
</Alert.Root>
{/if}
<Dialog.Header>
<Dialog.Title>Publish a New Product</Dialog.Title>
<Dialog.Description>Create a new product listing for your Rocket!</Dialog.Description>
</Dialog.Header>
<div class="grid gap-4 py-4">
<div class="grid grid-cols-4 items-center gap-4">
<Label for="name" class="text-right">Name</Label>
<Input bind:value={name} id="name" placeholder="Product Name" class="col-span-3" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label for="desc" class="text-right">Description</Label>
<Textarea bind:value={desc} id="desc" placeholder="Description" class="col-span-3" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label for="image" class="text-right">Cover Image</Label>
<Input bind:value={image} id="name" placeholder="URL of cover image" class="col-span-3" />
</div>
</div>
<Dialog.Footer>
<Button
on:click={() => {
publish($ndk);
}}
type="submit">Publish</Button
>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>

View File

@@ -36,7 +36,7 @@
<Badge class="ml-auto flex h-6 w-6 shrink-0 items-center justify-center rounded-full">6</Badge>
</a>
<a
href="##"
href="{base}/products"
class={getClass("products")}
>
<Package class={iconClass} />

View File

@@ -0,0 +1,25 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { Button } from '$lib/components/ui/button/index.js';
import * as Card from '$lib/components/ui/card/index.js';
import { getMission, getRocketURL } from '@/helpers';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { ChevronRight } from 'lucide-svelte';
export let event: NDKEvent;
//$page.url.searchParams.get("tab")
</script>
<Card.Root class="w-[350px]">
<Card.Header>
<Card.Title>{event.getMatchingTags('name')[0][1]}</Card.Title>
<Card.Description>{getMission(event)}</Card.Description>
</Card.Header>
<Card.Content></Card.Content>
<Card.Footer class="flex justify-between">
<Button on:click={()=>{console.log(event.rawEvent())}} variant="outline">Print to Console</Button>
<Button on:click={()=>{goto(`${base}/rockets/${getRocketURL(event)}`)}}>View Full Rocket<ChevronRight class="h-4 w-4" /></Button>
</Card.Footer>
</Card.Root>

View File

@@ -0,0 +1,9 @@
<script lang="ts">
export let title: string;
</script>
<div class="mb-4 flex items-center">
<h1 class="scroll-m-20 text-lg font-semibold tracking-tight md:text-xl">
{title}
</h1>
</div>

View File

@@ -0,0 +1,19 @@
<script lang="ts">
import { ndk } from "@/ndk";
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import type { NDKEventStore } from "@nostr-dev-kit/ndk-svelte";
import { onDestroy } from "svelte";
let entries: NDKEventStore<NDKEvent> | undefined;
onDestroy(() => {
entries?.unsubscribe();
});
entries = $ndk.storeSubscribe([{ kinds: [1908 as number] }], { subId: 'products' });
</script>
{#if entries && $entries}
{#each $entries as e}
<p>{e.tags}</p>
{/each}
{/if}

View File

@@ -7,6 +7,9 @@
import { derived, type Readable } from 'svelte/store';
import Heading from '../../../components/Heading.svelte';
import Todo from '../../../components/Todo.svelte';
import CreateNewProduct from '../../../components/CreateNewProduct.svelte';
import Subheading from '../../../components/Subheading.svelte';
import ProductCard from '../../../components/ProductCard.svelte';
//flow if we only have a d-tag: fetch all 31108's with this d-tag, sort by WoT, put Nostrocket Name Service one at the top. Dedupe same rocket (same state, shadows) from multiple users, just show them all as everyone agreeing.
//second pass: fetch ignition event for each, rebuild current state and validate all proofs, compute votepower and display only the states with > 50%.
@@ -14,10 +17,12 @@
let rName = $page.url.searchParams.get('d');
let rPubkey = $page.url.searchParams.get('p');
let events: NDKEventStore<NDKEvent> | undefined;
let latest: Readable<ExtendedBaseType<NDKEvent> | undefined>;
let rocketEvents: NDKEventStore<NDKEvent> | undefined;
let latestRocketEvent: Readable<ExtendedBaseType<NDKEvent> | undefined>;
let candidateProducts: Readable<ExtendedBaseType<NDKEvent>[]>;
onDestroy(() => {
events?.unsubscribe();
rocketEvents?.unsubscribe();
});
//if we don't have a d/p tags we just render the event provided
@@ -29,33 +34,56 @@
if (rIgnitionOrActual.length == 64 && rName && rPubkey) {
//the user wants the latest valid state of this rocket
events = $ndk.storeSubscribe(
[{ '#d': [rName], authors: [rPubkey], kinds: [31108 as number] }],
rocketEvents = $ndk.storeSubscribe(
[{ '#d': [rName], authors: [rPubkey], kinds: [31108 as number] },
{ '#a': [`31108:${rPubkey}:${rName}`]}],
{ subId: rName }
);
}
$: {
if (events) {
latest = derived(events, ($events) => {
if (events) {
let sorted = $events.toSorted((a, b) => {
if (rocketEvents) {
latestRocketEvent = derived(rocketEvents, ($events) => {
if (rocketEvents) {
let sorted = $events.filter(e=>{
return e.kind == 31108;
})
sorted = sorted.toSorted((a, b) => {
return a.created_at - b.created_at;
});
return sorted[0];
}
return undefined;
});
if ($latestRocketEvent) {
candidateProducts = derived(rocketEvents, ($events) =>{
return $events.filter(e=>{
return e.kind == 1908;
})
})
}
}
}
//todo: handle shadow events (fetch the shadowed event and render it instead)
</script>
{#if latest && $latest}
<Heading title={$latest.getMatchingTags("d")[0][1]} />
<Todo text={["delete rocket (if current user is rocket creator)", "modify relevant data and republish event according to https://github.com/nostrocket/NIPS/blob/main/31108.md and https://github.com/nostrocket/NIPS/blob/main/MSBR334000.md ", ]} />
<p>{$latest.id}</p>
{#if latestRocketEvent && $latestRocketEvent}
<Heading title={$latestRocketEvent.getMatchingTags('d')[0][1]} />
<Todo
text={[
'delete rocket (if current user is rocket creator)',
'modify relevant data and republish event according to https://github.com/nostrocket/NIPS/blob/main/31108.md and https://github.com/nostrocket/NIPS/blob/main/MSBR334000.md '
]}
/>
{#if candidateProducts && $candidateProducts}
<Subheading title="Products" />
<CreateNewProduct rocketEvent={$latestRocketEvent} />
{#each $candidateProducts as r}<ProductCard event={r} />{/each}
{/if}
{:else}
IGNITION: {rIgnitionOrActual} <br />
NAME: {rName} <br />