import { Button } from "@minmon/ui/components/button";
import { useForm } from "@tanstack/react-form";
import { useMutation, useQuery } from "@tanstack/react-query";
import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { useEffect } from "react";
import { toast } from "sonner";
import { DetailCard, FormCard, PageSection, QueryStateCard, SelectField, TextAreaField, TextField } from "@/components/dashboard-ui";
import { queryClient, trpc, trpcClient } from "@/utils/trpc";
export const Route = createFileRoute("/dashboard/notes_/$noteId")({
component: RouteComponent,
});
type NoteFormValues = {
serverId: string;
title: string;
category: string;
content: string;
};
function RouteComponent() {
const { noteId } = Route.useParams();
const navigate = useNavigate();
const note = useQuery(trpc.notes.byId.queryOptions({ id: noteId }));
const servers = useQuery(trpc.servers.list.queryOptions());
const updateNote = useMutation({
mutationFn: async (values: NoteFormValues) =>
trpcClient.notes.update.mutate({
id: noteId,
serverId: values.serverId,
title: values.title,
category: values.category || null,
content: values.content,
}),
onSuccess: async () => {
toast.success("Note updated");
await queryClient.invalidateQueries();
},
onError: (error) => toast.error(error.message),
});
const defaultValues: NoteFormValues = {
serverId: "",
title: "",
category: "",
content: "",
};
const form = useForm({
defaultValues,
onSubmit: async ({ value }) => {
await updateNote.mutateAsync(value);
},
});
useEffect(() => {
if (!note.data) {
return;
}
form.reset({
serverId: note.data.serverId,
title: note.data.title,
category: note.data.category ?? "",
content: note.data.content,
});
}, [note.data, form]);
if (note.isLoading || servers.isLoading) {
return