flockstr/components/Modals/EditEvent.tsx

159 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-10-24 16:34:44 -04:00
import { useEffect, useState } from "react";
import FormModal from "./FormModal";
import { z } from "zod";
import useEvents from "@/lib/hooks/useEvents";
import { updateList } from "@/lib/actions/create";
import { unixTimeNowInSeconds } from "@/lib/nostr/dates";
import { useModal } from "@/app/_providers/modal/provider";
import { toast } from "sonner";
import { useNDK } from "@/app/_providers/ndk";
import { NostrEvent } from "@nostr-dev-kit/ndk";
import { getTagValues } from "@/lib/nostr/utils";
2023-11-01 16:54:16 -04:00
import { addMinutesToDate, fromUnix, toUnix } from "@/lib/utils/dates";
2023-11-02 15:23:45 -04:00
import useCurrentUser from "@/lib/hooks/useCurrentUser";
import { nip19 } from "nostr-tools";
2023-10-24 16:34:44 -04:00
const EditListSchema = z.object({
name: z.string(),
2023-10-31 10:26:17 -04:00
about: z.string().optional(),
2023-10-24 16:34:44 -04:00
image: z.string().optional(),
2023-11-01 16:54:16 -04:00
start: z.string().optional(),
end: z.string().optional(),
2023-11-02 15:23:45 -04:00
calendar: z.string().optional(),
2023-10-24 16:34:44 -04:00
});
type EditListType = z.infer<typeof EditListSchema>;
type EditListModalProps = {
listEvent: NostrEvent;
};
export default function EditListModal({ listEvent }: EditListModalProps) {
const modal = useModal();
2023-11-02 15:23:45 -04:00
const { currentUser, calendars } = useCurrentUser();
2023-10-24 16:34:44 -04:00
const [isLoading, setIsLoading] = useState(false);
const [sent, setSent] = useState(false);
const { ndk } = useNDK();
const { events } = useEvents({
filter: {
kinds: [listEvent.kind as number],
authors: [listEvent.pubkey],
since: unixTimeNowInSeconds() - 10,
limit: 1,
},
enabled: sent,
});
useEffect(() => {
if (events.length) {
console.log("Done!");
setIsLoading(false);
2023-10-31 10:26:17 -04:00
toast.success("Event Updated!");
2023-10-24 16:34:44 -04:00
modal?.hide();
}
}, [events]);
async function handleSubmit(listData: EditListType) {
setIsLoading(true);
2023-11-01 16:54:16 -04:00
const dateKeys = ["start", "end"];
2023-11-02 15:23:45 -04:00
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][];
2023-10-24 16:34:44 -04:00
const result = await updateList(
ndk!,
2023-10-31 10:26:17 -04:00
{ ...listEvent, content: listData.about ?? "" },
2023-10-24 16:34:44 -04:00
newTags,
);
2023-11-02 15:27:00 -04:00
if (listData.calendar !== getTagValues("calendar", listEvent.tags)) {
2023-11-02 15:23:45 -04:00
const selectedCalendar = Array.from(calendars)
.find((option) => option.tagId() === listData.calendar)
?.rawEvent();
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);
2023-10-24 16:34:44 -04:00
}
const defaultValues: Partial<EditListType> = {
name:
getTagValues("title", listEvent.tags) ??
getTagValues("name", listEvent.tags),
image:
getTagValues("image", listEvent.tags) ??
getTagValues("picture", listEvent.tags),
2023-10-31 10:26:17 -04:00
about: listEvent.content,
2023-11-01 16:54:16 -04:00
start: getTagValues("start", listEvent.tags)
? fromUnix(
parseInt(getTagValues("start", listEvent.tags) as string),
).toISOString()
: new Date().toISOString(),
end: getTagValues("end", listEvent.tags)
? fromUnix(
parseInt(getTagValues("end", listEvent.tags) as string),
).toISOString()
: addMinutesToDate(new Date(), 60).toISOString(),
2023-11-02 15:23:45 -04:00
calendar: getTagValues("calendar", listEvent.tags),
2023-10-24 16:34:44 -04:00
};
return (
<FormModal
2023-10-31 10:26:17 -04:00
title="Edit Event"
2023-10-24 16:34:44 -04:00
fields={[
{
label: "Name",
type: "input",
slug: "name",
},
{
2023-10-31 10:26:17 -04:00
label: "About",
type: "text-area",
slug: "about",
2023-10-24 16:34:44 -04:00
},
{
2023-10-31 10:26:17 -04:00
label: "Image",
type: "upload",
slug: "image",
2023-10-24 16:34:44 -04:00
},
2023-11-01 16:54:16 -04:00
{
label: "Start",
type: "date-time",
slug: "start",
},
{
label: "End",
type: "date-time",
slug: "end",
},
2023-11-02 15:23:45 -04:00
{
label: "Link Calendar",
type: "select",
slug: "calendar",
options: Array.from(calendars).map((o) => ({
value: o.tagId(),
label: getTagValues("name", o.tags) as string,
})),
},
2023-10-24 16:34:44 -04:00
]}
defaultValues={defaultValues ?? {}}
formSchema={EditListSchema}
onSubmit={handleSubmit}
isSubmitting={isLoading}
cta={{
text: "Save Changes",
}}
/>
);
}