Skip to content

Commit

Permalink
refactor: fetch処理を関数で切り出して、1関数あたりの認知複雑度を改善
Browse files Browse the repository at this point in the history
  • Loading branch information
weweweok committed Apr 18, 2024
1 parent e1f009c commit 1c1e8c1
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions islands/ImageForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
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 response = await fetch(url, {
method: "POST",
body: formData,
mode: "cors",
});
return response.blob();
};

export default function ImageForm() {
const isActiveFileUpLoderDisable = useSignal(false);
const buttonDisable = useSignal(false);
Expand Down Expand Up @@ -35,18 +50,7 @@ export default function ImageForm() {
previewUploadImage.value = true;
isAnnounsing.value = true;

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 response = await fetch(url, {
method: "POST",
body: formData,
mode: "cors",
});
const asciiArtBlob = await response.blob();
const asciiArtBlob = await createAsciiArt(image);
const blobUrl = await window.URL.createObjectURL(asciiArtBlob);
const fileData = new FileReader();
fileData.onload = () => {
Expand Down

0 comments on commit 1c1e8c1

Please sign in to comment.