feat: migrate searchspaces page to use createSearchSpaceMutationAtom

This commit is contained in:
CREDO23 2025-12-12 07:51:00 +00:00
parent 862288367d
commit 4aa026cae4

View file

@ -1,46 +1,25 @@
"use client"; "use client";
import { useAtomValue } from "jotai";
import { motion } from "motion/react"; import { motion } from "motion/react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { SearchSpaceForm } from "@/components/search-space-form"; import { SearchSpaceForm } from "@/components/search-space-form";
import { authenticatedFetch } from "@/lib/auth-utils"; import { createSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-space-mutation.atoms";
export default function SearchSpacesPage() { export default function SearchSpacesPage() {
const router = useRouter(); const router = useRouter();
const createSearchSpace = useAtomValue(createSearchSpaceMutationAtom);
const handleCreateSearchSpace = async (data: { name: string; description?: string }) => { const handleCreateSearchSpace = async (data: { name: string; description?: string }) => {
try { const result = await createSearchSpace.mutateAsync({
const response = await authenticatedFetch( name: data.name,
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces`, description: data.description || "",
{ });
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: data.name,
description: data.description || "",
}),
}
);
if (!response.ok) { // Redirect to the newly created search space's onboarding
toast.error("Failed to create search space"); router.push(`/dashboard/${result.id}/onboard`);
throw new Error("Failed to create search space");
}
const result = await response.json(); return result;
toast.success("Search space created successfully", {
description: `"${data.name}" has been created.`,
});
// Redirect to the newly created search space's onboarding
router.push(`/dashboard/${result.id}/onboard`);
return result;
} catch (error) {
console.error("Error creating search space:", error);
throw error;
}
}; };
return ( return (