diff --git a/components/Modals/EditEvent.tsx b/components/Modals/EditEvent.tsx index 0f4431d..942b154 100644 --- a/components/Modals/EditEvent.tsx +++ b/components/Modals/EditEvent.tsx @@ -10,13 +10,15 @@ import { useNDK } from "@/app/_providers/ndk"; import { NostrEvent } from "@nostr-dev-kit/ndk"; import { getTagValues } from "@/lib/nostr/utils"; import { addMinutesToDate, fromUnix, toUnix } from "@/lib/utils/dates"; - +import useCurrentUser from "@/lib/hooks/useCurrentUser"; +import { nip19 } from "nostr-tools"; const EditListSchema = z.object({ name: z.string(), about: z.string().optional(), image: z.string().optional(), start: z.string().optional(), end: z.string().optional(), + calendar: z.string().optional(), }); type EditListType = z.infer; @@ -26,6 +28,7 @@ type EditListModalProps = { }; export default function EditListModal({ listEvent }: EditListModalProps) { const modal = useModal(); + const { currentUser, calendars } = useCurrentUser(); const [isLoading, setIsLoading] = useState(false); const [sent, setSent] = useState(false); const { ndk } = useNDK(); @@ -50,20 +53,41 @@ export default function EditListModal({ listEvent }: EditListModalProps) { async function handleSubmit(listData: EditListType) { setIsLoading(true); const dateKeys = ["start", "end"]; - const newTags = Object.entries(listData).map(([key, val]) => { - if (dateKeys.includes(key)) { - const unix = toUnix(new Date(val)); - return [key, unix.toString()]; - } else { - return [key, val]; - } - }) as [string, string][]; - setSent(true); + const removeKeys = [""]; + const newTags = Object.entries(listData) + .map(([key, val]) => { + if (dateKeys.includes(key)) { + const unix = toUnix(new Date(val)); + return [key, unix.toString()]; + } else if (removeKeys.includes(key)) { + return; + } else { + return [key, val]; + } + }) + .filter(Boolean) as [string, string][]; const result = await updateList( ndk!, { ...listEvent, content: listData.about ?? "" }, newTags, ); + console.log("listData.calendar", listData.calendar); + if (listData.calendar) { + const selectedCalendar = Array.from(calendars) + .find((option) => option.tagId() === listData.calendar) + ?.rawEvent(); + console.log("selectedCalendar", selectedCalendar); + + if (selectedCalendar) { + const encodedEvent = nip19.naddrEncode({ + identifier: getTagValues("d", listEvent.tags) as string, + kind: listEvent.kind as number, + pubkey: listEvent.pubkey, + }); + await updateList(ndk!, selectedCalendar, [["a", encodedEvent]]); + } + } + setSent(true); } const defaultValues: Partial = { name: @@ -83,6 +107,7 @@ export default function EditListModal({ listEvent }: EditListModalProps) { parseInt(getTagValues("end", listEvent.tags) as string), ).toISOString() : addMinutesToDate(new Date(), 60).toISOString(), + calendar: getTagValues("calendar", listEvent.tags), }; return ( @@ -114,6 +139,15 @@ export default function EditListModal({ listEvent }: EditListModalProps) { type: "date-time", slug: "end", }, + { + label: "Link Calendar", + type: "select", + slug: "calendar", + options: Array.from(calendars).map((o) => ({ + value: o.tagId(), + label: getTagValues("name", o.tags) as string, + })), + }, ]} defaultValues={defaultValues ?? {}} formSchema={EditListSchema}