Skip to content

Commit cf407f1

Browse files
committed
feat: add image for post create/update
1 parent c45413c commit cf407f1

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

apps/web/@/molecules/post-form/index.tsx

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"use client"
22

3-
import React from "react"
3+
import React, { useState } from "react"
44
import dynamic from "next/dynamic"
5+
import Image from "next/image"
56
import Link from "next/link"
67

78
import { 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"
911
import { useSession } from "next-auth/react"
1012
import { useTranslations } from "next-intl"
1113
import { Controller, useForm } from "react-hook-form"
1214
import AsyncCreatableSelect from "react-select/async-creatable"
13-
import { buttonVariants, cn, Label, LoadingButton } from "ui"
15+
import { Button, buttonVariants, cn, Label, LoadingButton, Typography } from "ui"
1416
import z from "zod"
1517

1618
import { 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

apps/web/@/molecules/upload/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactNode } from "react"
1+
import { ReactNode, useState } from "react"
22

33
import { Image } from "database"
44
import { X } from "lucide-react"
@@ -95,10 +95,19 @@ const TotalItems: React.FC = () => {
9595

9696
const Upload: React.FC<UploadProps> = ({ children, onSelect }) => {
9797
const t = useTranslations("uploads")
98+
const [open, setOpen] = useState(false)
99+
100+
const onSelectImage = (image?: Image) => {
101+
onSelect(image)
102+
setOpen(false)
103+
}
98104

99105
return (
100106
<FileManagerContainer>
101-
<Dialog>
107+
<Dialog
108+
open={open}
109+
onOpenChange={setOpen}
110+
>
102111
<DialogTrigger asChild>{children}</DialogTrigger>
103112
<DialogContent className="w-full max-w-[800px] gap-0 p-0">
104113
<DialogHeader className="mb-0 flex flex-row items-center gap-4 border-b px-4 py-1">
@@ -115,7 +124,7 @@ const Upload: React.FC<UploadProps> = ({ children, onSelect }) => {
115124
<TotalItems />
116125
<SelectedFiles />
117126
</div>
118-
<SelectButton onSelect={onSelect} />
127+
<SelectButton onSelect={onSelectImage} />
119128
</DialogFooter>
120129
</DialogContent>
121130
</Dialog>

0 commit comments

Comments
 (0)