-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: uploading/removing user avatars & banners
- Loading branch information
Showing
4 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import classNames from "classnames"; | ||
import { Input } from "@/app/(ui)/form/Input"; | ||
import { Select } from "@/app/(ui)/form/Select"; | ||
import { MarkdownTextArea } from "@/app/(ui)/markdown/MarkdownTextArea"; | ||
import { Image } from "@/app/(ui)/Image"; | ||
import { ChangeEvent, MouseEvent, useState } from "react"; | ||
import { | ||
deleteImageAction, | ||
uploadImageAction, | ||
} from "@/app/(ui)/markdown/imageActions"; | ||
import { Button } from "@/app/(ui)/button/Button"; | ||
import { TrashIcon } from "@heroicons/react/16/solid"; | ||
import { | ||
removeUserAvatar, | ||
removeUserBanner, | ||
} from "@/app/settings/loggedInUserActions"; | ||
import { Spinner } from "@/app/(ui)/Spinner"; | ||
|
||
type Props = { | ||
readonly inputId: string; | ||
readonly label: "Avatar" | "Banner"; | ||
readonly className?: string; | ||
readonly currentUrl?: string; | ||
}; | ||
|
||
export const ProfileImageInput = (props: Props) => { | ||
const [url, setUrl] = useState(props.currentUrl); | ||
const [imageDeleteUrl, setImageDeleteUrl] = useState<string | null>(null); | ||
|
||
const [uploading, setUploading] = useState(false); | ||
const [uploadError, setUploadError] = useState<string | null>(null); | ||
|
||
const handleImageUpload = async (e: ChangeEvent<HTMLInputElement>) => { | ||
const file = e.currentTarget.files?.[0]; | ||
if (file) { | ||
setUploading(true); | ||
setUploadError(null); | ||
const form = new FormData(); | ||
|
||
form.set("image", file); | ||
|
||
const res = await uploadImageAction(form); | ||
if (res.url && res.delete_url) { | ||
setUrl(res.url); | ||
setImageDeleteUrl(res.delete_url); | ||
} else { | ||
// @ts-ignore | ||
e.target.value = null; | ||
setUrl(undefined); | ||
setUploadError(`Error: ${res.msg}`); | ||
} | ||
|
||
setUploading(false); | ||
} | ||
}; | ||
|
||
const handleDeleteImage = async (e: MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
if (imageDeleteUrl) { | ||
await deleteImageAction(imageDeleteUrl); | ||
} | ||
if (props.label === "Avatar") { | ||
await removeUserAvatar(); | ||
} else { | ||
await removeUserBanner(); | ||
} | ||
|
||
const imageInput = document.getElementById( | ||
`${props.inputId}-file`, | ||
)! as HTMLInputElement; | ||
// @ts-ignore | ||
imageInput.value = null; | ||
|
||
setUrl(""); | ||
setImageDeleteUrl(null); | ||
}; | ||
|
||
const handleUrlChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
setUrl(e.currentTarget.value); | ||
}; | ||
|
||
return ( | ||
<div | ||
className={classNames( | ||
"flex max-w-96 flex-wrap items-center", | ||
props.className, | ||
)} | ||
> | ||
<div className={"flex items-center justify-between"}> | ||
<label | ||
className={classNames( | ||
"block flex items-center gap-2 text-sm font-medium leading-6", | ||
)} | ||
htmlFor={props.inputId} | ||
> | ||
{props.label} {uploading && <Spinner />}{" "} | ||
{uploadError && ( | ||
<span className={"text-sm font-normal text-rose-400"}> | ||
{uploadError} | ||
</span> | ||
)} | ||
</label> | ||
</div> | ||
|
||
<Input | ||
disabled={uploading} | ||
id={`${props.inputId}-file`} | ||
name={`${props.inputId}-file`} | ||
onChange={handleImageUpload} | ||
type={"file"} | ||
/> | ||
<div className={"relative w-full"}> | ||
{url && props.label === "Avatar" && ( | ||
<div className={"mt-2 flex items-end gap-4"}> | ||
<div> | ||
<div className={"relative h-[100px] w-[100px]"}> | ||
<Image | ||
alt={"Preview large"} | ||
className={"rounded object-cover"} | ||
fill={true} | ||
sizes={"100px"} | ||
src={url} | ||
/> | ||
</div> | ||
<span className={"text-xs text-neutral-400"}>{"Large"}</span> | ||
</div> | ||
<div> | ||
<div className={"relative h-[20px] w-[20px]"}> | ||
<Image | ||
alt={"Preview small"} | ||
className={"rounded object-cover"} | ||
fill={true} | ||
sizes={"100px"} | ||
src={url} | ||
/> | ||
</div> | ||
<span className={"text-xs text-neutral-400"}>{"Small"}</span> | ||
</div> | ||
</div> | ||
)} | ||
{url && props.label === "Banner" && ( | ||
<div className={"mt-2 flex items-end gap-4"}> | ||
<div className={"relative h-[100px] w-96"}> | ||
<Image | ||
alt={"Preview"} | ||
className={"rounded object-cover"} | ||
fill={true} | ||
sizes={"400px"} | ||
src={url} | ||
/> | ||
</div> | ||
</div> | ||
)} | ||
{url && ( | ||
<Button | ||
className={"absolute right-0 top-2 flex items-center gap-1"} | ||
color={"danger"} | ||
onClick={handleDeleteImage} | ||
> | ||
<TrashIcon className={"h-4"} /> | ||
{"Remove image"} | ||
</Button> | ||
)} | ||
</div> | ||
<input | ||
className={"hidden"} | ||
id={props.inputId} | ||
name={props.inputId} | ||
value={url} | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters