From 0356fa331281717668a7e70292b70385f075f7fc Mon Sep 17 00:00:00 2001 From: warmachine028 <75939390+warmachine028@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:26:46 +0530 Subject: [PATCH] feat: controller & routes in server --- client/src/api/posts.ts | 45 ++++++------------------ client/src/components/CreatePost.tsx | 26 +++++++------- client/src/components/Post.tsx | 13 ++++--- client/src/components/ui/mode-toggle.tsx | 4 +-- client/src/pages/Posts.tsx | 29 ++++++++++----- 5 files changed, 54 insertions(+), 63 deletions(-) diff --git a/client/src/api/posts.ts b/client/src/api/posts.ts index a0d0674..70cf3fc 100644 --- a/client/src/api/posts.ts +++ b/client/src/api/posts.ts @@ -2,40 +2,23 @@ import axios from 'axios' import { PostsResponse, Post } from '@/types' import { sleep } from '@/lib/utils' -const baseURL = import.meta.env.VITE_API_URL - -const api = axios.create({ baseURL }) +const api = axios.create({ baseURL: import.meta.env.VITE_API_URL }) export const getPosts = async (skip: number = 0, limit: number = 10): Promise => { try { - const { data } = await api.get(`/posts?limit=${limit}&skip=${skip}`) - - // Add imageUrl and transform reactions to the required format for each post - const postsWithImages = data.posts.map((post) => ({ - ...post, - imageUrl: `https://picsum.photos/seed/${post.id}/800/600` - })) - - return { - ...data, - posts: postsWithImages - } + const { data } = await api.get('/posts', { + params: { skip, limit } + }) + return data } catch (error) { throw handleApiError(error) } } -// Other API functions remain similar but need to be updated with the new Post interface -export const createPost = async (post: Omit): Promise => { +export const createPost = async (post: Omit): Promise => { try { - await sleep(5000) - const { data } = await api.post(`/posts/add`, post) - return { - ...data, - imageUrl: `https://picsum.photos/seed/${data.id}/800/600`, - views: 0, - reactions: { likes: 0, dislikes: 0 } - } + const { data } = await api.post('/posts', post) + return data } catch (error) { throw handleApiError(error) } @@ -44,18 +27,12 @@ export const createPost = async (post: Omit): export const updatePost = async (post: Partial & { id: number }): Promise => { try { const { data } = await api.put(`/posts/${post.id}`, post) - return { - ...data, - imageUrl: `https://picsum.photos/seed/${data.id}/800/600`, - reactions: post.reactions || { likes: 0, dislikes: 0 }, - views: post.views || 0 - } + return data } catch (error) { throw handleApiError(error) } } -// Delete a post export const deletePost = async (id: number): Promise => { try { await api.delete(`/posts/${id}`) @@ -102,9 +79,7 @@ export const getPostsByUser = async (userId: number): Promise => export const updateReaction = async (postId: number, type: 'like' | 'dislike') => { try { await sleep(3000) - const { data } = await api.put(`/posts/${postId}`, { - body: JSON.stringify({ reactions: { [type]: +1 } }) - }) + const { data } = await api.put(`/posts/${postId}`, { reactions: { [type]: +1 } }) return data } catch (error) { diff --git a/client/src/components/CreatePost.tsx b/client/src/components/CreatePost.tsx index e0ec622..9a3c2dc 100644 --- a/client/src/components/CreatePost.tsx +++ b/client/src/components/CreatePost.tsx @@ -1,7 +1,7 @@ import { useRef, useState } from 'react' import { Button, Input, Textarea } from '@/components/ui' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' -import { Loader2, Plus } from 'lucide-react' +import { ListRestart, Loader2, Plus } from 'lucide-react' import { useCreatePost } from '@/hooks' const CreatePost = () => { @@ -41,6 +41,7 @@ const CreatePost = () => { title: post.title.trim(), body: post.body.trim(), userId: 1, + imageUrl: URL.createObjectURL(post.image as File), tags: post.tags .split(',') .map((tag) => tag.trim()) @@ -50,7 +51,9 @@ const CreatePost = () => { dislikes: 0 } }) - + handleReset() + } + const handleReset = () => { setPost(initialData) if (fileInputRef.current) { fileInputRef.current.value = '' @@ -63,7 +66,7 @@ const CreatePost = () => { Create New Post -
+ { placeholder="eg: radio.png" ref={fileInputRef} /> - +
diff --git a/client/src/components/Post.tsx b/client/src/components/Post.tsx index 1ae73eb..bbb01b8 100644 --- a/client/src/components/Post.tsx +++ b/client/src/components/Post.tsx @@ -14,7 +14,7 @@ import { DialogTrigger } from '@/components/ui/dialog' import { useDeletePost, useUpdatePost, useUpdateReaction } from '@/hooks' -import { type Post as PostType } from '@/types' +import type { Post as PostType } from '@/types' const tagColors = [ 'bg-red-100 text-red-800', @@ -42,6 +42,7 @@ const Post = ({ post }: { post: PostType }) => { const handleSave = () => { handleUpdatePost(editedPost) setIsEditing(false) + setEditedPost(post) } return ( @@ -52,7 +53,7 @@ const Post = ({ post }: { post: PostType }) => { exit={{ opacity: 0, y: -50 }} transition={{ duration: 0.3 }} > - +
{ variant="secondary" className="flex items-center gap-1" onClick={() => handleReaction('like')} + title='Like' > {post.reactions.likes} @@ -118,18 +120,19 @@ const Post = ({ post }: { post: PostType }) => { variant="secondary" className="flex items-center gap-1" onClick={() => handleReaction('dislike')} + title="Dislike" > {post.reactions.dislikes} - + {post.views}
{isEditing ? ( - @@ -138,6 +141,7 @@ const Post = ({ post }: { post: PostType }) => { variant="ghost" onClick={() => setIsEditing(true)} className="text-primary hover:text-primary hover:bg-primary/10" + title="Edit" > Edit @@ -149,6 +153,7 @@ const Post = ({ post }: { post: PostType }) => { diff --git a/client/src/components/ui/mode-toggle.tsx b/client/src/components/ui/mode-toggle.tsx index 0c55898..191832b 100644 --- a/client/src/components/ui/mode-toggle.tsx +++ b/client/src/components/ui/mode-toggle.tsx @@ -3,13 +3,13 @@ import { Button } from './button' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from './dropdown-menu' import { useTheme } from '@/hooks' -export const ModeToggle = () => { +export const ModeToggle = ({ variant, className }: { variant?: 'default' | 'outline'; className?: string }) => { const { setTheme } = useTheme() return ( - +
+ + + +
- + {allPosts.length} posts