11"use client"
22
3- import React from "react"
3+ import React , { useState } from "react"
44import dynamic from "next/dynamic"
5+ import Image from "next/image"
56import Link from "next/link"
67
78import { zodResolver } from "@hookform/resolvers/zod"
8- import { Prisma } from "database"
9+ import { Image as ImageType , Prisma } from "database"
10+ import { Upload as UploadIcon , X } from "lucide-react"
911import { useSession } from "next-auth/react"
1012import { useTranslations } from "next-intl"
1113import { Controller , useForm } from "react-hook-form"
1214import AsyncCreatableSelect from "react-select/async-creatable"
13- import { buttonVariants , cn , Label , LoadingButton } from "ui"
15+ import { Button , buttonVariants , cn , Label , LoadingButton , Typography } from "ui"
1416import z from "zod"
1517
1618import { handleCreateUpdatePost } from "@/actions/protect/postAction"
@@ -27,6 +29,7 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
2729 const { title = "" , content = "" , tagOnPost = [ ] , id : postId } = postData || { }
2830 const t = useTranslations ( )
2931 const session = useSession ( )
32+ const [ image , setImage ] = useState < ImageType | null > ( null )
3033
3134 const userId = session ?. data ?. user ?. id
3235
@@ -117,11 +120,35 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
117120 </ div >
118121 </ div >
119122 < div className = "col-span-1" >
120- < Upload >
121- < div className = "flex h-[150px] cursor-pointer items-center justify-center rounded-sm bg-slate-300" >
122- < div > Upload</ div >
123- </ div >
124- </ Upload >
123+ < div className = "relative rounded-lg border-2 p-2" >
124+ < Upload onSelect = { setImage } >
125+ { image ? (
126+ < div className = "relative" >
127+ < Image
128+ src = { image . url }
129+ alt = "image"
130+ width = { 480 }
131+ height = { 270 }
132+ className = "border-1 flex aspect-video w-full cursor-pointer rounded-sm object-cover"
133+ />
134+ </ div >
135+ ) : (
136+ < div className = "border-1 flex aspect-video w-full cursor-pointer flex-col items-center justify-center gap-2 rounded-sm bg-slate-200" >
137+ < UploadIcon />
138+ < Typography variant = "mutedText" > { t ( "uploads.upload_image" ) } </ Typography >
139+ </ div >
140+ ) }
141+ </ Upload >
142+ { image && (
143+ < Button
144+ onClick = { ( ) => setImage ( null ) }
145+ variant = "outline"
146+ className = "absolute right-2 top-2 h-6 w-6 p-0"
147+ >
148+ < X className = "text-destructive" />
149+ </ Button >
150+ ) }
151+ </ div >
125152 < div className = "mt-4" >
126153 < Label > Tags</ Label >
127154 < Controller
0 commit comments