feat: migrate dashboard page to use searchSpacesAtom and deleteSearchSpaceMutationAtom

This commit is contained in:
CREDO23 2025-12-12 08:16:00 +00:00
parent 4aa026cae4
commit 734265c645
2 changed files with 10 additions and 25 deletions

View file

@ -1,5 +1,6 @@
"use client"; "use client";
import { useAtomValue } from "jotai";
import { AlertCircle, Loader2, Plus, Search, Trash2, UserCheck, Users } from "lucide-react"; import { AlertCircle, Loader2, Plus, Search, Trash2, UserCheck, Users } from "lucide-react";
import { motion, type Variants } from "motion/react"; import { motion, type Variants } from "motion/react";
import Image from "next/image"; import Image from "next/image";
@ -35,7 +36,8 @@ import {
import { Spotlight } from "@/components/ui/spotlight"; import { Spotlight } from "@/components/ui/spotlight";
import { Tilt } from "@/components/ui/tilt"; import { Tilt } from "@/components/ui/tilt";
import { useUser } from "@/hooks"; import { useUser } from "@/hooks";
import { useSearchSpaces } from "@/hooks/use-search-spaces"; import { searchSpacesAtom } from "@/atoms/search-spaces/search-space-query.atoms";
import { deleteSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-space-mutation.atoms";
import { authenticatedFetch } from "@/lib/auth-utils"; import { authenticatedFetch } from "@/lib/auth-utils";
/** /**
@ -154,7 +156,8 @@ const DashboardPage = () => {
}, },
}; };
const { searchSpaces, loading, error, refreshSearchSpaces } = useSearchSpaces(); const { data: searchSpaces = [], isLoading: loading, error, refetch: refreshSearchSpaces } = useAtomValue(searchSpacesAtom);
const { mutateAsync: deleteSearchSpace } = useAtomValue(deleteSearchSpaceMutationAtom);
// Fetch user details // Fetch user details
const { user, loading: isLoadingUser, error: userError } = useUser(); const { user, loading: isLoadingUser, error: userError } = useUser();
@ -169,29 +172,11 @@ const DashboardPage = () => {
}; };
if (loading) return <LoadingScreen />; if (loading) return <LoadingScreen />;
if (error) return <ErrorScreen message={error} />; if (error) return <ErrorScreen message={error?.message || 'Failed to load search spaces'} />;
const handleDeleteSearchSpace = async (id: number) => { const handleDeleteSearchSpace = async (id: number) => {
// Send DELETE request to the API await deleteSearchSpace({ id });
try { refreshSearchSpaces();
const response = await authenticatedFetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${id}`,
{ method: "DELETE" }
);
if (!response.ok) {
toast.error("Failed to delete search space");
throw new Error("Failed to delete search space");
}
// Refresh the search spaces list after successful deletion
refreshSearchSpaces();
} catch (error) {
console.error("Error deleting search space:", error);
toast.error("An error occurred while deleting the search space");
return;
}
toast.success("Search space deleted successfully");
}; };
return ( return (

View file

@ -8,10 +8,10 @@ import { createSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-spac
export default function SearchSpacesPage() { export default function SearchSpacesPage() {
const router = useRouter(); const router = useRouter();
const createSearchSpace = useAtomValue(createSearchSpaceMutationAtom); const { mutateAsync: createSearchSpace } = useAtomValue(createSearchSpaceMutationAtom);
const handleCreateSearchSpace = async (data: { name: string; description?: string }) => { const handleCreateSearchSpace = async (data: { name: string; description?: string }) => {
const result = await createSearchSpace.mutateAsync({ const result = await createSearchSpace({
name: data.name, name: data.name,
description: data.description || "", description: data.description || "",
}); });