assign calendar post event creation
This commit is contained in:
parent
a101ae0003
commit
25ecbfc3d3
@ -10,13 +10,15 @@ import { useNDK } from "@/app/_providers/ndk";
|
|||||||
import { NostrEvent } from "@nostr-dev-kit/ndk";
|
import { NostrEvent } from "@nostr-dev-kit/ndk";
|
||||||
import { getTagValues } from "@/lib/nostr/utils";
|
import { getTagValues } from "@/lib/nostr/utils";
|
||||||
import { addMinutesToDate, fromUnix, toUnix } from "@/lib/utils/dates";
|
import { addMinutesToDate, fromUnix, toUnix } from "@/lib/utils/dates";
|
||||||
|
import useCurrentUser from "@/lib/hooks/useCurrentUser";
|
||||||
|
import { nip19 } from "nostr-tools";
|
||||||
const EditListSchema = z.object({
|
const EditListSchema = z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
about: z.string().optional(),
|
about: z.string().optional(),
|
||||||
image: z.string().optional(),
|
image: z.string().optional(),
|
||||||
start: z.string().optional(),
|
start: z.string().optional(),
|
||||||
end: z.string().optional(),
|
end: z.string().optional(),
|
||||||
|
calendar: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
type EditListType = z.infer<typeof EditListSchema>;
|
type EditListType = z.infer<typeof EditListSchema>;
|
||||||
@ -26,6 +28,7 @@ type EditListModalProps = {
|
|||||||
};
|
};
|
||||||
export default function EditListModal({ listEvent }: EditListModalProps) {
|
export default function EditListModal({ listEvent }: EditListModalProps) {
|
||||||
const modal = useModal();
|
const modal = useModal();
|
||||||
|
const { currentUser, calendars } = useCurrentUser();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [sent, setSent] = useState(false);
|
const [sent, setSent] = useState(false);
|
||||||
const { ndk } = useNDK();
|
const { ndk } = useNDK();
|
||||||
@ -50,20 +53,41 @@ export default function EditListModal({ listEvent }: EditListModalProps) {
|
|||||||
async function handleSubmit(listData: EditListType) {
|
async function handleSubmit(listData: EditListType) {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
const dateKeys = ["start", "end"];
|
const dateKeys = ["start", "end"];
|
||||||
const newTags = Object.entries(listData).map(([key, val]) => {
|
const removeKeys = [""];
|
||||||
if (dateKeys.includes(key)) {
|
const newTags = Object.entries(listData)
|
||||||
const unix = toUnix(new Date(val));
|
.map(([key, val]) => {
|
||||||
return [key, unix.toString()];
|
if (dateKeys.includes(key)) {
|
||||||
} else {
|
const unix = toUnix(new Date(val));
|
||||||
return [key, val];
|
return [key, unix.toString()];
|
||||||
}
|
} else if (removeKeys.includes(key)) {
|
||||||
}) as [string, string][];
|
return;
|
||||||
setSent(true);
|
} else {
|
||||||
|
return [key, val];
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean) as [string, string][];
|
||||||
const result = await updateList(
|
const result = await updateList(
|
||||||
ndk!,
|
ndk!,
|
||||||
{ ...listEvent, content: listData.about ?? "" },
|
{ ...listEvent, content: listData.about ?? "" },
|
||||||
newTags,
|
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<EditListType> = {
|
const defaultValues: Partial<EditListType> = {
|
||||||
name:
|
name:
|
||||||
@ -83,6 +107,7 @@ export default function EditListModal({ listEvent }: EditListModalProps) {
|
|||||||
parseInt(getTagValues("end", listEvent.tags) as string),
|
parseInt(getTagValues("end", listEvent.tags) as string),
|
||||||
).toISOString()
|
).toISOString()
|
||||||
: addMinutesToDate(new Date(), 60).toISOString(),
|
: addMinutesToDate(new Date(), 60).toISOString(),
|
||||||
|
calendar: getTagValues("calendar", listEvent.tags),
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -114,6 +139,15 @@ export default function EditListModal({ listEvent }: EditListModalProps) {
|
|||||||
type: "date-time",
|
type: "date-time",
|
||||||
slug: "end",
|
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 ?? {}}
|
defaultValues={defaultValues ?? {}}
|
||||||
formSchema={EditListSchema}
|
formSchema={EditListSchema}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user