Skip to content

Commit

Permalink
refactor: 関数を移動させ1関数あたりのコード量を削減
Browse files Browse the repository at this point in the history
  • Loading branch information
weweweok committed Apr 18, 2024
1 parent a0ceff8 commit eda9dce
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions islands/ImageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { useSignal } from "https://esm.sh/*@preact/signals@1.2.1";
const createAsciiArt = async (image: File) => {
const formData = new FormData();
formData.append("files", image);
const url =
location.hostname === "localhost"
? "http://127.0.0.1:8080/files/"
: "https://create-ascii-art.onrender.com/files/";
const url = location.hostname === "localhost"
? "http://127.0.0.1:8080/files/"
: "https://create-ascii-art.onrender.com/files/";
const response = await fetch(url, {
method: "POST",
body: formData,
Expand All @@ -15,6 +14,29 @@ const createAsciiArt = async (image: File) => {
return response.blob();
};

const loadImageWhenUploadImage = (
fileData: FileReader,
) => (fileData.onload = () => {
const asciiArtPreview = document.getElementById(
"ascii-art",
) as HTMLImageElement;
asciiArtPreview.src = "";
const preview = document.getElementById("preview") as HTMLImageElement;
preview.src = fileData.result as string;
});

const loadImageWhencreateAsciiArt = (
fileData: FileReader,
blobUrl: string,
) => (fileData.onload = () => {
const asciiArtPreview = document.getElementById(
"ascii-art",
) as HTMLImageElement;
asciiArtPreview.src = blobUrl;
const preview = document.getElementById("preview") as HTMLImageElement;
preview.src = "";
});

export default function ImageForm() {
const isActiveFileUpLoderDisable = useSignal(false);
const buttonDisable = useSignal(false);
Expand All @@ -24,14 +46,8 @@ export default function ImageForm() {
isActiveFileUpLoderDisable.value = true;

const fileData = new FileReader();
fileData.onload = () => {
const asciiArtPreview = document.getElementById(
"ascii-art"
) as HTMLImageElement;
asciiArtPreview.src = "";
const preview = document.getElementById("preview") as HTMLImageElement;
preview.src = fileData.result as string;
};
loadImageWhenUploadImage(fileData);

const inputElement = event.target as HTMLInputElement;
if (inputElement.files) {
fileData.readAsDataURL(inputElement.files[0]);
Expand All @@ -41,7 +57,7 @@ export default function ImageForm() {
const upLoadToServer = async (e: Event) => {
e.preventDefault();
const imageElement = document.getElementById(
"upload-form"
"upload-form",
) as HTMLFormElement;
const image = imageElement.files[0];

Expand All @@ -51,14 +67,7 @@ export default function ImageForm() {
const asciiArtBlob = await createAsciiArt(image);
const blobUrl = await window.URL.createObjectURL(asciiArtBlob);
const fileData = new FileReader();
fileData.onload = () => {
const asciiArtPreview = document.getElementById(
"ascii-art"
) as HTMLImageElement;
asciiArtPreview.src = blobUrl;
const preview = document.getElementById("preview") as HTMLImageElement;
preview.src = "";
};
loadImageWhencreateAsciiArt(fileData, blobUrl);
fileData.readAsDataURL(asciiArtBlob);

isActiveFileUpLoderDisable.value = false;
Expand All @@ -85,11 +94,13 @@ export default function ImageForm() {
ファイルをアップロードする
</button>
</form>
{isAnnounsing.value ? (
<div id="announce-generating">
<h1>アスキーアートを生成中...</h1>
</div>
) : undefined}
{isAnnounsing.value
? (
<div id="announce-generating">
<h1>アスキーアートを生成中...</h1>
</div>
)
: undefined}
<img id="preview" class="max-w-xs max-h-56 " />
<img id="ascii-art" src="" class="max-w-xs max-h-56 content-center" />
</div>
Expand Down

0 comments on commit eda9dce

Please sign in to comment.