-
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.
Merge pull request #284 from BY-juun/master
Refactor: image upload ν μΌλ‘ μΆμν, giscus attribute μμν
- Loading branch information
Showing
10 changed files
with
132 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ServerURL } from "@constants"; | ||
import { useCallback, useState } from "react"; | ||
|
||
interface UploadImgResponse { | ||
url: string; | ||
uploaded: boolean; | ||
} | ||
|
||
interface Params { | ||
onImageUploadSuccess?: (params: UploadImgResponse) => void; | ||
} | ||
|
||
const useImageUpload = (params?: Params) => { | ||
const [uploadedImgSrc, setUploadedImgSrc] = useState<string | null>(null); | ||
|
||
const handleClickUploadButton = useCallback(() => { | ||
const inputElem = createInputElement(); | ||
|
||
document.body.appendChild(inputElem); | ||
|
||
inputElem.click(); | ||
|
||
inputElem.onchange = async () => { | ||
const file = inputElem.files; | ||
|
||
if (!file) { | ||
return; | ||
} | ||
|
||
const { url, uploaded } = await uploadImg(file[0]); | ||
params?.onImageUploadSuccess?.({ url, uploaded }); | ||
setUploadedImgSrc(url); | ||
document.body.removeChild(inputElem); | ||
}; | ||
}, [params]); | ||
|
||
return { uploadedImgSrc, setUploadedImgSrc, handleClickUploadButton }; | ||
}; | ||
|
||
const uploadImg = async (img: File) => { | ||
const formData = new FormData(); | ||
formData.append("img", img); | ||
|
||
const response = await fetch(`${ServerURL}/uploads`, { | ||
method: "post", | ||
body: formData, | ||
}); | ||
|
||
return (await response.json()) as UploadImgResponse; | ||
}; | ||
|
||
const createInputElement = () => { | ||
const inputElem = document.createElement("input"); | ||
inputElem.setAttribute("type", "file"); | ||
inputElem.setAttribute("accept", "image/*"); | ||
inputElem.style.display = "none"; | ||
document.body.appendChild(inputElem); | ||
return inputElem; | ||
}; | ||
|
||
export default useImageUpload; |
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
6c42812
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blog β ./
blog-topaz-omega-54.vercel.app
blog-git-production-by-juun.vercel.app
blog-by-juun.vercel.app
byjuun.com
www.byjuun.com