diff --git a/frontend/src/components/IdeaCard.tsx b/frontend/src/components/IdeaCard.tsx new file mode 100644 index 0000000..76a11c0 --- /dev/null +++ b/frontend/src/components/IdeaCard.tsx @@ -0,0 +1,38 @@ +// src/components/TweetCard.tsx +import { Button } from "@/components/ui/button"; +import { + Card, + CardContent, + CardFooter, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { Idea } from "@/http/api"; + +interface TweetCardProps { + idea: Idea; + onDelete: (id: string) => void; +} + +export function IdeaCard({ idea, onDelete }: TweetCardProps) { + const handleDelete = () => { + if (idea._id) { + onDelete(idea._id); + } else { + console.error("Idea id is missing."); + } + }; + return ( + + + {idea.title} + + {idea.content} + + + + + ); +} diff --git a/frontend/src/http/api.ts b/frontend/src/http/api.ts index 7208858..1225c1e 100644 --- a/frontend/src/http/api.ts +++ b/frontend/src/http/api.ts @@ -14,6 +14,12 @@ export interface Thread { content: string; } +export interface Idea { + _id: string; + title: string; + content: string; +} + const api = axios.create({ baseURL: conf.backendBaseUrl, headers: { @@ -87,6 +93,18 @@ export const getAllTweets = async () => { } }; +export const getAllIdeas = async () => { + try { + const response = await api.get("/api/v0/posts/ideas"); + return response.data; + } catch (error) { + if (axios.isAxiosError(error) && error.response) { + throw error.response.data; + } + throw error; + } +}; + export const getAllThreads = async () => { try { const response = await api.get("/api/v0/posts/threads"); @@ -110,6 +128,17 @@ export const deleteTweet = async (id: string) => { } }; +export const deleteIdea = async (id: string) => { + try { + await api.delete(`/api/v0/posts/ideas/${id}`); + } catch (error) { + if (axios.isAxiosError(error) && error.response) { + throw error.response.data; + } + throw error; + } +}; + export const deleteThread = async (id: string) => { try { await api.delete(`/api/v0/posts/threads/${id}`); diff --git a/frontend/src/pages/Ideas.tsx b/frontend/src/pages/Ideas.tsx index c40ec85..10b4d42 100644 --- a/frontend/src/pages/Ideas.tsx +++ b/frontend/src/pages/Ideas.tsx @@ -1,26 +1,117 @@ +import { IdeaCard } from "@/components/IdeaCard"; +import { Button } from "@/components/ui/button"; +import { + DropdownMenu, + DropdownMenuCheckboxItem, + DropdownMenuContent, + DropdownMenuLabel, + DropdownMenuSeparator, +} from "@/components/ui/dropdown-menu"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import config from "@/config/config"; +import { deleteIdea, getAllIdeas, Idea } from "@/http/api"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { Loader, PlusCircle } from "lucide-react"; +import { Link } from "react-router-dom"; const Ideas = () => { + const queryClient = useQueryClient(); + + const { data, error, isLoading } = useQuery<{ ideas: Idea[] }, Error>({ + queryKey: ["ideas"], + queryFn: getAllIdeas, + }); + const deleteMutation = useMutation({ + mutationFn: deleteIdea, + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["ideas"], + }); + }, + }); + + const handleDelete = async (id: string) => { + if (config.isDevelopment) { + console.log("Deleting idea with id:", id); + } + + try { + await deleteMutation.mutateAsync(id); + } catch (error) { + console.error("Failed to delete idea:", error); + } + }; + + if (config.isDevelopment) { + console.log(data); + } + + if (isLoading) { return ( - <> -
-
- {/*
-

- Generate Your First Post -

-

- You can generate your first X post using ai. -

- -
*/} -
- - ) +
+ +
+ ); } - - export default Ideas \ No newline at end of file + + return ( + <> +
+
+
+ +
+
+ + All + Active + Draft + +
+ + + Filter by + + + Active + + Draft + + + + + +
+
+
+ +
+ +
+ {data?.ideas && data.ideas.length > 0 + ? [...data.ideas].reverse().map((idea, index) => { + const key = idea._id || index; + if (config.isDevelopment) { + console.log(`Idea Key: ${key}`); + } + return ( + + ); + }) + : error &&
{error.message}
} +
+
+
+ + ); +}; + +export default Ideas;